#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 {
  unsigned int nlmsg_seq;
  unsigned 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 *, unsigned int);
int NLM_F_MULTI;
int /*<<< orphan*/ perror(char *);
int recv(int, char *, size_t, int /*<<< orphan*/);
int readNlSock(int sockFd, char *bufPtr, size_t buf_size, unsigned int seqNum,
               unsigned int pId) {
  struct nlmsghdr *nlHdr;
  int readLen = 0, msgLen = 0;
  do {
    if ((readLen = recv(sockFd, bufPtr, buf_size - msgLen, 0)) < 0) {
      perror("SOCK READ: ");
      return -1;
    }
    nlHdr = (struct nlmsghdr *)bufPtr;
    if ((NLMSG_OK(nlHdr, (unsigned)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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
movslq -0x30(%rbp),%rax
sub    %rax,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1195 <readNlSock+0x65>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <readNlSock+0x142>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <NLMSG_OK@plt>
cmp    $0x0,%rax
je     11cb <readNlSock+0x9b>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <readNlSock+0xb3>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <readNlSock+0x142>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <readNlSock+0xd0>
jmp    126c <readNlSock+0x13c>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%eax
mov    0x2db8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1238 <readNlSock+0x108>
jmp    126c <readNlSock+0x13c>
jmp    123d <readNlSock+0x10d>
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
mov    $0x1,%al
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x31(%rbp)
jne    1261 <readNlSock+0x131>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
cmp    -0x20(%rbp),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1158 <readNlSock+0x28>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readNlSock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,0x4(%rsp)
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %edi,%r12d
xor    %ebx,%ebx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xea4(%rip),%rdi
test   %eax,%eax
js     11f1 <readNlSock+0xc1>
mov    %eax,%r14d
jmp    1193 <readNlSock+0x63>
nopl   0x0(%rax)
mov    %r14d,%eax
add    %rax,%rbp
movslq %ebx,%rax
mov    %r13,%rdx
sub    %rax,%rdx
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11ea <readNlSock+0xba>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <NLMSG_OK@plt>
test   %rax,%rax
je     11e1 <readNlSock+0xb1>
mov    0x8(%rbp),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
je     11e1 <readNlSock+0xb1>
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
je     11fb <readNlSock+0xcb>
add    %r14d,%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
test   %eax,0x10(%rbp)
je     11fb <readNlSock+0xcb>
cmp    %r15d,0x0(%rbp)
jne    1170 <readNlSock+0x40>
mov    0x4(%rsp),%eax
cmp    %eax,0x4(%rbp)
jne    1170 <readNlSock+0x40>
jmp    11fb <readNlSock+0xcb>
lea    0xe24(%rip),%rdi
jmp    11f1 <readNlSock+0xc1>
lea    0xe0f(%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, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.nlmsghdr*
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %13
store i32 0, i32* %14
br label %15
15: ; preds = %77, %5
%16 = load i32, i32* %7
%17 = load i8*, i8** %8
%18 = load i64, i64* %9
%19 = load i32, i32* %14
%20 = sext i32 %19 to i64
%21 = sub i64 %18, %20
%22 = call i32 @recv(i32 %16, i8* %17, i64 %21, i32 0)
store i32 %22, i32* %13
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %81
26: ; preds = %15
%27 = load i8*, i8** %8
%28 = bitcast i8* %27 to %struct.nlmsghdr*
store %struct.nlmsghdr* %28, %struct.nlmsghdr** %12
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%30 = load i32, i32* %13
%31 = call i64 @NLMSG_OK(%struct.nlmsghdr* %29, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %26
%34 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%35 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %34, i32 0, i32 2
%36 = load i64, i64* %35
%37 = load i64, i64* @NLMSG_ERROR
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %33, %26
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %81
41: ; preds = %33
%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* @NLMSG_DONE
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %41
br label %79
48: ; preds = %41
%49 = load i32, i32* %13
%50 = load i8*, i8** %8
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %8
%53 = load i32, i32* %13
%54 = load i32, i32* %14
%55 = add nsw i32 %54, %53
store i32 %55, i32* %14
br label %56
56: ; preds = %48
%57 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%58 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %57, i32 0, i32 3
%59 = load i32, i32* %58
%60 = load i32, i32* @NLM_F_MULTI
%61 = and i32 %59, %60
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %56
br label %79
64: ; preds = %56
br label %65
65: ; preds = %64
%66 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%67 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* %10
%70 = icmp ne i32 %68, %69
br i1 %70, label %77, label %71
71: ; preds = %65
%72 = load %struct.nlmsghdr*, %struct.nlmsghdr** %12
%73 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %72, i32 0, i32 1
%74 = load i32, i32* %73
%75 = load i32, i32* %11
%76 = icmp ne i32 %74, %75
br label %77
77: ; preds = %71, %65
%78 = phi i1 [ true, %65 ], [ %76, %71 ]
br i1 %78, label %15, label %79
79: ; preds = %77, %63, %47
%80 = load i32, i32* %14
store i32 %80, i32* %6
br label %81
81: ; preds = %79, %39, %24
%82 = load i32, i32* %6
ret i32 %82
}

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, i64 %2, i32 %3, i32 %4) {
%6 = tail call i32 @recv(i32 %0, i8* %1, i64 %2, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %48, label %8
8: ; preds = %5, %43
%9 = phi i32 [ %46, %43 ], [ %6, %5 ]
%10 = phi i32 [ %27, %43 ], [ 0, %5 ]
%11 = phi i8* [ %26, %43 ], [ %1, %5 ]
%12 = bitcast i8* %11 to %struct.nlmsghdr*
%13 = tail call i64 @NLMSG_OK(%struct.nlmsghdr* %12, i32 %9)
%14 = icmp eq i64 %13, 0
br i1 %14, label %48, label %15
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %11, i64 8
%17 = bitcast i8* %16 to i64*
%18 = load i64, i64* %17
%19 = load i64, i64* @NLMSG_ERROR
%20 = icmp eq i64 %18, %19
br i1 %20, label %48, label %21
21: ; preds = %15
%22 = load i64, i64* @NLMSG_DONE
%23 = icmp eq i64 %18, %22
br i1 %23, label %51, label %24
24: ; preds = %21
%25 = zext i32 %9 to i64
%26 = getelementptr inbounds i8, i8* %11, i64 %25
%27 = add nsw i32 %9, %10
%28 = getelementptr inbounds i8, i8* %11, i64 16
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = load i32, i32* @NLM_F_MULTI
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %51, label %34
34: ; preds = %24
%35 = bitcast i8* %11 to i32*
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, %3
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = getelementptr inbounds i8, i8* %11, i64 4
%40 = bitcast i8* %39 to i32*
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, %4
br i1 %42, label %51, label %43
43: ; preds = %38, %34
%44 = sext i32 %27 to i64
%45 = sub i64 %2, %44
%46 = tail call i32 @recv(i32 %0, i8* %26, i64 %45, i32 0)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %8
48: ; preds = %8, %15, %43, %5
%49 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %43 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %15 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %8 ]
%50 = tail call i32 @perror(i8* %49)
br label %51
51: ; preds = %38, %21, %24, %48
%52 = phi i32 [ -1, %48 ], [ %27, %38 ], [ %27, %24 ], [ %10, %21 ]
ret i32 %52
}