#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_seq;
  int nlmsg_pid;
  scalar_t__ nlmsg_type;
  int nlmsg_flags;
};

/* Variables and functions */
scalar_t__ NLMSG_DONE;
scalar_t__ NLMSG_ERROR;
scalar_t__ NLMSG_OK(struct nlmsghdr *, int);
int NLM_F_MULTI;
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId) {
  struct nlmsghdr *nlHdr;
  int readLen = 0, msgLen = 0;
  do {
    if ((readLen = recv(sockFd, bufPtr, 8192 - msgLen, 0)) < 0) {
      perror("SOCK READ: ");
      return -1;
    }
    nlHdr = (struct nlmsghdr *)bufPtr;
    if ((NLMSG_OK(nlHdr, readLen) == 0) || (nlHdr->nlmsg_type == NLMSG_ERROR)) {
      perror("Error in recieved packet");
      return -1;
    }
    if (nlHdr->nlmsg_type == NLMSG_DONE) {
      break;
    } else {
      bufPtr += readLen;
      msgLen += readLen;
    }
    if ((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0) {
      break;
    }
  } while ((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pId));
  return msgLen;
}


x86 assembly (clang, -O0):
<readNlSock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2000,%edx
sub    -0x28(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    118d <readNlSock+0x5d>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <readNlSock+0x13a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     11c3 <readNlSock+0x93>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    11db <readNlSock+0xab>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <readNlSock+0x13a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <readNlSock+0xc8>
jmp    1264 <readNlSock+0x134>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%eax
mov    0x2dc0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1230 <readNlSock+0x100>
jmp    1264 <readNlSock+0x134>
jmp    1235 <readNlSock+0x105>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    $0x1,%al
cmp    -0x14(%rbp),%ecx
mov    %al,-0x29(%rbp)
jne    1259 <readNlSock+0x129>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x18(%rbp),%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1153 <readNlSock+0x23>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readNlSock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,0x4(%rsp)
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r13d
xor    %ebx,%ebx
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xea3(%rip),%rdi
test   %eax,%eax
js     11f8 <readNlSock+0xc8>
mov    %eax,%r12d
mov    0x2e61(%rip),%r14
jmp    11a1 <readNlSock+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%eax
add    %rax,%rbp
mov    $0x2000,%edx
sub    %ebx,%edx
mov    %r13d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
test   %eax,%eax
js     11f1 <readNlSock+0xc1>
mov    %rbp,%rdi
mov    %r12d,%esi
call   1040 <NLMSG_OK@plt>
test   %rax,%rax
je     11e8 <readNlSock+0xb8>
mov    0x8(%rbp),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
je     11e8 <readNlSock+0xb8>
cmp    (%r14),%rax
je     1202 <readNlSock+0xd2>
add    %r12d,%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
test   %eax,0x10(%rbp)
je     1202 <readNlSock+0xd2>
cmp    %r15d,0x0(%rbp)
jne    1180 <readNlSock+0x50>
mov    0x4(%rsp),%eax
cmp    %eax,0x4(%rbp)
jne    1180 <readNlSock+0x50>
jmp    1202 <readNlSock+0xd2>
lea    0xe1d(%rip),%rdi
jmp    11f8 <readNlSock+0xc8>
lea    0xe08(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32, i32, i64, i32 }
@.str = [12 x i8] c"SOCK READ: \00"
@NLMSG_ERROR = global i64 0
@.str.1 = [25 x i8] c"Error in recieved packet\00"
@NLMSG_DONE = global i64 0
@NLM_F_MULTI = global i32 0
define i32 @readNlSock(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.nlmsghdr*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
store i32 0, i32* %12
br label %13
13: ; preds = %73, %4
%14 = load i32, i32* %6
%15 = load i8*, i8** %7
%16 = load i32, i32* %12
%17 = sub nsw i32 8192, %16
%18 = call i32 @recv(i32 %14, i8* %15, i32 %17, i32 0)
store i32 %18, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
22: ; preds = %13
%23 = load i8*, i8** %7
%24 = bitcast i8* %23 to %struct.nlmsghdr*
store %struct.nlmsghdr* %24, %struct.nlmsghdr** %10
%25 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%26 = load i32, i32* %11
%27 = call i64 @NLMSG_OK(%struct.nlmsghdr* %25, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %22
%30 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%31 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %30, i32 0, i32 2
%32 = load i64, i64* %31
%33 = load i64, i64* @NLMSG_ERROR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29, %22
%36 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
37: ; preds = %29
%38 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%39 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %38, i32 0, i32 2
%40 = load i64, i64* %39
%41 = load i64, i64* @NLMSG_DONE
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %37
br label %75
44: ; preds = %37
%45 = load i32, i32* %11
%46 = load i8*, i8** %7
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %7
%49 = load i32, i32* %11
%50 = load i32, i32* %12
%51 = add nsw i32 %50, %49
store i32 %51, i32* %12
br label %52
52: ; preds = %44
%53 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%54 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %53, i32 0, i32 3
%55 = load i32, i32* %54
%56 = load i32, i32* @NLM_F_MULTI
%57 = and i32 %55, %56
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %52
br label %75
60: ; preds = %52
br label %61
61: ; preds = %60
%62 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%63 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32, i32* %8
%66 = icmp ne i32 %64, %65
br i1 %66, label %73, label %67
67: ; preds = %61
%68 = load %struct.nlmsghdr*, %struct.nlmsghdr** %10
%69 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = load i32, i32* %9
%72 = icmp ne i32 %70, %71
br label %73
73: ; preds = %67, %61
%74 = phi i1 [ true, %61 ], [ %72, %67 ]
br i1 %74, label %13, label %75
75: ; preds = %73, %59, %43
%76 = load i32, i32* %12
store i32 %76, i32* %5
br label %77
77: ; preds = %75, %35, %20
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32, i32, i64, i32 }
@.str = [12 x i8] c"SOCK READ: \00"
@NLMSG_ERROR = global i64 0
@.str.1 = [25 x i8] c"Error in recieved packet\00"
@NLMSG_DONE = global i64 0
@NLM_F_MULTI = global i32 0
define i32 @readNlSock(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @recv(i32 %0, i8* %1, i32 8192, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %46, label %7
7: ; preds = %4, %42
%8 = phi i32 [ %44, %42 ], [ %5, %4 ]
%9 = phi i32 [ %26, %42 ], [ 0, %4 ]
%10 = phi i8* [ %25, %42 ], [ %1, %4 ]
%11 = bitcast i8* %10 to %struct.nlmsghdr*
%12 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %11, i32 %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %46, label %14
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %10, i64 8
%16 = bitcast i8* %15 to i64*
%17 = load i64, i64* %16
%18 = load i64, i64* @NLMSG_ERROR
%19 = icmp eq i64 %17, %18
br i1 %19, label %46, label %20
20: ; preds = %14
%21 = load i64, i64* @NLMSG_DONE
%22 = icmp eq i64 %17, %21
br i1 %22, label %49, label %23
23: ; preds = %20
%24 = zext i32 %8 to i64
%25 = getelementptr inbounds i8, i8* %10, i64 %24
%26 = add nsw i32 %8, %9
%27 = getelementptr inbounds i8, i8* %10, i64 16
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = load i32, i32* @NLM_F_MULTI
%31 = and i32 %30, %29
%32 = icmp eq i32 %31, 0
br i1 %32, label %49, label %33
33: ; preds = %23
%34 = bitcast i8* %10 to i32*
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, %2
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = getelementptr inbounds i8, i8* %10, i64 4
%39 = bitcast i8* %38 to i32*
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, %3
br i1 %41, label %49, label %42
42: ; preds = %37, %33
%43 = sub nsw i32 8192, %26
%44 = tail call i32 @recv(i32 %0, i8* %25, i32 %43, i32 0)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %7
46: ; preds = %7, %14, %42, %4
%47 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %42 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %7 ]
%48 = tail call i32 @perror(i8* %47)
br label %49
49: ; preds = %37, %20, %23, %46
%50 = phi i32 [ -1, %46 ], [ %26, %37 ], [ %26, %23 ], [ %9, %20 ]
ret i32 %50
}