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

/* Variables and functions */
scalar_t__ 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*/ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void send_fd(int sock, int fd) {
  struct msghdr msg = {0};
  struct iovec iov;
  struct cmsghdr *cmsg;
  int myfds[1];
  char buf[CMSG_SPACE(sizeof myfds)];
  int *fdptr;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof buf;
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  fdptr = (int *)CMSG_DATA(cmsg);
  fdptr[0] = fd;
  msg.msg_controllen = cmsg->cmsg_len;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  iov.iov_base = "W";
  iov.iov_len = 1;
  sendmsg(sock, &msg, 0);
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x20(%rbp)
movaps %xmm0,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <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,-0x30(%rbp)
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x68(%rbp)
mov    -0x8(%rbp),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
lea    0xdf1(%rip),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movups %xmm0,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r13
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    -0x48(%rbp),%r15
mov    %r15,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r12d,(%rax)
mov    (%rbx),%eax
mov    %eax,-0x40(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
lea    0xe0a(%rip),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [2 x i8] c"W\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.msghdr
%6 = alloca %struct.iovec
%7 = alloca %struct.cmsghdr*
%8 = alloca [1 x i32]
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
%12 = bitcast %struct.msghdr* %5 to i8*
%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 %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i8* %16, i8** %17
%18 = trunc i64 %14 to i32
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
store %struct.cmsghdr* %20, %struct.cmsghdr** %7
%21 = load i32, i32* @SOL_SOCKET
%22 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %22, i32 0, i32 2
store i32 %21, i32* %23
%24 = load i32, i32* @SCM_RIGHTS
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 1
store i32 %24, i32* %26
%27 = call i32 @CMSG_LEN(i32 4)
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%31 = call i64 @CMSG_DATA(%struct.cmsghdr* %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %11
%33 = load i32, i32* %4
%34 = load i32*, i32** %11
%35 = getelementptr inbounds i32, i32* %34, i64 0
store i32 %33, i32* %35
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %40
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 1, i32* %41
%42 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %42
%43 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1, i32* %43
%44 = load i32, i32* %3
%45 = call i32 @sendmsg(i32 %44, %struct.msghdr* %5, i32 0)
%46 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [2 x i8] c"W\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca %struct.msghdr
%4 = alloca %struct.iovec
%5 = bitcast %struct.msghdr* %3 to i8*
%6 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
%7 = bitcast i32* %6 to i8*
%8 = bitcast %struct.iovec* %4 to i8*
%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 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i8* %12, i8** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 %9, i32* %14
%15 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%16 = load i32, i32* @SOL_SOCKET
%17 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @SCM_RIGHTS
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @CMSG_LEN(i32 4)
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %15, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i64 @CMSG_DATA(%struct.cmsghdr* %15)
%23 = inttoptr i64 %22 to i32*
store i32 %1, i32* %23
%24 = load i32, i32* %21
store i32 %24, i32* %14
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %27
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %28
%29 = call i32 @sendmsg(i32 %0, %struct.msghdr* %3, i32 0)
ret void
}