#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;
  char *msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
  scalar_t__ msg_namelen;
  scalar_t__ msg_name;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int cmsg_type;
};
typedef int /*<<< orphan*/ connfd;
typedef int /*<<< orphan*/ ccmsg;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int SCM_RIGHTS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int receive_fd(int fd) {
  struct msghdr msg;
  struct iovec iov;
  char buf[1];
  int rv;
  int connfd = -1;
  char ccmsg[CMSG_SPACE(sizeof(connfd))];
  struct cmsghdr *cmsg;
  iov.iov_base = buf;
  iov.iov_len = 1;
  msg.msg_name = 0;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = ccmsg;
  msg.msg_controllen = sizeof(ccmsg);
  rv = recvmsg(fd, &msg, 0);
  if (rv == -1) {
    perror("recvmsg");
    return -1;
  }
  cmsg = CMSG_FIRSTHDR(&msg);
  if (!cmsg->cmsg_type == SCM_RIGHTS) {
    fprintf(stderr, "got control message of unknown type %d\n",
            cmsg->cmsg_type);
    return -1;
  }
  return *(int *)CMSG_DATA(cmsg);
}


x86 assembly (clang, -O0):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x54(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
lea    -0x49(%rbp),%rdx
mov    %rdx,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x48(%rbp),%rdx
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x38(%rbp)
mov    %rcx,-0x30(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    120f <receive_fd+0xaf>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    127f <receive_fd+0x11f>
lea    -0x38(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
cmpl   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%eax
jne    126a <receive_fd+0x10a>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
mov    (%rax),%edx
lea    0xdb6(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    127f <receive_fd+0x11f>
mov    -0x70(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %edi,%ebx
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r14
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
lea    -0x11(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
xorps  %xmm0,%xmm0
movups %xmm0,-0x38(%rbp)
lea    -0x28(%rbp),%rcx
mov    %rcx,-0x40(%rbp)
movl   $0x1,-0x58(%rbp)
mov    %rdx,-0x50(%rbp)
mov    %eax,-0x48(%rbp)
lea    -0x58(%rbp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0xffffffff,%eax
je     121a <receive_fd+0xba>
lea    -0x58(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    (%rax),%edx
xor    %ecx,%ecx
test   %edx,%edx
sete   %cl
mov    0x2df4(%rip),%rsi
cmp    %ecx,(%rsi)
jne    1204 <receive_fd+0xa4>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    120e <receive_fd+0xae>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %r14,%rsp
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    120e <receive_fd+0xae>

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i64 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@.str = [8 x i8] c"recvmsg\00"
@SCM_RIGHTS = global i32 0
@stderr = global i32 0
@.str.1 = [40 x i8] c"got control message of unknown type %d\0A\00"
define i32 @receive_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = alloca [1 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.cmsghdr*
%12 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %8
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 5
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 4
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 3
store %struct.iovec* %5, %struct.iovec** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 0
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 1
store i8* %16, i8** %24
%25 = trunc i64 %14 to i32
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %3
%28 = call i32 @recvmsg(i32 %27, %struct.msghdr* %4, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %1
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
store i32 1, i32* %12
br label %54
33: ; preds = %1
%34 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
store %struct.cmsghdr* %34, %struct.cmsghdr** %11
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
%40 = zext i1 %39 to i32
%41 = load i32, i32* @SCM_RIGHTS
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %47)
store i32 -1, i32* %2
store i32 1, i32* %12
br label %54
49: ; preds = %33
%50 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%51 = call i64 @CMSG_DATA(%struct.cmsghdr* %50)
%52 = inttoptr i64 %51 to i32*
%53 = load i32, i32* %52
store i32 %53, i32* %2
store i32 1, i32* %12
br label %54
54: ; preds = %49, %43, %31
%55 = load i8*, i8** %9
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i64 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@.str = [8 x i8] c"recvmsg\00"
@SCM_RIGHTS = global i32 0
@stderr = global i32 0
@.str.1 = [40 x i8] c"got control message of unknown type %d\0A\00"
define i32 @receive_fd(i32 %0) {
%2 = alloca %struct.msghdr
%3 = alloca %struct.iovec
%4 = alloca [1 x i8]
%5 = bitcast %struct.msghdr* %2 to i8*
%6 = bitcast %struct.iovec* %3 to i8*
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%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 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
store i8* %7, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
store i32 1, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 4
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 3
%16 = bitcast i64* %14 to i8*
store %struct.iovec* %3, %struct.iovec** %15
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 0
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 1
store i8* %11, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 2
store i32 %8, i32* %19
%20 = call i32 @recvmsg(i32 %0, %struct.msghdr* %2, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %39
24: ; preds = %1
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
%30 = load i32, i32* @SCM_RIGHTS
%31 = icmp eq i32 %30, %29
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %39
35: ; preds = %24
%36 = call i64 @CMSG_DATA(%struct.cmsghdr* %25)
%37 = inttoptr i64 %36 to i32*
%38 = load i32, i32* %37
br label %39
39: ; preds = %35, %32, %22
%40 = phi i32 [ -1, %22 ], [ -1, %32 ], [ %38, %35 ]
ret i32 %40
}