#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 {
  char *msg_control;
  int msg_controllen;
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recvfd(int src_fd) {
  int file_descriptors[1];
  char buffer[CMSG_SPACE(sizeof file_descriptors)];
  char ping;
  struct iovec ping_vec = {
      .iov_base = &ping,
      .iov_len = sizeof ping,
  };
  struct msghdr message = {
      .msg_control = buffer,
      .msg_controllen = sizeof buffer,
      .msg_iov = &ping_vec,
      .msg_iovlen = 1,
  };
  recvmsg(src_fd, &message, 0);
  struct cmsghdr *cmessage = CMSG_FIRSTHDR(&message);
  memcpy(file_descriptors, CMSG_DATA(cmessage), sizeof file_descriptors);
  return file_descriptors[0];
}


x86 assembly (clang, -O0):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    -0x19(%rbp),%rdx
mov    %rdx,-0x30(%rbp)
movl   $0x1,-0x28(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
movl   $0x1,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
lea    -0x48(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x50(%rbp)
lea    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r14d
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    -0x19(%rbp),%rcx
mov    %rcx,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
mov    %rdx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x38(%rbp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %rbx,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
lea    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%eax
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.cmsghdr = type { i32 }
define i32 @recvfd(i32 %0) {
%2 = alloca i32
%3 = alloca [1 x i32]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
store i32 %0, i32* %2
%10 = call i32 @CMSG_SPACE(i32 4)
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
store i8* %6, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i8* %13, i8** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
%18 = trunc i64 %11 to i32
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %7, %struct.iovec** %20
%21 = load i32, i32* %2
%22 = call i32 @recvmsg(i32 %21, %struct.msghdr* %8, i32 0)
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %23, %struct.cmsghdr** %9
%24 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%26 = call i32 @CMSG_DATA(%struct.cmsghdr* %25)
%27 = call i32 @memcpy(i32* %24, i32 %26, i32 4)
%28 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i8*, i8** %4
ret i32 %29
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.cmsghdr = type { i32 }
define i32 @recvfd(i32 %0) {
%2 = alloca [1 x i32]
%3 = alloca i8
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = bitcast [1 x i32]* %2 to i8*
%7 = tail call i32 @CMSG_SPACE(i32 4)
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast %struct.iovec* %4 to i8*
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %3, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %13
%14 = bitcast %struct.msghdr* %5 to i8*
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i8* %10, i8** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 %7, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %18
%19 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%20 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%21 = getelementptr inbounds [1 x i32], [1 x i32]* %2, i64 0, i64 0
%22 = call i32 @CMSG_DATA(%struct.cmsghdr* %20)
%23 = call i32 @memcpy(i32* %21, i32 %22, i32 4)
%24 = load i32, i32* %21
ret i32 %24
}