#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;
  int msg_controllen;
  struct cmsghdr *msg_control;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  void *member_0;
  size_t member_1;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int CMSG_LEN(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
ssize_t sendfd(int socket, const void *buffer, size_t length, int fd) {
  struct iovec iov = {(void *)buffer, length};
  char buf[CMSG_LEN(sizeof(int))];
  struct cmsghdr *cmsg = (struct cmsghdr *)buf;
  ssize_t r;
  cmsg->cmsg_len = sizeof(buf);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  *((int *)CMSG_DATA(cmsg)) = fd;
  struct msghdr msg = {0};
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = cmsg;
  msg.msg_controllen = cmsg->cmsg_len;
  r = sendmsg(socket, &msg, 0);
  if (r < 0)
    warn("sendmsg");
  return r;
}


x86 assembly (clang, -O0):
<sendfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <CMSG_DATA@plt>
mov    -0x74(%rbp),%ecx
mov    %ecx,(%rax)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x1,-0x70(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
xor    %edx,%edx
call   1040 <sendmsg@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jge    123d <sendfd+0xed>
lea    0xdc8(%rip),%rdi
call   1030 <warn@plt>
mov    -0x50(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r15d
mov    %edi,%r14d
mov    %rsi,-0x60(%rbp)
mov    %rdx,-0x58(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %rsp,-0x30(%rbp)
mov    %eax,%r13d
mov    %rsp,%r12
add    $0xf,%r13
and    $0xfffffffffffffff0,%r13
mov    %r12,%rbx
sub    %r13,%rbx
mov    %rbx,%rsp
neg    %r13
mov    %eax,(%rbx)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    %rbx,%rdi
call   1050 <CMSG_DATA@plt>
mov    %r15d,(%rax)
movq   $0x0,-0x38(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rbx,-0x48(%rbp)
mov    (%r12,%r13,1),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x50(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <sendmsg@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11f0 <sendfd+0xb0>
lea    0xe15(%rip),%rdi
call   1030 <warn@plt>
mov    -0x30(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i64 }
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i32, %struct.cmsghdr*, %struct.iovec*, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [8 x i8] c"sendmsg\00"
define i64 @sendfd(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.iovec
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.cmsghdr*
%13 = alloca i64
%14 = alloca %struct.msghdr
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
%16 = load i8*, i8** %6
store i8* %16, i8** %15
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
%18 = load i64, i64* %7
store i64 %18, i64* %17
%19 = call i32 @CMSG_LEN(i32 4)
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %10
%22 = alloca i8, i64 %20
store i64 %20, i64* %11
%23 = bitcast i8* %22 to %struct.cmsghdr*
store %struct.cmsghdr* %23, %struct.cmsghdr** %12
%24 = trunc i64 %20 to i32
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* @SOL_SOCKET
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%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** %12
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = load i32, i32* %8
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%35 = call i64 @CMSG_DATA(%struct.cmsghdr* %34)
%36 = inttoptr i64 %35 to i32*
store i32 %33, i32* %36
%37 = bitcast %struct.msghdr* %14 to i8*
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 3
store %struct.iovec* %9, %struct.iovec** %38
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
store i32 1, i32* %39
%40 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 2
store %struct.cmsghdr* %40, %struct.cmsghdr** %41
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 1
store i32 %44, i32* %45
%46 = load i32, i32* %5
%47 = call i64 @sendmsg(i32 %46, %struct.msghdr* %14, i32 0)
store i64 %47, i64* %13
%48 = load i64, i64* %13
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %4
%51 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %50, %4
%53 = load i64, i64* %13
%54 = load i8*, i8** %10
ret i64 %53
}

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