#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 dummy;
};
struct nlmsghdr {
  int nlmsg_type;
};
struct nlmsgerr {
  int /*<<< orphan*/ error;
};
struct msghdr {
  void *member_0;
  int member_1;
  int member_3;
  int /*<<< orphan*/ member_6;
  int /*<<< orphan*/ member_5;
  int /*<<< orphan*/ *member_4;
  struct iovec *member_2;
};
struct iovec {
  char *member_0;
  int member_1;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
#define NLMSG_DONE 129
#define NLMSG_ERROR 128
struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *, int);
scalar_t__ NLMSG_OK(struct nlmsghdr *, int);
int /*<<< orphan*/ printf(char *, ...);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int nltest_recvmsg(int sock) {
  int len;
  char buf[4096];
  struct iovec iov = {buf, sizeof(buf)};
  struct sockaddr_nl sa;
  struct msghdr msg = {(void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0};
  struct nlmsghdr *nh = (struct nlmsghdr *)buf;
  len = recvmsg(sock, &msg, 0);
  for (; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
    switch (nh->nlmsg_type) {
    case NLMSG_DONE:
      printf("NLMSG_DONE\n");
      break;
    case NLMSG_ERROR: {
      struct nlmsgerr *nlmerr = (struct nlmsgerr *)NLMSG_DATA(nh);
      if (nlmerr->error == 0) {
        printf("NLMSG_ACK\n");
      } else {
        printf("NLMSG_ERROR\n");
      }
      break;
    }
    default:
      printf("NLMSG type %d\n", nh->nlmsg_type);
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<nltest_recvmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1020(%rbp)
movl   $0x1000,-0x1018(%rbp)
lea    -0x1028(%rbp),%rax
mov    %rax,-0x1050(%rbp)
movl   $0x4,-0x1048(%rbp)
lea    -0x1020(%rbp),%rax
mov    %eax,-0x1044(%rbp)
movl   $0x1,-0x1040(%rbp)
movl   $0x0,-0x103c(%rbp)
movq   $0x0,-0x1038(%rbp)
movq   $0x0,-0x1030(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1050(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %eax,-0x8(%rbp)
mov    -0x1058(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     12c8 <nltest_recvmsg+0x178>
mov    -0x1058(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1064(%rbp)
sub    $0x80,%eax
je     1248 <nltest_recvmsg+0xf8>
jmp    121f <nltest_recvmsg+0xcf>
mov    -0x1064(%rbp),%eax
sub    $0x81,%eax
jne    1291 <nltest_recvmsg+0x141>
jmp    1235 <nltest_recvmsg+0xe5>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a8 <nltest_recvmsg+0x158>
mov    -0x1058(%rbp),%rdi
call   1050 <NLMSG_DATA@plt>
mov    %rax,-0x1060(%rbp)
mov    -0x1060(%rbp),%rax
cmpl   $0x0,(%rax)
jne    127e <nltest_recvmsg+0x12e>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128c <nltest_recvmsg+0x13c>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a8 <nltest_recvmsg+0x158>
mov    -0x1058(%rbp),%rax
mov    (%rax),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ad <nltest_recvmsg+0x15d>
mov    -0x1058(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1070 <NLMSG_NEXT@plt>
mov    %rax,-0x1058(%rbp)
jmp    11e7 <nltest_recvmsg+0x97>
mov    -0x8(%rbp),%eax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nltest_recvmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1048,%rsp
lea    0x40(%rsp),%rbx
mov    %rbx,0x8(%rsp)
movl   $0x1000,0x10(%rsp)
mov    %rsp,%rax
mov    %rax,0x18(%rsp)
movl   $0x4,0x20(%rsp)
lea    0x8(%rsp),%rax
mov    %eax,0x24(%rsp)
movl   $0x1,0x28(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x2c(%rsp)
movl   $0x0,0x3c(%rsp)
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <NLMSG_OK@plt>
test   %rax,%rax
je     1271 <nltest_recvmsg+0x111>
lea    0xe3f(%rip),%r14
lea    0xe2c(%rip),%r15
lea    0xe3b(%rip),%r12
lea    0xe0f(%rip),%r13
jmp    1226 <nltest_recvmsg+0xc6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <NLMSG_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1040 <NLMSG_OK@plt>
test   %rax,%rax
je     1271 <nltest_recvmsg+0x111>
mov    (%rbx),%esi
cmp    $0x80,%esi
je     1250 <nltest_recvmsg+0xf0>
cmp    $0x81,%esi
jne    1200 <nltest_recvmsg+0xa0>
mov    %r12,%rdi
call   1030 <puts@plt>
jmp    120a <nltest_recvmsg+0xaa>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <NLMSG_DATA@plt>
cmpl   $0x0,(%rax)
je     1267 <nltest_recvmsg+0x107>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    120a <nltest_recvmsg+0xaa>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    120a <nltest_recvmsg+0xaa>
mov    %ebp,%eax
add    $0x1048,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.sockaddr_nl = type { i32 }
%struct.msghdr = type { i8*, i32, i32, i32, i32, i32*, %struct.iovec* }
%struct.nlmsghdr = type { i32 }
%struct.nlmsgerr = type { i32 }
@.str.3 = [15 x i8] c"NLMSG type %d\0A\00"
@str = [12 x i8] c"NLMSG_ERROR\00"
@str.4 = [10 x i8] c"NLMSG_ACK\00"
@str.5 = [11 x i8] c"NLMSG_DONE\00"
define i32 @nltest_recvmsg(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.iovec
%4 = alloca %struct.sockaddr_nl
%5 = alloca %struct.msghdr
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.iovec* %3 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
store i8* %6, i8** %8
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
store i32 4096, i32* %9
%10 = bitcast %struct.sockaddr_nl* %4 to i8*
%11 = bitcast %struct.msghdr* %5 to i8*
%12 = bitcast %struct.msghdr* %5 to %struct.sockaddr_nl**
store %struct.sockaddr_nl* %4, %struct.sockaddr_nl** %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 4, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
%15 = ptrtoint %struct.iovec* %3 to i64
%16 = trunc i64 %15 to i32
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
%19 = bitcast [4096 x i8]* %2 to %struct.nlmsghdr*
%20 = bitcast i32* %18 to i8*
%21 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%22 = call i64 @NLMSG_OK(%struct.nlmsghdr* %19, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %1
%25 = bitcast [4096 x i8]* %2 to i32*
br label %26
26: ; preds = %24, %44
%27 = phi %struct.nlmsghdr* [ %45, %44 ], [ %19, %24 ]
%28 = phi i32* [ %46, %44 ], [ %25, %24 ]
%29 = load i32, i32* %28
switch i32 %29, label %42 [
i32 129, label %30
i32 128, label %32
]
30: ; preds = %26
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0))
br label %44
32: ; preds = %26
%33 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %27)
%34 = inttoptr i64 %33 to %struct.nlmsgerr*
%35 = getelementptr inbounds %struct.nlmsgerr, %struct.nlmsgerr* %34, i64 0, i32 0
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %44
40: ; preds = %32
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %44
42: ; preds = %26
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %44
44: ; preds = %38, %40, %30, %42
%45 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %27, i32 %21)
%46 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %45, i64 0, i32 0
%47 = call i64 @NLMSG_OK(%struct.nlmsghdr* %45, i32 %21)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %26
49: ; preds = %44, %1
ret i32 %21
}