#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 */
struct nlmsghdr {
  int nlmsg_flags;
  int /*<<< orphan*/ nlmsg_len;
};
struct nlmsg_entry {
  int dummy;
};
struct nl_handle {
  int dummy;
};
struct iovec {
  int /*<<< orphan*/ iov_len;
  struct nlmsghdr *iov_base;
};

/* Variables and functions */
int NLM_F_DUMP;
int nl_recv(struct nl_handle *, struct nlmsg_entry **, int);
int nl_send(struct nl_handle *, struct iovec *, int);
int nl_exchange(struct nl_handle *hnd, struct nlmsghdr *src,
                struct nlmsg_entry **dest) {
  struct iovec iov = {
      .iov_base = src,
      .iov_len = src->nlmsg_len,
  };
  int is_dump;
  int err;
  is_dump = !!(src->nlmsg_flags & NLM_F_DUMP);
  err = nl_send(hnd, &iov, 1);
  if (err)
    return err;
  return nl_recv(hnd, dest, is_dump);
}


x86 assembly (clang, -O0):
<nl_exchange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e95(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1040 <nl_send@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1191 <nl_exchange+0x71>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <nl_exchange+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <nl_recv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_exchange>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
mov    0x4(%rsi),%eax
mov    %eax,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    (%rsi),%r15d
mov    0x2ea2(%rip),%rax
mov    (%rax),%ebp
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1040 <nl_send@plt>
test   %eax,%eax
jne    116e <nl_exchange+0x4e>
xor    %edx,%edx
test   %r15d,%ebp
setne  %dl
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <nl_recv@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nl_handle = type { i32 }
%struct.nlmsghdr = type { i32, i32 }
%struct.nlmsg_entry = type { i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
@NLM_F_DUMP = global i32 0
define i32 @nl_exchange(%struct.nl_handle* %0, %struct.nlmsghdr* %1, %struct.nlmsg_entry** %2) {
%4 = alloca i32
%5 = alloca %struct.nl_handle*
%6 = alloca %struct.nlmsghdr*
%7 = alloca %struct.nlmsg_entry**
%8 = alloca %struct.iovec
%9 = alloca i32
%10 = alloca i32
store %struct.nl_handle* %0, %struct.nl_handle** %5
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %6
store %struct.nlmsg_entry** %2, %struct.nlmsg_entry*** %7
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
%12 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%13 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %12, i32 0, i32 1
%14 = load i32, i32* %13
store i32 %14, i32* %11
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
store %struct.nlmsghdr* %16, %struct.nlmsghdr** %15
%17 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%18 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @NLM_F_DUMP
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
store i32 %25, i32* %9
%26 = load %struct.nl_handle*, %struct.nl_handle** %5
%27 = call i32 @nl_send(%struct.nl_handle* %26, %struct.iovec* %8, i32 1)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %3
%31 = load i32, i32* %10
store i32 %31, i32* %4
br label %37
32: ; preds = %3
%33 = load %struct.nl_handle*, %struct.nl_handle** %5
%34 = load %struct.nlmsg_entry**, %struct.nlmsg_entry*** %7
%35 = load i32, i32* %9
%36 = call i32 @nl_recv(%struct.nl_handle* %33, %struct.nlmsg_entry** %34, i32 %35)
store i32 %36, i32* %4
br label %37
37: ; preds = %32, %30
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
%struct.nl_handle = type { i32 }
%struct.nlmsghdr = type { i32, i32 }
%struct.nlmsg_entry = type { i32 }
%struct.iovec = type { i32, %struct.nlmsghdr* }
@NLM_F_DUMP = global i32 0
define i32 @nl_exchange(%struct.nl_handle* %0, %struct.nlmsghdr* %1, %struct.nlmsg_entry** %2) {
%4 = alloca %struct.iovec
%5 = bitcast %struct.iovec* %4 to i8*
%6 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
%7 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 1
%8 = load i32, i32* %7
store i32 %8, i32* %6
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %9
%10 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @NLM_F_DUMP
%13 = call i32 @nl_send(%struct.nl_handle* %0, %struct.iovec* %4, i32 1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = and i32 %12, %11
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @nl_recv(%struct.nl_handle* %0, %struct.nlmsg_entry** %2, i32 %18)
br label %20
20: ; preds = %3, %15
%21 = phi i32 [ %19, %15 ], [ %13, %3 ]
ret i32 %21
}