#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_type;
};
struct nlmsgerr {
  int /*<<< orphan*/ error;
};
typedef scalar_t__ answer;

/* Variables and functions */
int __netlink_send(int, struct nlmsghdr *);
int /*<<< orphan*/ errno;
int netlink_recv(int, struct nlmsghdr *);
int netlink_transaction(int fd, struct nlmsghdr *request,
                        struct nlmsghdr *answer) {
  int ret;
  ret = __netlink_send(fd, request);
  if (ret < 0)
    return -1;
  ret = netlink_recv(fd, answer);
  if (ret < 0)
    return -1;
  ret = 0;
  if (answer->nlmsg_type == 0x2) {
    struct nlmsgerr *err = (struct nlmsgerr *)((
        void *)((char *)(answer) + sizeof(struct nlmsghdr)));
    errno = -err->error;
    if (err->error < 0)
      ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<netlink_transaction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <__netlink_send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1158 <netlink_transaction+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <netlink_transaction+0xad>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <netlink_recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117d <netlink_transaction+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <netlink_transaction+0xad>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x2,(%rax)
jne    11c7 <netlink_transaction+0xa7>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11c2 <netlink_transaction+0xa2>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11c7 <netlink_transaction+0xa7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netlink_transaction>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %edi,%ebp
call   1040 <__netlink_send@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1161 <netlink_transaction+0x41>
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <netlink_recv@plt>
test   %eax,%eax
js     1161 <netlink_transaction+0x41>
xor    %ebx,%ebx
cmpl   $0x2,(%r14)
jne    1161 <netlink_transaction+0x41>
mov    0x4(%r14),%ebx
mov    %ebx,%eax
neg    %eax
mov    0x2e84(%rip),%rcx
mov    %eax,(%rcx)
sar    $0x1f,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.nlmsgerr = type { i32 }
@errno = global i32 0
define i32 @netlink_transaction(i32 %0, %struct.nlmsghdr* %1, %struct.nlmsghdr* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.nlmsghdr*
%7 = alloca %struct.nlmsghdr*
%8 = alloca i32
%9 = alloca %struct.nlmsgerr*
store i32 %0, i32* %5
store %struct.nlmsghdr* %1, %struct.nlmsghdr** %6
store %struct.nlmsghdr* %2, %struct.nlmsghdr** %7
%10 = load i32, i32* %5
%11 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%12 = call i32 @__netlink_send(i32 %10, %struct.nlmsghdr* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %45
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%19 = call i32 @netlink_recv(i32 %17, %struct.nlmsghdr* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %4
br label %45
23: ; preds = %16
store i32 0, i32* %8
%24 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%25 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %43
28: ; preds = %23
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %7
%30 = bitcast %struct.nlmsghdr* %29 to i8*
%31 = getelementptr inbounds i8, i8* %30, i64 4
%32 = bitcast i8* %31 to %struct.nlmsgerr*
store %struct.nlmsgerr* %32, %struct.nlmsgerr** %9
%33 = load %struct.nlmsgerr*, %struct.nlmsgerr** %9
%34 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sub nsw i32 0, %35
store i32 %36, i32* @errno
%37 = load %struct.nlmsgerr*, %struct.nlmsgerr** %9
%38 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %28
store i32 -1, i32* %8
br label %42
42: ; preds = %41, %28
br label %43
43: ; preds = %42, %23
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %22, %15
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
@errno = global i32 0
define i32 @netlink_transaction(i32 %0, %struct.nlmsghdr* %1, %struct.nlmsghdr* %2) {
%4 = tail call i32 @__netlink_send(i32 %0, %struct.nlmsghdr* %1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = tail call i32 @netlink_recv(i32 %0, %struct.nlmsghdr* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %2, i64 1, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 0, %15
store i32 %16, i32* @errno
%17 = ashr i32 %15, 31
br label %18
18: ; preds = %9, %13, %6, %3
%19 = phi i32 [ -1, %3 ], [ -1, %6 ], [ %17, %13 ], [ 0, %9 ]
ret i32 %19
}