#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 {
  scalar_t__ nlmsg_type;
  scalar_t__ nlmsg_seq;
  scalar_t__ nlmsg_pid;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ NLMSG_DONE;
scalar_t__ NLMSG_ERROR;
scalar_t__ NLMSG_NEXT(struct nlmsghdr *, int);
scalar_t__ NLMSG_OK(struct nlmsghdr *, unsigned int);
int /*<<< orphan*/ free(void *);
scalar_t__ getpid();
void *malloc(size_t);
int netlink_recv(int, void *, size_t);
struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_done) {
  size_t l_size = 4096;
  void *l_buffer = NULL;
  for (;;) {
    free(l_buffer);
    l_buffer = malloc(l_size);
    int l_read = netlink_recv(p_socket, l_buffer, l_size);
    *p_size = l_read;
    if (l_read == -2) {
      free(l_buffer);
      return NULL;
    }
    if (l_read >= 0) {
      pid_t l_pid = getpid();
      struct nlmsghdr *l_hdr;
      for (l_hdr = (struct nlmsghdr *)l_buffer;
           NLMSG_OK(l_hdr, (unsigned int)l_read);
           l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read)) {
        if ((pid_t)l_hdr->nlmsg_pid != l_pid ||
            (int)l_hdr->nlmsg_seq != p_socket) {
          continue;
        }
        if (l_hdr->nlmsg_type == NLMSG_DONE) {
          *p_done = 1;
          break;
        }
        if (l_hdr->nlmsg_type == NLMSG_ERROR) {
          free(l_buffer);
          return NULL;
        }
      }
      return (struct nlmsghdr *)l_buffer;
    }
    l_size *= 2;
  }
}


