#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 msghdr {
  int msg_namelen;
  int msg_iovlen;
  int msg_flags;
  scalar_t__ msg_controllen;
  int /*<<< orphan*/ *msg_control;
  struct iovec *msg_iov;
  void *msg_name;
};
struct iovec {
  void *member_0;
  size_t member_1;
};
typedef int /*<<< orphan*/ nladdr;

/* Variables and functions */
scalar_t__ EINTR;
int MSG_TRUNC;
scalar_t__ errno;
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int nl_recvmsg(int sd, int request, int seq, void *buf, size_t buflen,
               int *flags) {
  struct msghdr msg;
  struct iovec iov = {buf, buflen};
  struct sockaddr_nl nladdr;
  int read_len;
  for (;;) {
    msg.msg_name = (void *)&nladdr;
    msg.msg_namelen = sizeof(nladdr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
    msg.msg_flags = 0;
    read_len = recvmsg(sd, &msg, 0);
    if ((read_len < 0 && errno == EINTR) || (msg.msg_flags & MSG_TRUNC))
      continue;
    if (flags)
      *flags = msg.msg_flags;
    break;
  }
  return read_len;
}


x86 assembly (clang, -O0):
<nl_recvmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x58(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x54(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x58(%rbp),%rsi
xor    %edx,%edx
call   1030 <recvmsg@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    11aa <nl_recvmsg+0x9a>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <nl_recvmsg+0xaf>
mov    -0x50(%rbp),%eax
mov    0x2e34(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c4 <nl_recvmsg+0xb4>
jmp    1140 <nl_recvmsg+0x30>
cmpq   $0x0,-0x28(%rbp)
je     11d8 <nl_recvmsg+0xc8>
mov    -0x50(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11dd <nl_recvmsg+0xcd>
mov    -0x74(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nl_recvmsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,0x8(%rsp)
mov    %edi,%ebp
mov    %rcx,0x18(%rsp)
mov    %r8,0x20(%rsp)
lea    0x38(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x18(%rsp),%r13
lea    0x28(%rsp),%rbx
mov    0x2e9e(%rip),%r14
jmp    1159 <nl_recvmsg+0x49>
nopl   0x0(%rax)
mov    0x30(%rsp),%ecx
test   %ecx,(%r14)
je     11a3 <nl_recvmsg+0x93>
mov    %r12,0x50(%rsp)
movl   $0x4,0x28(%rsp)
mov    %r13,0x48(%rsp)
movq   $0x1,0x2c(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,(%r15)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <recvmsg@plt>
test   %eax,%eax
jns    1150 <nl_recvmsg+0x40>
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e34(%rip),%rdx
cmp    (%rdx),%rcx
je     1159 <nl_recvmsg+0x49>
jmp    1150 <nl_recvmsg+0x40>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     11af <nl_recvmsg+0x9f>
mov    %ecx,(%rdx)
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i32, i32, i64, i32*, %struct.iovec*, i8* }
%struct.iovec = type { i8*, i64 }
%struct.sockaddr_nl = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@MSG_TRUNC = global i32 0
define i32 @nl_recvmsg(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32*
%13 = alloca %struct.msghdr
%14 = alloca %struct.iovec
%15 = alloca %struct.sockaddr_nl
%16 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
store i32* %5, i32** %12
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 0
%18 = load i8*, i8** %10
store i8* %18, i8** %17
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 1
%20 = load i64, i64* %11
store i64 %20, i64* %19
br label %21
21: ; preds = %44, %6
%22 = bitcast %struct.sockaddr_nl* %15 to i8*
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 6
store i8* %22, i8** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 0
store i32 4, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 5
store %struct.iovec* %14, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 4
store i32* null, i32** %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i32 0, i32* %29
%30 = load i32, i32* %7
%31 = call i32 @recvmsg(i32 %30, %struct.msghdr* %13, i32 0)
store i32 %31, i32* %16
%32 = load i32, i32* %16
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %21
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %44, label %38
38: ; preds = %34, %21
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load i32, i32* @MSG_TRUNC
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38, %34
br label %21
45: ; preds = %38
%46 = load i32*, i32** %12
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
%50 = load i32, i32* %49
%51 = load i32*, i32** %12
store i32 %50, i32* %51
br label %52
52: ; preds = %48, %45
br label %53
53: ; preds = %52
%54 = load i32, i32* %16
ret i32 %54
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i32, i32, i64, i32*, %struct.iovec*, i8* }
%struct.iovec = type { i8*, i64 }
%struct.sockaddr_nl = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@MSG_TRUNC = global i32 0
define i32 @nl_recvmsg(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4, i32* writeonly %5) {
%7 = alloca %struct.msghdr
%8 = alloca %struct.iovec
%9 = alloca %struct.sockaddr_nl
%10 = bitcast %struct.msghdr* %7 to i8*
%11 = bitcast %struct.iovec* %8 to i8*
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i64 0, i32 0
store i8* %3, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i64 0, i32 1
store i64 %4, i64* %13
%14 = bitcast %struct.sockaddr_nl* %9 to i8*
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 6
%16 = bitcast i8** %15 to %struct.sockaddr_nl**
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 5
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
%22 = bitcast i64* %20 to i8*
br label %23
23: ; preds = %35, %6
store %struct.sockaddr_nl* %9, %struct.sockaddr_nl** %16
store i32 4, i32* %17
store %struct.iovec* %8, %struct.iovec** %18
store i32 1, i32* %19
store i32 0, i32* %21
%24 = call i32 @recvmsg(i32 %0, %struct.msghdr* %7, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %35, label %30
30: ; preds = %26, %23
%31 = load i32, i32* %21
%32 = load i32, i32* @MSG_TRUNC
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %30, %26
br label %23
36: ; preds = %30
%37 = icmp eq i32* %5, null
br i1 %37, label %39, label %38
38: ; preds = %36
store i32 %31, i32* %5
br label %39
39: ; preds = %36, %38
ret i32 %24
}