#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;
struct ibv_wc {
  scalar_t__ status;
  int byte_len;
};
struct ibv_qp {
  int dummy;
};
struct ibv_mr {
  int dummy;
};
struct ibv_cq {
  int dummy;
};
struct ibv_ah {
  int dummy;
};
typedef int /*<<< orphan*/ counter;

/* Variables and functions */
scalar_t__ IBV_WC_SUCCESS;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ibPostReceive(struct ibv_qp *, struct ibv_mr *, char *,
                                 size_t);
int /*<<< orphan*/ ibPostSendAndWait(struct ibv_ah *, struct ibv_qp *,
                                     struct ibv_mr *, char *, int,
                                     struct ibv_cq *);
int ibv_poll_cq(struct ibv_cq *, int, struct ibv_wc *);
int /*<<< orphan*/ memcpy(char *, int *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
void server(struct ibv_ah *ah, struct ibv_qp *qp, struct ibv_mr *mr,
            struct ibv_cq *rxcq, char *rxbuf, size_t rxbufsize,
            struct ibv_cq *txcq, char *txbuf, size_t txbufsize) {
  while (1) {
    struct ibv_wc iwc;
    while (ibv_poll_cq(rxcq, 1, &iwc) < 1)
      ;
    if (iwc.status != IBV_WC_SUCCESS) {
      fprintf(stderr, "ibv_poll_cq returned failure\n");
      exit(1);
    }
    assert(iwc.byte_len == 44);
    int counter = ((volatile uint32_t *)rxbuf)[10] + 1;
    if (counter == 0)
      break;
    ibPostReceive(qp, mr, rxbuf, rxbufsize);
    memcpy(txbuf, &counter, sizeof(counter));
    ibPostSendAndWait(ah, qp, mr, txbuf, sizeof(counter), txcq);
  }
  printf("server exiting...\n");
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
jmp    11b1 <server+0x31>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
lea    -0x40(%rbp),%rdx
call   1040 <ibv_poll_cq@plt>
cmp    $0x1,%eax
jge    11d1 <server+0x51>
jmp    11b1 <server+0x31>
mov    -0x40(%rbp),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <server+0x84>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x2c,-0x38(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rax
mov    0x28(%rax),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1231 <server+0xb1>
jmp    127c <server+0xfc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1090 <ibPostReceive@plt>
mov    0x18(%rbp),%rdi
lea    -0x44(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x18(%rbp),%rcx
mov    0x10(%rbp),%r9
mov    $0x4,%r8d
call   1050 <ibPostSendAndWait@plt>
jmp    11ac <server+0x2c>
lea    0xd9b(%rip),%rdi
call   1030 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,0x8(%rsp)
lea    0x18(%rsp),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
call   1040 <ibv_poll_cq@plt>
test   %eax,%eax
jle    11b0 <server+0x30>
mov    0x18(%rsp),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <server+0xdd>
xor    %edi,%edi
cmpl   $0x2c,0x20(%rsp)
sete   %dil
call   1060 <assert@plt>
mov    0x28(%r15),%eax
inc    %eax
mov    %eax,0x4(%rsp)
je     1242 <server+0xc2>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    0x10(%rsp),%rcx
call   1090 <ibPostReceive@plt>
mov    0x68(%rsp),%rbp
mov    %rbp,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    $0x4,%r8d
mov    0x60(%rsp),%r9
call   1050 <ibPostSendAndWait@plt>
jmp    11b0 <server+0x30>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.ibv_ah = type { i32 }
%struct.ibv_qp = type { i32 }
%struct.ibv_mr = type { i32 }
%struct.ibv_cq = type { i32 }
%struct.ibv_wc = type { i64, i32 }
@IBV_WC_SUCCESS = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"ibv_poll_cq returned failure\0A\00"
@.str.1 = [19 x i8] c"server exiting...\0A\00"
define void @server(%struct.ibv_ah* %0, %struct.ibv_qp* %1, %struct.ibv_mr* %2, %struct.ibv_cq* %3, i8* %4, i64 %5, %struct.ibv_cq* %6, i8* %7, i64 %8) {
%10 = alloca %struct.ibv_ah*
%11 = alloca %struct.ibv_qp*
%12 = alloca %struct.ibv_mr*
%13 = alloca %struct.ibv_cq*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca %struct.ibv_cq*
%17 = alloca i8*
%18 = alloca i64
%19 = alloca %struct.ibv_wc
%20 = alloca i32
store %struct.ibv_ah* %0, %struct.ibv_ah** %10
store %struct.ibv_qp* %1, %struct.ibv_qp** %11
store %struct.ibv_mr* %2, %struct.ibv_mr** %12
store %struct.ibv_cq* %3, %struct.ibv_cq** %13
store i8* %4, i8** %14
store i64 %5, i64* %15
store %struct.ibv_cq* %6, %struct.ibv_cq** %16
store i8* %7, i8** %17
store i64 %8, i64* %18
br label %21
21: ; preds = %9, %50
br label %22
22: ; preds = %26, %21
%23 = load %struct.ibv_cq*, %struct.ibv_cq** %13
%24 = call i32 @ibv_poll_cq(%struct.ibv_cq* %23, i32 1, %struct.ibv_wc* %19)
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %22
br label %22
27: ; preds = %22
%28 = getelementptr inbounds %struct.ibv_wc, %struct.ibv_wc* %19, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @IBV_WC_SUCCESS
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = getelementptr inbounds %struct.ibv_wc, %struct.ibv_wc* %19, i32 0, i32 1
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 44
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i8*, i8** %14
%43 = bitcast i8* %42 to i32*
%44 = getelementptr inbounds i32, i32* %43, i64 10
%45 = load volatile i32, i32* %44
%46 = add nsw i32 %45, 1
store i32 %46, i32* %20
%47 = load i32, i32* %20
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %36
br label %64
50: ; preds = %36
%51 = load %struct.ibv_qp*, %struct.ibv_qp** %11
%52 = load %struct.ibv_mr*, %struct.ibv_mr** %12
%53 = load i8*, i8** %14
%54 = load i64, i64* %15
%55 = call i32 @ibPostReceive(%struct.ibv_qp* %51, %struct.ibv_mr* %52, i8* %53, i64 %54)
%56 = load i8*, i8** %17
%57 = call i32 @memcpy(i8* %56, i32* %20, i32 4)
%58 = load %struct.ibv_ah*, %struct.ibv_ah** %10
%59 = load %struct.ibv_qp*, %struct.ibv_qp** %11
%60 = load %struct.ibv_mr*, %struct.ibv_mr** %12
%61 = load i8*, i8** %17
%62 = load %struct.ibv_cq*, %struct.ibv_cq** %16
%63 = call i32 @ibPostSendAndWait(%struct.ibv_ah* %58, %struct.ibv_qp* %59, %struct.ibv_mr* %60, i8* %61, i32 4, %struct.ibv_cq* %62)
br label %21
64: ; preds = %49
%65 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.ibv_ah = type { i32 }
%struct.ibv_qp = type { i32 }
%struct.ibv_mr = type { i32 }
%struct.ibv_cq = type { i32 }
%struct.ibv_wc = type { i64, i32 }
@IBV_WC_SUCCESS = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"ibv_poll_cq returned failure\0A\00"
@str = [18 x i8] c"server exiting...\00"
define void @server(%struct.ibv_ah* %0, %struct.ibv_qp* %1, %struct.ibv_mr* %2, %struct.ibv_cq* %3, i8* %4, i64 %5, %struct.ibv_cq* %6, i8* %7, i64 %8) {
%10 = alloca %struct.ibv_wc
%11 = alloca i32
%12 = bitcast %struct.ibv_wc* %10 to i8*
%13 = getelementptr inbounds %struct.ibv_wc, %struct.ibv_wc* %10, i64 0, i32 0
%14 = getelementptr inbounds %struct.ibv_wc, %struct.ibv_wc* %10, i64 0, i32 1
%15 = bitcast i32* %11 to i8*
%16 = getelementptr inbounds i8, i8* %4, i64 40
%17 = bitcast i8* %16 to i32*
br label %18
18: ; preds = %38, %9
br label %19
19: ; preds = %19, %18
%20 = call i32 @ibv_poll_cq(%struct.ibv_cq* %3, i32 1, %struct.ibv_wc* %10)
%21 = icmp slt i32 %20, 1
br i1 %21, label %19, label %22
22: ; preds = %19
%23 = load i64, i64* %13
%24 = load i64, i64* @IBV_WC_SUCCESS
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i32, i32* %14
%32 = icmp eq i32 %31, 44
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load volatile i32, i32* %17
%36 = add nsw i32 %35, 1
store i32 %36, i32* %11
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %30
%39 = call i32 @ibPostReceive(%struct.ibv_qp* %1, %struct.ibv_mr* %2, i8* %4, i64 %5)
%40 = call i32 @memcpy(i8* %7, i32* %11, i32 4)
%41 = call i32 @ibPostSendAndWait(%struct.ibv_ah* %0, %struct.ibv_qp* %1, %struct.ibv_mr* %2, i8* %7, i32 4, %struct.ibv_cq* %6)
br label %18
42: ; preds = %30
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
ret void
}