#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);
scalar_t__ EINTR;
int SCM_RIGHTS;
scalar_t__ errno;
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);
  while (((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR)
    ;
  if (rv == -1) {
    perror("recvmsg");
    return -1;
  }
  if (!rv) {
    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,%ecx
mov    %ecx,-0x50(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x75(%rbp)
jne    1210 <receive_fd+0xb0>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x75(%rbp)
mov    -0x75(%rbp),%al
test   $0x1,%al
jne    1220 <receive_fd+0xc0>
jmp    1225 <receive_fd+0xc5>
jmp    11d5 <receive_fd+0x75>
cmpl   $0xffffffff,-0x50(%rbp)
jne    124e <receive_fd+0xee>
lea    0xdca(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    12db <receive_fd+0x17b>
cmpl   $0x0,-0x50(%rbp)
jne    126b <receive_fd+0x10b>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    12db <receive_fd+0x17b>
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    0x2d40(%rip),%rcx
cmp    (%rcx),%eax
jne    12c6 <receive_fd+0x166>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    -0x70(%rbp),%rax
mov    (%rax),%edx
lea    0xd5a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    12db <receive_fd+0x17b>
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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r15
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
lea    -0x29(%rbp),%rcx
mov    %rcx,-0x40(%rbp)
movl   $0x1,-0x38(%rbp)
xorps  %xmm0,%xmm0
movups %xmm0,-0x50(%rbp)
lea    -0x40(%rbp),%rcx
mov    %rcx,-0x58(%rbp)
movl   $0x1,-0x70(%rbp)
mov    %rdx,-0x68(%rbp)
mov    %eax,-0x60(%rbp)
lea    -0x70(%rbp),%r14
mov    0x2e0e(%rip),%r12
mov    0x2df7(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0xffffffff,%eax
jne    11fb <receive_fd+0x9b>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     11e0 <receive_fd+0x80>
cmp    $0xffffffff,%eax
je     125b <receive_fd+0xfb>
mov    $0xffffffff,%ebx
test   %eax,%eax
je     1247 <receive_fd+0xe7>
lea    -0x70(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    (%rax),%edx
xor    %ecx,%ecx
test   %edx,%edx
sete   %cl
mov    0x2dae(%rip),%rsi
cmp    %ecx,(%rsi)
jne    123d <receive_fd+0xdd>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1247 <receive_fd+0xe7>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ebx
mov    %r15,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1247 <receive_fd+0xe7>

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i64 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.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
br label %27
27: ; preds = %37, %1
%28 = load i32, i32* %3
%29 = call i32 @recvmsg(i32 %28, %struct.msghdr* %4, i32 0)
store i32 %29, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br label %35
35: ; preds = %31, %27
%36 = phi i1 [ false, %27 ], [ %34, %31 ]
br i1 %36, label %37, label %38
37: ; preds = %35
br label %27
38: ; preds = %35
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = 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 %68
43: ; preds = %38
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %43
store i32 -1, i32* %2
store i32 1, i32* %12
br label %68
47: ; preds = %43
%48 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
store %struct.cmsghdr* %48, %struct.cmsghdr** %11
%49 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%50 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = icmp ne i32 %51, 0
%53 = xor i1 %52, true
%54 = zext i1 %53 to i32
%55 = load i32, i32* @SCM_RIGHTS
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %63
57: ; preds = %47
%58 = load i32, i32* @stderr
%59 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%60 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %61)
store i32 -1, i32* %2
store i32 1, i32* %12
br label %68
63: ; preds = %47
%64 = load %struct.cmsghdr*, %struct.cmsghdr** %11
%65 = call i64 @CMSG_DATA(%struct.cmsghdr* %64)
%66 = inttoptr i64 %65 to i32*
%67 = load i32, i32* %66
store i32 %67, i32* %2
store i32 1, i32* %12
br label %68
68: ; preds = %63, %57, %46, %41
%69 = load i8*, i8** %9
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i64 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.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
br label %20
20: ; preds = %20, %1
%21 = call i32 @recvmsg(i32 %0, %struct.msghdr* %2, i32 0)
%22 = icmp eq i32 %21, -1
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
%26 = select i1 %22, i1 %25, i1 false
br i1 %26, label %20, label %27
27: ; preds = %20
br i1 %22, label %28, label %30
28: ; preds = %27
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %47
30: ; preds = %27
%31 = icmp eq i32 %21, 0
br i1 %31, label %47, label %32
32: ; preds = %30
%33 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i64 0, i32 0
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
%38 = load i32, i32* @SCM_RIGHTS
%39 = icmp eq i32 %38, %37
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %47
43: ; preds = %32
%44 = call i64 @CMSG_DATA(%struct.cmsghdr* %33)
%45 = inttoptr i64 %44 to i32*
%46 = load i32, i32* %45
br label %47
47: ; preds = %30, %43, %40, %28
%48 = phi i32 [ -1, %28 ], [ -1, %40 ], [ %46, %43 ], [ -1, %30 ]
ret i32 %48
}