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

/* 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*/ memmove(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ memset(char *, char, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int s, int fd) {
  struct msghdr msg = {0};
  char buf[CMSG_SPACE(sizeof(int))];
  memset(buf, '\0', sizeof(buf));
  struct iovec io = {.iov_base = "", .iov_len = 1};
  msg.msg_iov = &io;
  msg.msg_iovlen = 1;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_level = SOL_SOCKET;
  memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
  msg.msg_controllen = cmsg->cmsg_len;
  return (sendmsg(s, &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)
movq   $0x0,-0x10(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rax
mov    0x2c24(%rip),%rdx
mov    %rdx,-0x50(%rbp)
mov    0x2c21(%rip),%rdx
mov    %rdx,-0x48(%rbp)
lea    -0x50(%rbp),%rdx
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x30(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x58(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    -0x58(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
mov    %esi,-0x24(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%r15d
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <memset@plt>
movups 0x2c31(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rbx,-0x48(%rbp)
mov    %r15d,-0x40(%rbp)
lea    -0x50(%rbp),%r15
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x24(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    (%rbx),%eax
mov    %eax,-0x40(%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.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@.str = [1 x i8] zeroinitializer
@__const.send_fd.io = %struct.iovec { i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0), i32 1 }
@SCM_RIGHTS = global i32 0
@SOL_SOCKET = global i32 0
define i32 @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.msghdr
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.iovec
%9 = alloca %struct.cmsghdr*
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = bitcast %struct.msghdr* %5 to i8*
%11 = call i32 @CMSG_SPACE(i32 4)
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = trunc i64 %12 to i32
%16 = call i32 @memset(i8* %14, i8 signext 0, i32 %15)
%17 = bitcast %struct.iovec* %8 to i8*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store %struct.iovec* %8, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i8* %14, i8** %20
%21 = trunc i64 %12 to i32
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 %21, i32* %22
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
store %struct.cmsghdr* %23, %struct.cmsghdr** %9
%24 = call i32 @CMSG_LEN(i32 4)
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 0
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 2
store i32 %27, i32* %29
%30 = load i32, i32* @SOL_SOCKET
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%34 = call i32 @CMSG_DATA(%struct.cmsghdr* %33)
%35 = call i32 @memmove(i32 %34, i32* %4, i32 4)
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%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 2
store i32 %38, i32* %39
%40 = load i32, i32* %3
%41 = call i32 @sendmsg(i32 %40, %struct.msghdr* %5, i32 0)
%42 = load i8*, i8** %6
ret i32 %41
}

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