#include <stdatomic.h>
#include <stdint.h>
#include <stdbool.h>

#include <infiniband/verbs_api.h>
#include <infiniband/verbs.h>
#include <threads.h>

#define READ_ONCE(x) ({ \
    typeof(x) val; \
    __atomic_thread_fence(__ATOMIC_ACQUIRE); \
    val = (x); \
    __atomic_thread_fence(__ATOMIC_ACQUIRE); \
    val; \
})

struct ib_res_struct {
    struct ibv_context      *ctx;
    struct ibv_pd           *pd;            // Protection Domain
    struct ibv_mr           *mr;            // Memory Registration
    struct ibv_cq           *cq;            // Completion Queue
    struct ibv_qp           **qp;           // Queue Pair (send, recv)
    struct ibv_srq          *srq;           // Shared Receive Queue
    struct ibv_port_attr    port_attr;
    struct ibv_device_attr  dev_attr;

    int                     nr_qps;
    char                    *ib_buf;
    size_t                  ib_buf_size;
};

struct ib_res_struct IB_RES;

int run_as_server() {
    // byte buffer in ib_res
    char *buf = IB_RES.ib_buf;

    char prev_char = *buf;
    while(READ_ONCE(*buf) != 0x8f) {
    }
}