#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 sockaddr_nl {
  int /*<<< orphan*/ nl_family;
};
struct nlmsghdr {
  scalar_t__ nlmsg_type;
};
struct nlmsgerr {
  scalar_t__ error;
};
struct msghdr {
  int member_1;
  int member_3;
  int /*<<< orphan*/ member_6;
  int /*<<< orphan*/ member_5;
  int /*<<< orphan*/ *member_4;
  struct iovec *member_2;
  struct sockaddr_nl *member_0;
};
struct iovec {
  char *member_0;
  int member_1;
};

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_ERROR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct sockaddr_nl *malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int _nlmsg_recieve(int fd) {
  struct sockaddr_nl *sa = malloc(sizeof(struct sockaddr_nl));
  memset(sa, 0, sizeof(struct sockaddr_nl));
  sa->nl_family = AF_NETLINK;
  int len = 4096;
  char buf[len];
  struct iovec iov = {buf, len};
  struct msghdr msg = {sa, sizeof(*sa), &iov, 1, NULL, 0, 0};
  recvmsg(fd, &msg, 0);
  struct nlmsghdr *ret = (struct nlmsghdr *)buf;
  if (ret->nlmsg_type == NLMSG_ERROR) {
    struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(ret);
    if (err->error < 0) {
      fprintf(stderr, "recieve error: %d\n", err->error);
      return 1;
    }
  } else {
    fprintf(stderr, "invalid recieve type\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_nlmsg_recieve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x4,%edi
mov    %edi,-0x84(%rbp)
call   1060 <malloc@plt>
mov    -0x84(%rbp),%edx
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1000,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x80(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x60(%rbp)
movl   $0x4,-0x5c(%rbp)
lea    -0x38(%rbp),%rax
mov    %eax,-0x58(%rbp)
movl   $0x1,-0x54(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    -0x80(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    127d <_nlmsg_recieve+0x12d>
mov    -0x68(%rbp),%rdi
call   1050 <NLMSG_DATA@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
cmpq   $0x0,(%rax)
jge    1278 <_nlmsg_recieve+0x128>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
mov    (%rax),%rdx
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    12b5 <_nlmsg_recieve+0x165>
jmp    12a7 <_nlmsg_recieve+0x157>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    12b5 <_nlmsg_recieve+0x165>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nlmsg_recieve>:
push   %rbp
push   %rbx
sub    $0x1048,%rsp
mov    %edi,%ebp
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
lea    0x40(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x1000,0x10(%rsp)
mov    %ebx,0x18(%rsp)
movl   $0x4,0x1c(%rsp)
lea    0x8(%rsp),%rax
mov    %eax,0x20(%rsp)
movl   $0x1,0x24(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
movq   $0x0,0x38(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    0x40(%rsp),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <_nlmsg_recieve+0xc3>
lea    0x40(%rsp),%rdi
call   1050 <NLMSG_DATA@plt>
mov    (%rax),%rbx
test   %rbx,%rbx
jns    120d <_nlmsg_recieve+0xbd>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
shr    $0x3f,%rbx
jmp    122f <_nlmsg_recieve+0xdf>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x1048,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_nl = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
%struct.nlmsghdr = type { i64 }
%struct.nlmsgerr = type { i64 }
@AF_NETLINK = global i32 0
@NLMSG_ERROR = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"recieve error: %d\0A\00"
@.str.1 = [22 x i8] c"invalid recieve type\0A\00"
define i32 @_nlmsg_recieve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_nl*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.iovec
%9 = alloca %struct.msghdr
%10 = alloca %struct.nlmsghdr*
%11 = alloca %struct.nlmsgerr*
%12 = alloca i32
store i32 %0, i32* %3
%13 = call %struct.sockaddr_nl* @malloc(i32 4)
store %struct.sockaddr_nl* %13, %struct.sockaddr_nl** %4
%14 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %4
%15 = call i32 @memset(%struct.sockaddr_nl* %14, i32 0, i32 4)
%16 = load i32, i32* @AF_NETLINK
%17 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %4
%18 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %17, i32 0, i32 0
store i32 %16, i32* %18
store i32 4096, i32* %5
%19 = load i32, i32* %5
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %6
%22 = alloca i8, i64 %20
store i64 %20, i64* %7
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
store i8* %22, i8** %23
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
%25 = load i32, i32* %5
store i32 %25, i32* %24
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
%27 = load %struct.sockaddr_nl*, %struct.sockaddr_nl** %4
%28 = ptrtoint %struct.sockaddr_nl* %27 to i32
store i32 %28, i32* %26
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i32 4, i32* %29
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
%31 = ptrtoint %struct.iovec* %8 to i32
store i32 %31, i32* %30
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store i32 1, i32* %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 4
store i32* null, i32** %33
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 5
store %struct.iovec* null, %struct.iovec** %34
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 6
store %struct.sockaddr_nl* null, %struct.sockaddr_nl** %35
%36 = load i32, i32* %3
%37 = call i32 @recvmsg(i32 %36, %struct.msghdr* %9, i32 0)
%38 = bitcast i8* %22 to %struct.nlmsghdr*
store %struct.nlmsghdr* %38, %struct.nlmsghdr** %10
%39 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%40 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @NLMSG_ERROR
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %59
44: ; preds = %1
%45 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%46 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %45)
%47 = inttoptr i64 %46 to %struct.nlmsgerr*
store %struct.nlmsgerr* %47, %struct.nlmsgerr** %11
%48 = load %struct.nlmsgerr*, %struct.nlmsgerr** %11
%49 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %44
%53 = load i32, i32* @stderr
%54 = load %struct.nlmsgerr*, %struct.nlmsgerr** %11
%55 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i64 %56)
store i32 1, i32* %2
store i32 1, i32* %12
br label %63
58: ; preds = %44
br label %62
59: ; preds = %1
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
store i32 1, i32* %12
br label %63
62: ; preds = %58
store i32 0, i32* %2
store i32 1, i32* %12
br label %63
63: ; preds = %62, %59, %52
%64 = load i8*, i8** %6
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i32, i32, i32*, %struct.iovec*, %struct.sockaddr_nl* }
%struct.sockaddr_nl = type { i32 }
%struct.nlmsghdr = type { i64 }
%struct.nlmsgerr = type { i64 }
@AF_NETLINK = global i32 0
@NLMSG_ERROR = global i64 0
@stderr = global i32 0
@.str = [19 x i8] c"recieve error: %d\0A\00"
@.str.1 = [22 x i8] c"invalid recieve type\0A\00"
define i32 @_nlmsg_recieve(i32 %0) {
%2 = alloca %struct.iovec
%3 = alloca %struct.msghdr
%4 = tail call %struct.sockaddr_nl* @malloc(i32 4)
%5 = tail call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 4)
%6 = load i32, i32* @AF_NETLINK
%7 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i64 0, i32 0
store i32 %6, i32* %7
%8 = alloca [4096 x i8]
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%10 = bitcast %struct.iovec* %2 to i8*
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 0
store i8* %9, i8** %11
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 1
store i32 4096, i32* %12
%13 = bitcast %struct.msghdr* %3 to i8*
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
%15 = ptrtoint %struct.sockaddr_nl* %4 to i64
%16 = trunc i64 %15 to i32
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 4, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
%19 = ptrtoint %struct.iovec* %2 to i64
%20 = trunc i64 %19 to i32
store i32 %20, i32* %18
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store i32 1, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 4
%23 = bitcast i32** %22 to i8*
%24 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%25 = bitcast [4096 x i8]* %8 to i64*
%26 = load i64, i64* %25
%27 = load i64, i64* @NLMSG_ERROR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %1
%30 = bitcast [4096 x i8]* %8 to %struct.nlmsghdr*
%31 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %30)
%32 = inttoptr i64 %31 to %struct.nlmsgerr*
%33 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %32, i64 0, i32 0
%34 = load i64, i64* %33
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i64 %34)
br label %39
39: ; preds = %29, %36
%40 = lshr i64 %34, 63
%41 = trunc i64 %40 to i32
br label %45
42: ; preds = %1
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %39, %42
%46 = phi i32 [ 1, %42 ], [ %41, %39 ]
ret i32 %46
}