#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 /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ 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 /*<<< orphan*/ err_remark(char *, int);
int /*<<< orphan*/ err_syserr(char *);
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void send_fd(int socket, int fd) {
  struct msghdr msg = {0};
  char buf[CMSG_SPACE(sizeof(fd))];
  memset(buf, '\0', sizeof(buf));
  struct iovec io[2] = {
      {.iov_base = "ABC", .iov_len = 3},
      {.iov_base = "CDE", .iov_len = 3},
  };
  msg.msg_iov = io;
  msg.msg_iovlen = 2;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  err_remark("put fd %d\n", fd);
  *((int *)CMSG_DATA(cmsg)) = fd;
  msg.msg_controllen = cmsg->cmsg_len;
  if (sendmsg(socket, &msg, 0) < 0)
    err_syserr("Failed to send message\n");
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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   1080 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x78(%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,-0x80(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x80(%rbp),%rcx
mov    -0x78(%rbp),%rax
mov    0x2c01(%rip),%rdx
mov    %rdx,-0x60(%rbp)
mov    0x2bfe(%rip),%rdx
mov    %rdx,-0x58(%rbp)
mov    0x2bfb(%rip),%rdx
mov    %rdx,-0x50(%rbp)
mov    0x2bf8(%rip),%rdx
mov    %rdx,-0x48(%rbp)
lea    -0x60(%rbp),%rdx
mov    %rdx,-0x18(%rbp)
movl   $0x2,-0x30(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x68(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2d8a(%rip),%rax
mov    (%rax),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%esi
lea    0xd95(%rip),%rdi
call   1090 <err_remark@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x6c(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    12b9 <send_fd+0x139>
lea    0xd5f(%rip),%rdi
call   1060 <err_syserr@plt>
mov    -0x38(%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,%r12d
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %eax,%r15d
mov    %rsp,%r13
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>
movaps 0x2c1f(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0x2c04(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x2,-0x50(%rbp)
mov    %rbx,-0x48(%rbp)
mov    %r15d,-0x40(%rbp)
lea    -0x50(%rbp),%r15
mov    %r15,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,(%rbx)
lea    0xdc7(%rip),%rdi
mov    %r12d,%esi
call   1090 <err_remark@plt>
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r12d,(%rax)
mov    (%rbx),%eax
mov    %eax,-0x40(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
jns    1277 <send_fd+0xf7>
lea    0xda1(%rip),%rdi
call   1060 <err_syserr@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 }
@.str = [4 x i8] c"ABC\00"
@.str.1 = [4 x i8] c"CDE\00"
@__const.send_fd.io = [2 x %struct.iovec] [%struct.iovec { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 3 }, %struct.iovec { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i32 3 }]
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str.2 = [11 x i8] c"put fd %d\0A\00"
@.str.3 = [24 x i8] c"Failed to send message\0A\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.msghdr
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [2 x %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 [2 x %struct.iovec]* %8 to i8*
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %8, i64 0, i64 0
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store %struct.iovec* %18, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i32 2, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i8* %14, i8** %21
%22 = trunc i64 %12 to i32
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 %22, i32* %23
%24 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
store %struct.cmsghdr* %24, %struct.cmsghdr** %9
%25 = load i32, i32* @SOL_SOCKET
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i32 0, i32 2
store i32 %25, i32* %27
%28 = load i32, i32* @SCM_RIGHTS
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %29, i32 0, i32 1
store i32 %28, i32* %30
%31 = call i32 @CMSG_LEN(i32 4)
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %4
%35 = call i32 @err_remark(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %4
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%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** %9
%41 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* %3
%45 = call i64 @sendmsg(i32 %44, %struct.msghdr* %5, i32 0)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %2
%48 = call i32 @err_syserr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %2
%50 = load i8*, i8** %6
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 }
@.str = [4 x i8] c"ABC\00"
@.str.1 = [4 x i8] c"CDE\00"
@__const.send_fd.io = [2 x %struct.iovec] [%struct.iovec { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 3 }, %struct.iovec { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i32 3 }]
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str.2 = [11 x i8] c"put fd %d\0A\00"
@.str.3 = [24 x i8] c"Failed to send message\0A\00"
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca %struct.msghdr
%4 = alloca [2 x %struct.iovec]
%5 = bitcast %struct.msghdr* %3 to i8*
%6 = tail call i32 @CMSG_SPACE(i32 4)
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(i8* %9, i8 signext 0, i32 %6)
%11 = bitcast [2 x %struct.iovec]* %4 to i8*
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %4, i64 0, i64 0
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %12, %struct.iovec** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 2, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i8* %9, i8** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 %6, i32* %16
%17 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%18 = load i32, i32* @SOL_SOCKET
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @SCM_RIGHTS
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @CMSG_LEN(i32 4)
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i64 0, i32 0
store i32 %22, i32* %23
%24 = call i32 @err_remark(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%25 = call i64 @CMSG_DATA(%struct.cmsghdr* %17)
%26 = inttoptr i64 %25 to i32*
store i32 %1, i32* %26
%27 = load i32, i32* %23
store i32 %27, i32* %16
%28 = call i64 @sendmsg(i32 %0, %struct.msghdr* %3, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 @err_syserr(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %2
ret void
}