#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 */
typedef int /*<<< orphan*/ tmp;
struct msghdr {
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int sockfd) {
  ssize_t rv;
  char tmp[((((unsigned int)(sizeof(struct cmsghdr)) + (sizeof(int) - 1)) &
             ~(sizeof(int) - 1)) +
            (((unsigned int)(sizeof(int)) + (sizeof(int) - 1)) &
             ~(sizeof(int) - 1)))];
  struct cmsghdr *cmsg;
  struct iovec iov;
  struct msghdr msg;
  char ch = '\0';
  memset(&msg, 0, sizeof(msg));
  iov.iov_base = &ch;
  iov.iov_len = 1;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = tmp;
  msg.msg_controllen = sizeof(tmp);
  rv = recvmsg(sockfd, &msg, 0);
  if (rv <= 0) {
    return -1;
  }
  cmsg = CMSG_FIRSTHDR(&msg);
  return *(int *)CMSG_DATA(cmsg);
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0x51(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    -0x51(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x28(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x8,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     11b5 <recv_fd+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <recv_fd+0x90>
lea    -0x50(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
movb   $0x0,0xf(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    0xf(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x1,0x38(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x1,0x10(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x8,0x20(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
test   %rax,%rax
jle    11bf <recv_fd+0x7f>
lea    0x10(%rsp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
jmp    11c4 <recv_fd+0x84>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

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

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