#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 {
  int msg_iovlen;
  int msg_controllen;
  scalar_t__ msg_control;
  struct iovec *msg_iov;
  scalar_t__ msg_namelen;
  scalar_t__ msg_name;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memmove(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int sock) {
  int n;
  int fd;
  char buf[1];
  struct iovec iov;
  struct msghdr msg;
  struct cmsghdr *cmsg;
  char cms[CMSG_SPACE(sizeof(int))];
  iov.iov_base = buf;
  iov.iov_len = 1;
  memset(&msg, 0, sizeof msg);
  msg.msg_name = 0;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = (caddr_t)cms;
  msg.msg_controllen = sizeof cms;
  if ((n = recvmsg(sock, &msg, 0)) < 0)
    return -1;
  if (n == 0)
    return -1;
  cmsg = CMSG_FIRSTHDR(&msg);
  memmove(&fd, CMSG_DATA(cmsg), sizeof(int));
  close(sock);
  return fd;
}


x86 assembly (clang, -O0):
<recv_fd>:
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,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
lea    -0x11(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    -0x80(%rbp),%rcx
mov    -0x78(%rbp),%rax
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x28(%rbp),%rdx
mov    %rdx,-0x40(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    122b <recv_fd+0xbb>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1283 <recv_fd+0x113>
cmpl   $0x0,-0xc(%rbp)
jne    1248 <recv_fd+0xd8>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1283 <recv_fd+0x113>
lea    -0x50(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%esi
lea    -0x10(%rbp),%rdi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
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
lea    -0x29(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x38(%rbp)
lea    -0x68(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x68(%rbp)
mov    %rbx,-0x60(%rbp)
mov    %r13d,-0x64(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
test   %eax,%eax
jle    1226 <recv_fd+0xb6>
lea    -0x68(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
lea    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
jmp    122b <recv_fd+0xbb>
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.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i64, %struct.iovec*, i64, i64 }
%struct.cmsghdr = type { i32 }
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x i8]
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %3
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 1, i32* %19
%20 = call i32 @memset(%struct.msghdr* %8, i32 0, i32 40)
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 5
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %7, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %24
%25 = ptrtoint i8* %16 to i64
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i64 %25, i64* %26
%27 = trunc i64 %14 to i32
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %3
%30 = call i32 @recvmsg(i32 %29, %struct.msghdr* %8, i32 0)
store i32 %30, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %12
br label %45
33: ; preds = %1
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -1, i32* %2
store i32 1, i32* %12
br label %45
37: ; preds = %33
%38 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %38, %struct.cmsghdr** %9
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%40 = call i32 @CMSG_DATA(%struct.cmsghdr* %39)
%41 = call i32 @memmove(i32* %5, i32 %40, i32 4)
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %5
store i32 %44, i32* %2
store i32 1, i32* %12
br label %45
45: ; preds = %37, %36, %32
%46 = load i8*, i8** %10
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i64, %struct.iovec*, i64, i64 }
%struct.cmsghdr = type { i32 }
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca [1 x i8]
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = bitcast i32* %2 to i8*
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.iovec* %4 to i8*
%9 = bitcast %struct.msghdr* %5 to i8*
%10 = tail call i32 @CMSG_SPACE(i32 4)
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %7, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %15
%16 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 40)
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
%19 = bitcast i64* %17 to i8*
store %struct.iovec* %4, %struct.iovec** %18
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %20
%21 = ptrtoint i8* %13 to i64
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i64 %21, i64* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 %10, i32* %23
%24 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%25 = icmp slt i32 %24, 1
br i1 %25, label %32, label %26
26: ; preds = %1
%27 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%28 = call i32 @CMSG_DATA(%struct.cmsghdr* %27)
%29 = call i32 @memmove(i32* %2, i32 %28, i32 4)
%30 = call i32 @close(i32 %0)
%31 = load i32, i32* %2
br label %32
32: ; preds = %1, %26
%33 = phi i32 [ %31, %26 ], [ -1, %1 ]
ret i32 %33
}