#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 msghdr {
  char *msg_control;
  int msg_controllen;
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  scalar_t__ *iov_base;
  int iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
  scalar_t__ cmsg_len;
};
typedef int /*<<< orphan*/ msg;
typedef scalar_t__ int32_t;
typedef int /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ err;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
scalar_t__ CMSG_LEN(int);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ EINVAL;
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int vrf_popfd(int sock) {
  int32_t err;
  int fd;
  char buf[CMSG_SPACE(sizeof(fd))];
  struct msghdr msg;
  struct iovec iov;
  struct cmsghdr *cmsg;
  err = 0;
  iov.iov_base = &err;
  iov.iov_len = sizeof(err);
  memset(&msg, 0, sizeof(msg));
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  if (recvmsg(sock, &msg, 0) < 0)
    return -1;
  if (err != 0) {
    errno = err;
    return -1;
  }
  fd = -1;
  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
      continue;
    if (cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
      errno = EINVAL;
      return -1;
    }
    memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
  }
  if (fd == -1)
    errno = EINVAL;
  return fd;
}


x86 assembly (clang, -O0):
<vrf_popfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x8,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    %rcx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1090 <recvmsg@plt>
cmp    $0x0,%rax
jge    1231 <vrf_popfd+0xb1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1357 <vrf_popfd+0x1d7>
cmpq   $0x0,-0x10(%rbp)
je     125d <vrf_popfd+0xdd>
mov    -0x10(%rbp),%rcx
mov    0x2d91(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1357 <vrf_popfd+0x1d7>
movl   $0xffffffff,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     132c <vrf_popfd+0x1ac>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
jne    12ab <vrf_popfd+0x12b>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
je     12b0 <vrf_popfd+0x130>
jmp    1316 <vrf_popfd+0x196>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x78(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
cmp    %rcx,%rax
je     12fd <vrf_popfd+0x17d>
mov    0x2ceb(%rip),%rax
mov    (%rax),%rcx
mov    0x2cf1(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    1357 <vrf_popfd+0x1d7>
mov    -0x58(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%esi
lea    -0x14(%rbp),%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x58(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1080 <CMSG_NXTHDR@plt>
mov    %rax,-0x58(%rbp)
jmp    1271 <vrf_popfd+0xf1>
cmpl   $0xffffffff,-0x14(%rbp)
jne    134a <vrf_popfd+0x1ca>
mov    0x2c8b(%rip),%rax
mov    (%rax),%rcx
mov    0x2c91(%rip),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vrf_popfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%r14d
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%r13d
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movq   $0x0,-0x38(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x8,-0x60(%rbp)
lea    -0x50(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %rbx,-0x50(%rbp)
mov    %r13d,-0x48(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1090 <recvmsg@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
js     12c8 <vrf_popfd+0x148>
mov    -0x38(%rbp),%rax
test   %rax,%rax
jne    12b9 <vrf_popfd+0x139>
movl   $0xffffffff,-0x2c(%rbp)
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     12af <vrf_popfd+0x12f>
mov    %rax,%rbx
mov    0x2da6(%rip),%r13
mov    0x2d87(%rip),%r14
lea    -0x50(%rbp),%r15
jmp    1263 <vrf_popfd+0xe3>
nop
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12a7 <vrf_popfd+0x127>
mov    (%rbx),%rax
cmp    0x0(%r13),%rax
jne    1250 <vrf_popfd+0xd0>
mov    0x8(%rbx),%rax
cmp    (%r14),%rax
jne    1250 <vrf_popfd+0xd0>
mov    0x10(%rbx),%rax
mov    %rax,-0x58(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,-0x58(%rbp)
jne    12af <vrf_popfd+0x12f>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
lea    -0x2c(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    1250 <vrf_popfd+0xd0>
mov    -0x2c(%rbp),%eax
cmp    $0xffffffff,%eax
jne    12c8 <vrf_popfd+0x148>
mov    0x2d12(%rip),%rax
mov    (%rax),%rax
mov    0x2d18(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i64*, i32 }
%struct.cmsghdr = type { i64, i64, i64 }
@errno = global i64 0
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@EINVAL = global i64 0
define i32 @vrf_popfd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.msghdr
%9 = alloca %struct.iovec
%10 = alloca %struct.cmsghdr*
%11 = alloca i32
store i32 %0, i32* %3
%12 = call i32 @CMSG_SPACE(i32 4)
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store i64 0, i64* %4
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
store i64* %4, i64** %16
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
store i32 8, i32* %17
%18 = call i32 @memset(%struct.msghdr* %8, i32 0, i32 24)
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i8* %15, i8** %19
%20 = trunc i64 %13 to i32
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %9, %struct.iovec** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 1, i32* %23
%24 = load i32, i32* %3
%25 = call i64 @recvmsg(i32 %24, %struct.msghdr* %8, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %11
br label %73
28: ; preds = %1
%29 = load i64, i64* %4
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = load i64, i64* %4
store i64 %32, i64* @errno
store i32 -1, i32* %2
store i32 1, i32* %11
br label %73
33: ; preds = %28
store i32 -1, i32* %5
%34 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %34, %struct.cmsghdr** %10
br label %35
35: ; preds = %63, %33
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%37 = icmp ne %struct.cmsghdr* %36, null
br i1 %37, label %38, label %66
38: ; preds = %35
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @SOL_SOCKET
%43 = icmp ne i64 %41, %42
br i1 %43, label %50, label %44
44: ; preds = %38
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = load i64, i64* @SCM_RIGHTS
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %44, %38
br label %63
51: ; preds = %44
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%53 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %52, i32 0, i32 2
%54 = load i64, i64* %53
%55 = call i64 @CMSG_LEN(i32 4)
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = load i64, i64* @EINVAL
store i64 %58, i64* @errno
store i32 -1, i32* %2
store i32 1, i32* %11
br label %73
59: ; preds = %51
%60 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%61 = call i32 @CMSG_DATA(%struct.cmsghdr* %60)
%62 = call i32 @memcpy(i32* %5, i32 %61, i32 4)
br label %63
63: ; preds = %59, %50
%64 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%65 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %8, %struct.cmsghdr* %64)
store %struct.cmsghdr* %65, %struct.cmsghdr** %10
br label %35
66: ; preds = %35
%67 = load i32, i32* %5
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = load i64, i64* @EINVAL
store i64 %70, i64* @errno
br label %71
71: ; preds = %69, %66
%72 = load i32, i32* %5
store i32 %72, i32* %2
store i32 1, i32* %11
br label %73
73: ; preds = %71, %57, %31, %27
%74 = load i8*, i8** %6
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i64*, i32 }
%struct.cmsghdr = type { i64, i64, i64 }
@errno = global i64 0
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@EINVAL = global i64 0
define i32 @vrf_popfd(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = bitcast i64* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = tail call i32 @CMSG_SPACE(i32 4)
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast %struct.msghdr* %4 to i8*
%13 = bitcast %struct.iovec* %5 to i8*
store i64 0, i64* %2
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i64* %2, i64** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 8, i32* %15
%16 = call i32 @memset(%struct.msghdr* %4, i32 0, i32 24)
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i8* %11, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i32 %8, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 1, i32* %20
%21 = call i64 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %60, label %23
23: ; preds = %1
%24 = load i64, i64* %2
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %58
26: ; preds = %23
store i32 -1, i32* %3
%27 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%28 = icmp eq %struct.cmsghdr* %27, null
br i1 %28, label %56, label %29
29: ; preds = %26, %50
%30 = phi %struct.cmsghdr* [ %51, %50 ], [ %27, %26 ]
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i64 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @SOL_SOCKET
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %29
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i64 0, i32 1
%37 = load i64, i64* %36
%38 = load i64, i64* @SCM_RIGHTS
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %50
40: ; preds = %35
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i64 0, i32 2
%42 = load i64, i64* %41
%43 = call i64 @CMSG_LEN(i32 4)
%44 = icmp eq i64 %42, %43
br i1 %44, label %47, label %45
45: ; preds = %40
%46 = load i64, i64* @EINVAL
br label %58
47: ; preds = %40
%48 = call i32 @CMSG_DATA(%struct.cmsghdr* %30)
%49 = call i32 @memcpy(i32* %3, i32 %48, i32 4)
br label %50
50: ; preds = %29, %35, %47
%51 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %30)
%52 = icmp eq %struct.cmsghdr* %51, null
br i1 %52, label %53, label %29
53: ; preds = %50
%54 = load i32, i32* %3
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %26, %53
%57 = load i64, i64* @EINVAL
br label %58
58: ; preds = %23, %45, %56
%59 = phi i64 [ %57, %56 ], [ %46, %45 ], [ %24, %23 ]
store i64 %59, i64* @errno
br label %60
60: ; preds = %58, %53, %1
%61 = phi i32 [ -1, %1 ], [ %54, %53 ], [ -1, %58 ]
ret i32 %61
}