#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_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ ctl_buf;

/* 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 check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void send_fd(int chan, int fd) {
  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);
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  *(int *)CMSG_DATA(cmsg) = fd;
  msg.msg_controllen = cmsg->cmsg_len;
  ssize_t send_len = check(sendmsg(chan, &msg, 0), "sendmsg(fd)");
  if (send_len != 1) {
    err(1, "sendmsg(fd len)");
  }
  check(close(fd), "close(send fd)");
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-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   1070 <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)
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x68(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dbf(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x70(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,%edi
lea    0xd98(%rip),%rsi
call   1090 <check@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x1,-0x6c(%rbp)
je     128b <send_fd+0x10b>
mov    $0x1,%edi
lea    0xd86(%rip),%rsi
call   1080 <err@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,%edi
lea    0xd80(%rip),%rsi
call   1090 <check@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    $0x48,%rsp
mov    %esi,%r15d
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   1070 <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,-0x58(%rbp)
mov    %eax,-0x50(%rbp)
lea    -0x60(%rbp),%r12
mov    %r12,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r15d,(%rax)
mov    (%rbx),%eax
mov    %eax,-0x50(%rbp)
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
lea    0xdb6(%rip),%rsi
mov    %eax,%edi
call   1090 <check@plt>
cmp    $0x1,%eax
je     1267 <send_fd+0xe7>
lea    0xdaf(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %r15d,%edi
call   1050 <close@plt>
lea    0xda6(%rip),%rsi
mov    %eax,%edi
call   1090 <check@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.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [12 x i8] c"sendmsg(fd)\00"
@.str.1 = [16 x i8] c"sendmsg(fd len)\00"
@.str.2 = [15 x i8] c"close(send fd)\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i8]
%6 = alloca %struct.iovec
%7 = alloca %struct.msghdr
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.cmsghdr*
%11 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%12 = bitcast [1 x i8]* %5 to i8*
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
%14 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
store i8* %14, i8** %13
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1, i32* %15
%16 = bitcast %struct.msghdr* %7 to i8*
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 0
store i32 1, i32* %18
%19 = call i32 @CMSG_SPACE(i32 4)
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %8
%22 = alloca i8, i64 %20
store i64 %20, i64* %9
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i8* %22, i8** %23
%24 = trunc i64 %20 to i32
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 %24, i32* %25
%26 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
store %struct.cmsghdr* %26, %struct.cmsghdr** %10
%27 = load i32, i32* @SOL_SOCKET
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 2
store i32 %27, i32* %29
%30 = load i32, i32* @SCM_RIGHTS
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = call i32 @CMSG_LEN(i32 4)
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* %4
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%38 = call i64 @CMSG_DATA(%struct.cmsghdr* %37)
%39 = inttoptr i64 %38 to i32*
store i32 %36, i32* %39
%40 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* %3
%45 = call i32 @sendmsg(i32 %44, %struct.msghdr* %7, i32 0)
%46 = call i32 @check(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp ne i32 %47, 1
br i1 %48, label %49, label %51
49: ; preds = %2
%50 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %2
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
%54 = call i32 @check(i32 %53, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%55 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [12 x i8] c"sendmsg(fd)\00"
@.str.1 = [16 x i8] c"sendmsg(fd len)\00"
@.str.2 = [15 x i8] c"close(send fd)\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca [1 x i8]
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 0, i8* %6
%7 = bitcast %struct.iovec* %4 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %6, i8** %8
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %9
%10 = bitcast %struct.msghdr* %5 to i8*
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %12
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
%16 = alloca i8, i64 %14
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 %13, i32* %18
%19 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%20 = load i32, i32* @SOL_SOCKET
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %19, i64 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @SCM_RIGHTS
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %19, i64 0, i32 1
store i32 %22, i32* %23
%24 = call i32 @CMSG_LEN(i32 4)
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %19, i64 0, i32 0
store i32 %24, i32* %25
%26 = call i64 @CMSG_DATA(%struct.cmsghdr* %19)
%27 = inttoptr i64 %26 to i32*
store i32 %1, i32* %27
%28 = load i32, i32* %25
store i32 %28, i32* %18
%29 = call i32 @sendmsg(i32 %0, %struct.msghdr* %5, i32 0)
%30 = call i32 @check(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i32 %30, 1
br i1 %31, label %34, label %32
32: ; preds = %2
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %2
%35 = call i32 @close(i32 %1)
%36 = call i32 @check(i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
ret void
}