#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 /*<<< orphan*/ uint8_t;
struct nlmsghdr {
  scalar_t__ nlmsg_type;
  int nlmsg_len;
};
struct inet_diag_msg {
  int dummy;
};
typedef int /*<<< orphan*/ recv_buf;

/* Variables and functions */
int /*<<< orphan*/ AF_NETLINK;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ NETLINK_INET_DIAG;
scalar_t__ NLMSG_DATA(struct nlmsghdr *);
scalar_t__ NLMSG_DONE;
scalar_t__ NLMSG_ERROR;
int NLMSG_LENGTH(int);
struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *, int);
scalar_t__ NLMSG_OK(struct nlmsghdr *, int);
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
int /*<<< orphan*/ parse_diag_msg(struct inet_diag_msg *, int);
int /*<<< orphan*/ perror(char *);
int recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ send_diag_msg(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int nl_sock = 0, numbytes = 0, rtalen = 0;
  struct nlmsghdr *nlh;
  uint8_t recv_buf[(getpagesize() < 8192L ? getpagesize() : 8192L)];
  struct inet_diag_msg *diag_msg;
  if ((nl_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_INET_DIAG)) == -1) {
    perror("socket: ");
    return EXIT_FAILURE;
  }
  if (send_diag_msg(nl_sock) < 0) {
    perror("sendmsg: ");
    return EXIT_FAILURE;
  }
  while (1) {
    numbytes = recv(nl_sock, recv_buf, sizeof(recv_buf), 0);
    nlh = (struct nlmsghdr *)recv_buf;
    while (NLMSG_OK(nlh, numbytes)) {
      if (nlh->nlmsg_type == NLMSG_DONE)
        return EXIT_SUCCESS;
      if (nlh->nlmsg_type == NLMSG_ERROR) {
        fprintf(stderr, "Error in netlink message\n");
        return EXIT_FAILURE;
      }
      diag_msg = (struct inet_diag_msg *)NLMSG_DATA(nlh);
      rtalen = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*diag_msg));
      parse_diag_msg(diag_msg, rtalen);
      nlh = NLMSG_NEXT(nlh, numbytes);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