x86 assembly (clang, -O0):
<getNetlinkResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x1000,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <netlink_recv@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0xfffffffe,-0x34(%rbp)
jne    11d5 <getNetlinkResponse+0x75>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <getNetlinkResponse+0x15b>
cmpl   $0x0,-0x34(%rbp)
jl     12aa <getNetlinkResponse+0x14a>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1050 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     129d <getNetlinkResponse+0x13d>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
cmp    -0x40(%rbp),%rax
jne    122b <getNetlinkResponse+0xcb>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0xc(%rbp),%eax
je     1230 <getNetlinkResponse+0xd0>
jmp    1288 <getNetlinkResponse+0x128>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <getNetlinkResponse+0xf6>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
jmp    129d <getNetlinkResponse+0x13d>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <getNetlinkResponse+0x123>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bb <getNetlinkResponse+0x15b>
jmp    1288 <getNetlinkResponse+0x128>
mov    -0x48(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1080 <NLMSG_NEXT@plt>
mov    %rax,-0x48(%rbp)
jmp    11f2 <getNetlinkResponse+0x92>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12bb <getNetlinkResponse+0x15b>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1183 <getNetlinkResponse+0x23>
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNetlinkResponse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%r15d
xor    %edi,%edi
call   1030 <free@plt>
mov    $0x1000,%ebx
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    $0x1000,%edx
mov    %r15d,%edi
mov    %rax,%rsi
call   1060 <netlink_recv@plt>
mov    %eax,(%r12)
cmp    $0xfffffffe,%eax
je     11e3 <getNetlinkResponse+0x83>
mov    %eax,%ebp
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
jns    1200 <getNetlinkResponse+0xa0>
add    %rbx,%rbx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %r15d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <netlink_recv@plt>
mov    %eax,%ebp
mov    %eax,(%r12)
cmp    $0xfffffffe,%eax
jne    11b0 <getNetlinkResponse+0x50>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
je     11ee <getNetlinkResponse+0x8e>
mov    %r14,(%rsp)
mov    0x2db4(%rip),%r14
mov    %r13,%rbx
jmp    124c <getNetlinkResponse+0xec>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <NLMSG_NEXT@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <NLMSG_OK@plt>
test   %rax,%rax
je     11ee <getNetlinkResponse+0x8e>
cmp    %r12,0x10(%rbx)
jne    1230 <getNetlinkResponse+0xd0>
cmp    %r15d,0x8(%rbx)
jne    1230 <getNetlinkResponse+0xd0>
mov    (%rbx),%rax
cmp    (%r14),%rax
je     1271 <getNetlinkResponse+0x111>
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    1230 <getNetlinkResponse+0xd0>
jmp    11e3 <getNetlinkResponse+0x83>
mov    (%rsp),%rax
movl   $0x1,(%rax)
jmp    11ee <getNetlinkResponse+0x8e>

LLVM IR (-O0):
%struct.nlmsghdr = type { i64, i64, i64 }
@NLMSG_DONE = global i64 0
@NLMSG_ERROR = global i64 0
define %struct.nlmsghdr* @getNetlinkResponse(i32 %0, i32* %1, i32* %2) {
%4 = alloca %struct.nlmsghdr*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca %struct.nlmsghdr*
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i64 4096, i64* %8
store i8* null, i8** %9
br label %13
13: ; preds = %81, %3
%14 = load i8*, i8** %9
%15 = call i32 @free(i8* %14)
%16 = load i64, i64* %8
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %9
%18 = load i32, i32* %5
%19 = load i8*, i8** %9
%20 = load i64, i64* %8
%21 = call i32 @netlink_recv(i32 %18, i8* %19, i64 %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = load i32*, i32** %6
store i32 %22, i32* %23
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, -2
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
store %struct.nlmsghdr* null, %struct.nlmsghdr** %4
br label %84
29: ; preds = %13
%30 = load i32, i32* %10
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %81
32: ; preds = %29
%33 = call i64 (...) @getpid()
store i64 %33, i64* %11
%34 = load i8*, i8** %9
%35 = bitcast i8* %34 to %struct.nlmsghdr*
store %struct.nlmsghdr* %35, %struct.nlmsghdr** %12
br label %36
36: ; preds = %73, %32
%37 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%38 = load i32, i32* %10
%39 = call i64 @NLMSG_OK(%struct.nlmsghdr* %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %78
41: ; preds = %36
%42 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%43 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %42, i32 0, i32 2
%44 = load i64, i64* %43
%45 = load i64, i64* %11
%46 = icmp ne i64 %44, %45
br i1 %46, label %54, label %47
47: ; preds = %41
%48 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%49 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %48, i32 0, i32 1
%50 = load i64, i64* %49
%51 = trunc i64 %50 to i32
%52 = load i32, i32* %5
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %47, %41
br label %73
55: ; preds = %47
%56 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%57 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* @NLMSG_DONE
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = load i32*, i32** %7
store i32 1, i32* %62
br label %78
63: ; preds = %55
%64 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%65 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %64, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load i64, i64* @NLMSG_ERROR
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %72
69: ; preds = %63
%70 = load i8*, i8** %9
%71 = call i32 @free(i8* %70)
store %struct.nlmsghdr* null, %struct.nlmsghdr** %4
br label %84
72: ; preds = %63
br label %73
73: ; preds = %72, %54
%74 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%75 = load i32, i32* %10
%76 = call i64 @NLMSG_NEXT(%struct.nlmsghdr* %74, i32 %75)
%77 = inttoptr i64 %76 to %struct.nlmsghdr*
store %struct.nlmsghdr* %77, %struct.nlmsghdr** %12
br label %36
78: ; preds = %61, %36
%79 = load i8*, i8** %9
%80 = bitcast i8* %79 to %struct.nlmsghdr*
store %struct.nlmsghdr* %80, %struct.nlmsghdr** %4
br label %84
81: ; preds = %29
%82 = load i64, i64* %8
%83 = mul i64 %82, 2
store i64 %83, i64* %8
br label %13
84: ; preds = %78, %69, %26
%85 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
ret %struct.nlmsghdr* %85
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i64, i64, i64 }
@NLMSG_DONE = global i64 0
@NLMSG_ERROR = global i64 0
define %struct.nlmsghdr* @getNetlinkResponse(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @free(i8* null)
%5 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%6 = tail call i32 @netlink_recv(i32 %0, i8* %5, i64 4096)
store i32 %6, i32* %1
%7 = icmp eq i32 %6, -2
br i1 %7, label %13, label %16
8: ; preds = %16
%9 = tail call i32 @free(i8* %18)
%10 = tail call i8* @malloc(i64 %21)
%11 = tail call i32 @netlink_recv(i32 %0, i8* %10, i64 %21)
store i32 %11, i32* %1
%12 = icmp eq i32 %11, -2
br i1 %12, label %13, label %16
13: ; preds = %8, %3
%14 = phi i8* [ %5, %3 ], [ %10, %8 ]
%15 = tail call i32 @free(i8* %14)
br label %53
16: ; preds = %3, %8
%17 = phi i32 [ %11, %8 ], [ %6, %3 ]
%18 = phi i8* [ %10, %8 ], [ %5, %3 ]
%19 = phi i64 [ %21, %8 ], [ 4096, %3 ]
%20 = icmp sgt i32 %17, -1
%21 = shl i64 %19, 1
br i1 %20, label %22, label %8
22: ; preds = %16
%23 = tail call i64 (...) @getpid()
%24 = bitcast i8* %18 to %struct.nlmsghdr*
%25 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %24, i32 %17)
%26 = icmp eq i64 %25, 0
br i1 %26, label %53, label %27
27: ; preds = %22, %48
%28 = phi %struct.nlmsghdr* [ %50, %48 ], [ %24, %22 ]
%29 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i64 0, i32 2
%30 = load i64, i64* %29
%31 = icmp eq i64 %30, %23
br i1 %31, label %32, label %48
32: ; preds = %27
%33 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i64 0, i32 1
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i32
%36 = icmp eq i32 %35, %0
br i1 %36, label %37, label %48
37: ; preds = %32
%38 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %28, i64 0, i32 0
%39 = load i64, i64* %38
%40 = load i64, i64* @NLMSG_DONE
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 1, i32* %2
br label %53
43: ; preds = %37
%44 = load i64, i64* @NLMSG_ERROR
%45 = icmp eq i64 %39, %44
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @free(i8* %18)
br label %53
48: ; preds = %43, %27, %32
%49 = tail call i64 @NLMSG_NEXT(%struct.nlmsghdr* %28, i32 %17)
%50 = inttoptr i64 %49 to %struct.nlmsghdr*
%51 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %50, i32 %17)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %27
53: ; preds = %48, %22, %13, %42, %46
%54 = phi %struct.nlmsghdr* [ %24, %42 ], [ null, %46 ], [ null, %13 ], [ %24, %22 ], [ %24, %48 ]
ret %struct.nlmsghdr* %54
}