#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*/ 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 = recvmsg(chan, &msg, 0);
  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);
      return fd;
    }
  }
  return -1;
}


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   1060 <recvmsg@plt>
mov    %eax,-0x64(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     1258 <recv_fd+0x108>
mov    -0x70(%rbp),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <recv_fd+0xed>
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <recv_fd+0xed>
mov    -0x70(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1266 <recv_fd+0x116>
jmp    1242 <recv_fd+0xf2>
mov    -0x70(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x70(%rbp)
jmp    11e3 <recv_fd+0x93>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-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   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%r14d
movb   $0x0,-0x29(%rbp)
lea    -0x29(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x1,-0x68(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x60(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x60(%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,-0x58(%rbp)
mov    %eax,-0x50(%rbp)
lea    -0x60(%rbp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %rbx,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     122e <recv_fd+0xde>
mov    0x2dfd(%rip),%r12
mov    0x2de6(%rip),%r13
lea    -0x60(%rbp),%rbx
jmp    1210 <recv_fd+0xc0>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     122e <recv_fd+0xde>
mov    (%rax),%rcx
cmp    (%r12),%rcx
jne    1200 <recv_fd+0xb0>
mov    0x8(%rax),%rcx
cmp    0x0(%r13),%rcx
jne    1200 <recv_fd+0xb0>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%r14d
mov    %r15,%rsp
mov    %r14d,%eax
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, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
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)
store i32 %28, i32* %9
%29 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
store %struct.cmsghdr* %29, %struct.cmsghdr** %10
br label %30
30: ; preds = %52, %1
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%32 = icmp ne %struct.cmsghdr* %31, null
br i1 %32, label %33, label %55
33: ; preds = %30
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* @SOL_SOCKET
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %51
39: ; preds = %33
%40 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %40, i32 0, i32 1
%42 = load i64, i64* %41
%43 = load i64, i64* @SCM_RIGHTS
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%47 = call i64 @CMSG_DATA(%struct.cmsghdr* %46)
%48 = inttoptr i64 %47 to i32*
%49 = load i32, i32* %48
store i32 %49, i32* %11
%50 = load i32, i32* %11
store i32 %50, i32* %2
store i32 1, i32* %12
br label %56
51: ; preds = %39, %33
br label %52
52: ; preds = %51
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%54 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %6, %struct.cmsghdr* %53)
store %struct.cmsghdr* %54, %struct.cmsghdr** %10
br label %30
55: ; preds = %30
store i32 -1, i32* %2
store i32 1, i32* %12
br label %56
56: ; preds = %55, %45
%57 = load i8*, i8** %7
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
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 %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%20 = icmp eq %struct.cmsghdr* %19, null
br i1 %20, label %39, label %21
21: ; preds = %1, %36
%22 = phi %struct.cmsghdr* [ %37, %36 ], [ %19, %1 ]
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i64 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @SOL_SOCKET
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i64 0, i32 1
%29 = load i64, i64* %28
%30 = load i64, i64* @SCM_RIGHTS
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = call i64 @CMSG_DATA(%struct.cmsghdr* %22)
%34 = inttoptr i64 %33 to i32*
%35 = load i32, i32* %34
br label %39
36: ; preds = %21, %27
%37 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %22)
%38 = icmp eq %struct.cmsghdr* %37, null
br i1 %38, label %39, label %21
39: ; preds = %36, %1, %32
%40 = phi i32 [ %35, %32 ], [ -1, %1 ], [ -1, %36 ]
ret i32 %40
}