#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;
};
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_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_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 == 4);
    int counter = *(volatile uint32_t *)rxbuf + 1;
    if (counter == 0)
      break;
    ibPostReceive(qp, mr, rxbuf, rxbufsize);
    memcpy(txbuf, &counter, sizeof(counter));
    ibPostSendAndWait(qp, mr, txbuf, sizeof(counter), txcq);
  }
  printf("server exiting...\n");
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
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    11ad <server+0x2d>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
lea    -0x40(%rbp),%rdx
call   1040 <ibv_poll_cq@plt>
cmp    $0x1,%eax
jge    11cd <server+0x4d>
jmp    11ad <server+0x2d>
mov    -0x40(%rbp),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
je     1200 <server+0x80>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x4,-0x38(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    122c <server+0xac>
jmp    1272 <server+0xf2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1090 <ibPostReceive@plt>
mov    0x10(%rbp),%rdi
lea    -0x44(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x10(%rbp),%rdx
mov    -0x30(%rbp),%r8
mov    $0x4,%ecx
call   1050 <ibPostSendAndWait@plt>
jmp    11a8 <server+0x28>
lea    0xda5(%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,0x8(%rsp)
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%rbp
mov    0x60(%rsp),%r14
lea    0x18(%rsp),%r15
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%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    124b <server+0xcb>
xor    %edi,%edi
cmpl   $0x4,0x20(%rsp)
sete   %dil
call   1060 <assert@plt>
mov    (%r12),%eax
inc    %eax
mov    %eax,0x4(%rsp)
je     1230 <server+0xb0>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    0x8(%rsp),%rcx
call   1090 <ibPostReceive@plt>
mov    %r14,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    $0x4,%ecx
mov    0x10(%rsp),%r8
call   1050 <ibPostSendAndWait@plt>
jmp    11b0 <server+0x30>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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