#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;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ ctl_buf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ make_cloexec(int);
int /*<<< orphan*/ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int chan) {
  char buf[1] = {0};
  struct iovec data = {.iov_base = buf, .iov_len = 1};
  struct msghdr msg = {0};
  msg.msg_iov = &data;
  msg.msg_iovlen = 1;
  char ctl_buf[CMSG_SPACE(sizeof(int))];
  msg.msg_control = ctl_buf;
  msg.msg_controllen = sizeof(ctl_buf);
  ssize_t recv_len = check(recvmsg(chan, &msg, 0), "recvmsg(fd)");
  for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
      int fd = *(int *)CMSG_DATA(cmsg);
      make_cloexec(fd);
      return fd;
    }
  }
  err(1, "no fd received");
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
lea    -0x9(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,%edi
lea    0xdf4(%rip),%rsi
call   1080 <check@plt>
mov    %eax,-0x64(%rbp)
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     129e <recv_fd+0x11e>
mov    -0x70(%rbp),%rax
mov    (%rax),%rax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <recv_fd+0x103>
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1283 <recv_fd+0x103>
mov    -0x70(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%edi
call   1090 <make_cloexec@plt>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    12b6 <recv_fd+0x136>
jmp    1288 <recv_fd+0x108>
mov    -0x70(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1060 <CMSG_NXTHDR@plt>
mov    %rax,-0x70(%rbp)
jmp    1221 <recv_fd+0xa1>
mov    $0x1,%edi
lea    0xd62(%rip),%rsi
call   1050 <err@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x58(%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   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
movb   $0x0,-0x21(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x1,-0x58(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
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
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
lea    0xdfa(%rip),%rsi
mov    %eax,%edi
call   1080 <check@plt>
mov    %rbx,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1265 <recv_fd+0xe5>
mov    0x2dc7(%rip),%r14
mov    0x2db0(%rip),%r12
lea    -0x50(%rbp),%rbx
jmp    1240 <recv_fd+0xc0>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     1265 <recv_fd+0xe5>
mov    (%rax),%rcx
cmp    (%r14),%rcx
jne    1230 <recv_fd+0xb0>
mov    0x8(%rax),%rcx
cmp    (%r12),%rcx
jne    1230 <recv_fd+0xb0>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <make_cloexec@plt>
jmp    1276 <recv_fd+0xf6>
lea    0xda0(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    %r15,%rsp
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i64, i64 }
@.str = [12 x i8] c"recvmsg(fd)\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str.1 = [15 x i8] c"no fd received\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x i8]
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.cmsghdr*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
%13 = bitcast [1 x i8]* %4 to i8*
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
%15 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
store i8* %15, i8** %14
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i32 1, i32* %16
%17 = bitcast %struct.msghdr* %6 to i8*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store %struct.iovec* %5, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i32 1, i32* %19
%20 = call i32 @CMSG_SPACE(i32 4)
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %7
%23 = alloca i8, i64 %21
store i64 %21, i64* %8
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i8* %23, i8** %24
%25 = trunc i64 %21 to i32
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* %3
%28 = call i32 @recvmsg(i32 %27, %struct.msghdr* %6, i32 0)
%29 = call i32 @check(i32 %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %29, i32* %9
%30 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
store %struct.cmsghdr* %30, %struct.cmsghdr** %10
br label %31
31: ; preds = %55, %1
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%33 = icmp ne %struct.cmsghdr* %32, null
br i1 %33, label %34, label %58
34: ; preds = %31
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @SOL_SOCKET
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %54
40: ; preds = %34
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
%43 = load i64, i64* %42
%44 = load i64, i64* @SCM_RIGHTS
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %54
46: ; preds = %40
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%48 = call i64 @CMSG_DATA(%struct.cmsghdr* %47)
%49 = inttoptr i64 %48 to i32*
%50 = load i32, i32* %49
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = call i32 @make_cloexec(i32 %51)
%53 = load i32, i32* %11
store i32 %53, i32* %2
store i32 1, i32* %12
br label %60
54: ; preds = %40, %34
br label %55
55: ; preds = %54
%56 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%57 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %6, %struct.cmsghdr* %56)
store %struct.cmsghdr* %57, %struct.cmsghdr** %10
br label %31
58: ; preds = %31
%59 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %12
br label %60
60: ; preds = %58, %46
%61 = load i8*, i8** %7
%62 = load i32, i32* %12
switch i32 %62, label %65 [
i32 0, label %63
i32 1, label %63
]
63: ; preds = %60, %60
%64 = load i32, i32* %2
ret i32 %64
65: ; preds = %60
unreachable
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i64, i64 }
@.str = [12 x i8] c"recvmsg(fd)\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str.1 = [15 x i8] c"no fd received\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca [1 x i8]
%3 = alloca %struct.iovec
%4 = alloca %struct.msghdr
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
store i8 0, i8* %5
%6 = bitcast %struct.iovec* %3 to i8*
%7 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
store i8* %5, i8** %7
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
store i32 1, i32* %8
%9 = bitcast %struct.msghdr* %4 to i8*
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store %struct.iovec* %3, %struct.iovec** %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i32 1, i32* %11
%12 = call i32 @CMSG_SPACE(i32 4)
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
%15 = alloca i8, i64 %13
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i8* %15, i8** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 %12, i32* %17
%18 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%19 = call i32 @check(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%21 = icmp eq %struct.cmsghdr* %20, null
br i1 %21, label %41, label %22
22: ; preds = %1, %33
%23 = phi %struct.cmsghdr* [ %34, %33 ], [ %20, %1 ]
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i64 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* @SOL_SOCKET
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i64 0, i32 1
%30 = load i64, i64* %29
%31 = load i64, i64* @SCM_RIGHTS
%32 = icmp eq i64 %30, %31
br i1 %32, label %36, label %33
33: ; preds = %22, %28
%34 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %23)
%35 = icmp eq %struct.cmsghdr* %34, null
br i1 %35, label %41, label %22
36: ; preds = %28
%37 = call i64 @CMSG_DATA(%struct.cmsghdr* %23)
%38 = inttoptr i64 %37 to i32*
%39 = load i32, i32* %38
%40 = call i32 @make_cloexec(i32 %39)
br label %43
41: ; preds = %33, %1
%42 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %36, %41
%44 = phi i32 [ %39, %36 ], [ undef, %41 ]
ret i32 %44
}