#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*/ msg_namelen;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  int dummy;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ control;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
ssize_t send_fd(int socket, int fd, struct iovec *iov, int iovlen) {
  char control[CMSG_SPACE(sizeof(fd))];
  struct msghdr message = {
      .msg_name = NULL,
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
  };
  struct cmsghdr *cmsg;
  if (fd != -1) {
    message.msg_control = control, message.msg_controllen = sizeof(control);
    cmsg = CMSG_FIRSTHDR(&message);
    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
  } else {
    message.msg_control = NULL;
    message.msg_controllen = 0;
  }
  return sendmsg(socket, &message, 0);
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     124c <send_fd+0xdc>
mov    -0x70(%rbp),%rax
mov    -0x68(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
mov    %eax,-0x48(%rbp)
lea    -0x58(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x60(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dd2(%rip),%rax
mov    (%rax),%ecx
mov    -0x60(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
mov    -0x60(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x60(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    125b <send_fd+0xeb>
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x58(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x20(%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    %ecx,%r12d
mov    %rdx,%r15
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,-0x24(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%edx
mov    %rsp,%rcx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rcx
mov    %rcx,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x60(%rbp)
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x40(%rbp)
mov    %r12d,-0x60(%rbp)
mov    %r15,-0x48(%rbp)
cmp    $0xffffffff,%ebx
je     1207 <send_fd+0xa7>
mov    %rcx,-0x58(%rbp)
mov    %eax,-0x50(%rbp)
lea    -0x60(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,0x8(%rbx)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x24(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <memcpy@plt>
jmp    120e <send_fd+0xae>
movl   $0x0,-0x50(%rbp)
lea    -0x60(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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