#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 cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int sendfd(int dst_fd, int fd) {
  int file_descriptors[1] = {fd};
  char buffer[CMSG_SPACE(sizeof file_descriptors)];
  struct msghdr message = {
      .msg_control = buffer,
      .msg_controllen = sizeof buffer,
  };
  struct cmsghdr *cmessage = CMSG_FIRSTHDR(&message);
  cmessage->cmsg_level = SOL_SOCKET;
  cmessage->cmsg_type = SCM_RIGHTS;
  cmessage->cmsg_len = CMSG_LEN(sizeof file_descriptors);
  message.msg_controllen = cmessage->cmsg_len;
  memcpy(CMSG_DATA(cmessage), file_descriptors, sizeof file_descriptors);
  char ping = 23;
  struct iovec ping_vec = {
      .iov_base = &ping,
      .iov_len = sizeof ping,
  };
  message.msg_iov = &ping_vec;
  message.msg_iovlen = 1;
  return sendmsg(dst_fd, &message, 0);
}


x86 assembly (clang, -O0):
<sendfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x40(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movb   $0x17,-0x41(%rbp)
lea    -0x41(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edi,%r14d
mov    %esi,-0x28(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rdx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x40(%rbp),%r15
mov    %r15,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %eax,-0x38(%rbp)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x28(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movb   $0x17,-0x21(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x48(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @sendfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i32]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca i8
%11 = alloca %struct.iovec
store i32 %0, i32* %3
store i32 %1, i32* %4
%12 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%13 = load i32, i32* %4
store i32 %13, i32* %12
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
%20 = trunc i64 %15 to i32
store i32 %20, i32* %19
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 0, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* null, %struct.iovec** %22
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %23, %struct.cmsghdr** %9
%24 = load i32, i32* @SOL_SOCKET
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 2
store i32 %24, i32* %26
%27 = load i32, i32* @SCM_RIGHTS
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 1
store i32 %27, i32* %29
%30 = call i32 @CMSG_LEN(i32 4)
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 %35, i32* %36
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%38 = call i32 @CMSG_DATA(%struct.cmsghdr* %37)
%39 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%40 = call i32 @memcpy(i32 %38, i32* %39, i32 4)
store i8 23, i8* %10
%41 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i8* %10, i8** %41
%42 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i32 1, i32* %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %43
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 1, i32* %44
%45 = load i32, i32* %3
%46 = call i32 @sendmsg(i32 %45, %struct.msghdr* %8, i32 0)
%47 = load i8*, i8** %6
ret i32 %46
}

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