#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 {
  void *iov_base;
  int /*<<< orphan*/ iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  int /*<<< orphan*/ cmsg_len;
};
typedef int /*<<< orphan*/ ssize_t;

/* 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, const void *buffer, ssize_t buffer_size) {
  char control[CMSG_SPACE(sizeof(int))];
  struct iovec iov = {.iov_base = (void *)buffer, .iov_len = buffer_size};
  struct msghdr message = {
      .msg_name = NULL,
      .msg_namelen = 0,
      .msg_iov = &iov,
      .msg_iovlen = 1,
  };
  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    $0x80,%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,-0x80(%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,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
movl   $0x1,-0x68(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     125e <send_fd+0xee>
mov    -0x80(%rbp),%rax
mov    -0x78(%rbp),%rcx
mov    %rcx,-0x60(%rbp)
mov    %eax,-0x58(%rbp)
lea    -0x68(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2da0(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x70(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    126d <send_fd+0xfd>
movq   $0x0,-0x60(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x68(%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    $0x50,%rsp
mov    %ecx,%r15d
mov    %rdx,%r12
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
mov    %r12,-0x70(%rbp)
mov    %r15d,-0x68(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x60(%rbp)
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x40(%rbp)
movl   $0x1,-0x60(%rbp)
lea    -0x70(%rbp),%rdx
mov    %rdx,-0x48(%rbp)
cmp    $0xffffffff,%ebx
je     1216 <send_fd+0xb6>
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    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2de0(%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    121d <send_fd+0xbd>
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 { i8*, 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, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.iovec
%12 = alloca %struct.msghdr
%13 = alloca %struct.cmsghdr*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
%19 = load i8*, i8** %7
store i8* %19, i8** %18
%20 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
%21 = load i32, i32* %8
store i32 %21, i32* %20
%22 = bitcast %struct.msghdr* %12 to i8*
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 0
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %24
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %44
27: ; preds = %4
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
store i8* %17, i8** %28
%29 = trunc i64 %15 to i32
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 2
store i32 %29, i32* %30
%31 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %12)
store %struct.cmsghdr* %31, %struct.cmsghdr** %13
%32 = call i32 @CMSG_LEN(i32 4)
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 2
store i32 %32, i32* %34
%35 = load i32, i32* @SOL_SOCKET
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 1
store i32 %35, i32* %37
%38 = load i32, i32* @SCM_RIGHTS
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%42 = call i32 @CMSG_DATA(%struct.cmsghdr* %41)
%43 = call i32 @memcpy(i32 %42, i32* %6, i32 4)
br label %47
44: ; preds = %4
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
store i8* null, i8** %45
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 2
store i32 0, i32* %46
br label %47
47: ; preds = %44, %27
%48 = load i32, i32* %5
%49 = call i32 @sendmsg(i32 %48, %struct.msghdr* %12, i32 0)
%50 = load i8*, i8** %9
ret i32 %49
}

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