cltq
cmp    $0x2000,%rax
jge    1202 <main+0x52>
mov    $0x0,%al
call   10b0 <getpagesize@plt>
cltq
mov    %rax,-0x50(%rbp)
jmp    1210 <main+0x60>
mov    $0x2000,%eax
mov    %rax,-0x50(%rbp)
jmp    1210 <main+0x60>
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    128c <main+0xdc>
lea    0xd91(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13cb <main+0x21b>
mov    -0x14(%rbp),%edi
call   1090 <send_diag_msg@plt>
cmp    $0x0,%rax
jge    12c2 <main+0x112>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13cb <main+0x21b>
jmp    12c7 <main+0x117>
jmp    12cc <main+0x11c>
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rax
mov    -0x14(%rbp),%edi
shl    $0x2,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,-0x18(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     13c6 <main+0x216>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca3(%rip),%rcx
cmp    (%rcx),%rax
jne    1336 <main+0x186>
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13cb <main+0x21b>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
jne    137a <main+0x1ca>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
lea    0xcb6(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13cb <main+0x21b>
mov    -0x28(%rbp),%rdi
call   1080 <NLMSG_DATA@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x64(%rbp)
mov    $0x4,%edi
call   1040 <NLMSG_LENGTH@plt>
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <parse_diag_msg@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10c0 <NLMSG_NEXT@plt>
mov    %rax,-0x28(%rbp)
jmp    12f1 <main+0x141>
jmp    12c7 <main+0x117>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   10b0 <getpagesize@plt>
mov    $0x2000,%r14d
cmp    $0x1fff,%eax
jg     11df <main+0x2f>
xor    %eax,%eax
call   10b0 <getpagesize@plt>
movslq %eax,%r14
mov    %rsp,%rcx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1305 <main+0x155>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <send_diag_msg@plt>
test   %rax,%rax
js     1313 <main+0x163>
mov    %ebx,-0x2c(%rbp)
shl    $0x2,%r14d
mov    %r14,-0x40(%rbp)
mov    0x2d73(%rip),%r12
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rbx
mov    %rbx,%rsi
mov    -0x40(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
je     1245 <main+0x95>
mov    -0x38(%rbp),%rbx
mov    (%rbx),%rax
cmp    (%r12),%rax
je     12cf <main+0x11f>
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
je     12d8 <main+0x128>
mov    %rbx,%rdi
call   1080 <NLMSG_DATA@plt>
mov    %rax,%r15
mov    0x8(%rbx),%r14d
mov    $0x4,%edi
call   1040 <NLMSG_LENGTH@plt>
sub    %eax,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <parse_diag_msg@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   10c0 <NLMSG_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13d,%esi
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
jne    1270 <main+0xc0>
jmp    1245 <main+0x95>
mov    0x2cd2(%rip),%rax
jmp    12f4 <main+0x144>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ed <main+0x13d>
lea    0xcef(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ed <main+0x13d>

LLVM IR (-O0):
%struct.nlmsghdr = type { i64, i32 }
%struct.inet_diag_msg = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_INET_DIAG = global i32 0
@.str = [9 x i8] c"socket: \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"sendmsg: \00"
@NLMSG_DONE = global i64 0
@EXIT_SUCCESS = global i32 0
@NLMSG_ERROR = global i64 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error in netlink message\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.nlmsghdr*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.inet_diag_msg*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%14 = call i32 (...) @getpagesize()
%15 = sext i32 %14 to i64
%16 = icmp slt i64 %15, 8192
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (...) @getpagesize()
%19 = sext i32 %18 to i64
br label %21
20: ; preds = %2
br label %21
21: ; preds = %20, %17
%22 = phi i64 [ %19, %17 ], [ 8192, %20 ]
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %10
%24 = alloca i32, i64 %22
store i64 %22, i64* %11
%25 = load i32, i32* @AF_NETLINK
%26 = load i32, i32* @SOCK_DGRAM
%27 = load i32, i32* @NETLINK_INET_DIAG
%28 = call i32 @socket(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
store i32 %32, i32* %3
store i32 1, i32* %13
br label %87
33: ; preds = %21
%34 = load i32, i32* %6
%35 = call i64 @send_diag_msg(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %3
store i32 1, i32* %13
br label %87
40: ; preds = %33
br label %41
41: ; preds = %86, %40
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = mul nuw i64 4, %22
%45 = trunc i64 %44 to i32
%46 = call i32 @recv(i32 %43, i32* %24, i32 %45, i32 0)
store i32 %46, i32* %7
%47 = bitcast i32* %24 to %struct.nlmsghdr*
store %struct.nlmsghdr* %47, %struct.nlmsghdr** %9
br label %48
48: ; preds = %71, %42
%49 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%50 = load i32, i32* %7
%51 = call i64 @NLMSG_OK(%struct.nlmsghdr* %49, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %86
53: ; preds = %48
%54 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%55 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = load i64, i64* @NLMSG_DONE
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %61
59: ; preds = %53
%60 = load i32, i32* @EXIT_SUCCESS
store i32 %60, i32* %3
store i32 1, i32* %13
br label %87
61: ; preds = %53
%62 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%63 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = load i64, i64* @NLMSG_ERROR
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
store i32 %70, i32* %3
store i32 1, i32* %13
br label %87
71: ; preds = %61
%72 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%73 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %72)
%74 = inttoptr i64 %73 to %struct.inet_diag_msg*
store %struct.inet_diag_msg* %74, %struct.inet_diag_msg** %12
%75 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%76 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %75, i32 0, i32 1
%77 = load i32, i32* %76
%78 = call i32 @NLMSG_LENGTH(i32 4)
%79 = sub nsw i32 %77, %78
store i32 %79, i32* %8
%80 = load %struct.inet_diag_msg*, %struct.inet_diag_msg** %12
%81 = load i32, i32* %8
%82 = call i32 @parse_diag_msg(%struct.inet_diag_msg* %80, i32 %81)
%83 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%84 = load i32, i32* %7
%85 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %83, i32 %84)
store %struct.nlmsghdr* %85, %struct.nlmsghdr** %9
br label %48
86: ; preds = %48
br label %41
87: ; preds = %67, %59, %37, %30
%88 = load i8*, i8** %10
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64, i32 }
%struct.inet_diag_msg = type { i32 }
@AF_NETLINK = global i32 0
@SOCK_DGRAM = global i32 0
@NETLINK_INET_DIAG = global i32 0
@.str = [9 x i8] c"socket: \00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"sendmsg: \00"
@NLMSG_DONE = global i64 0
@EXIT_SUCCESS = global i32 0
@NLMSG_ERROR = global i64 0
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error in netlink message\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = icmp slt i32 %3, 8192
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 (...) @getpagesize()
%7 = sext i32 %6 to i64
br label %8
8: ; preds = %2, %5
%9 = phi i64 [ %7, %5 ], [ 8192, %2 ]
%10 = alloca i32, i64 %9
%11 = load i32, i32* @AF_NETLINK
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @NETLINK_INET_DIAG
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %55
18: ; preds = %8
%19 = tail call i64 @send_diag_msg(i32 %14)
%20 = icmp slt i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = trunc i64 %9 to i32
%23 = shl i32 %22, 2
%24 = bitcast i32* %10 to %struct.nlmsghdr*
br label %27
25: ; preds = %18
%26 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %55
27: ; preds = %31, %21
%28 = call i32 @recv(i32 %14, i32* %10, i32 %23, i32 0)
%29 = call i64 @NLMSG_OK(%struct.nlmsghdr* %24, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %44, %27
br label %27
32: ; preds = %27, %44
%33 = phi %struct.nlmsghdr* [ %52, %44 ], [ %24, %27 ]
%34 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %33, i64 0, i32 0
%35 = load i64, i64* %34
%36 = load i64, i64* @NLMSG_DONE
%37 = icmp eq i64 %35, %36
br i1 %37, label %55, label %38
38: ; preds = %32
%39 = load i64, i64* @NLMSG_ERROR
%40 = icmp eq i64 %35, %39
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %55
44: ; preds = %38
%45 = call i64 @NLMSG_DATA(%struct.nlmsghdr* %33)
%46 = inttoptr i64 %45 to %struct.inet_diag_msg*
%47 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %33, i64 0, i32 1
%48 = load i32, i32* %47
%49 = call i32 @NLMSG_LENGTH(i32 4)
%50 = sub nsw i32 %48, %49
%51 = call i32 @parse_diag_msg(%struct.inet_diag_msg* %46, i32 %50)
%52 = call %struct.nlmsghdr* @NLMSG_NEXT(%struct.nlmsghdr* %33, i32 %28)
%53 = call i64 @NLMSG_OK(%struct.nlmsghdr* %52, i32 %28)
%54 = icmp eq i64 %53, 0
br i1 %54, label %31, label %32
55: ; preds = %32, %41, %25, %16
%56 = phi i32* [ @EXIT_FAILURE, %16 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_FAILURE, %41 ], [ @EXIT_SUCCESS, %32 ]
%57 = load i32, i32* %56
ret i32 %57
}