#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;
  void *msg_controllen;
  scalar_t__ msg_control;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  void *cmsg_len;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
void *CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memmove(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int sock, int fd) {
  char buf[1];
  struct iovec iov;
  struct msghdr msg;
  struct cmsghdr *cmsg;
  int n;
  char cms[CMSG_SPACE(sizeof(int))];
  buf[0] = 0;
  iov.iov_base = buf;
  iov.iov_len = 1;
  memset(&msg, 0, sizeof msg);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = (caddr_t)cms;
  msg.msg_controllen = CMSG_LEN(sizeof(int));
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
  if ((n = sendmsg(sock, &msg, 0)) != iov.iov_len)
    return -1;
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x4,%edi
call   1080 <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,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
movb   $0x0,-0xd(%rbp)
lea    -0xd(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x70(%rbp),%rax
lea    -0x20(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
mov    %rax,-0x30(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %rax,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    0x2dba(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memmove@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x4c(%rbp)
cmp    -0x18(%rbp),%eax
je     128c <send_fd+0x10c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    12a2 <send_fd+0x122>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
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    %edi,%r14d
mov    %esi,-0x28(%rbp)
mov    $0x4,%edi
call   1080 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movb   $0x0,-0x21(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x58(%rbp)
mov    %rbx,-0x48(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %rax,-0x50(%rbp)
mov    %r15,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1070 <CMSG_LEN@plt>
mov    %rax,0x8(%rbx)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x28(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1090 <memmove@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    -0x30(%rbp),%ecx
jne    1267 <send_fd+0xe7>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %r12,%rsp
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*, i64, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i8* }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x i8]
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca i8, i64 %15
store i64 %15, i64* %12
%18 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
store i8 0, i8* %18
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%20 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 1, i32* %21
%22 = call i32 @memset(%struct.msghdr* %8, i32 0, i32 32)
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %7, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %24
%25 = ptrtoint i8* %17 to i64
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i64 %25, i64* %26
%27 = call i8* @CMSG_LEN(i32 4)
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i8* %27, i8** %28
%29 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %29, %struct.cmsghdr** %9
%30 = call i8* @CMSG_LEN(i32 4)
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 2
store i8* %30, i8** %32
%33 = load i32, i32* @SOL_SOCKET
%34 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%35 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %34, i32 0, i32 1
store i32 %33, i32* %35
%36 = load i32, i32* @SCM_RIGHTS
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%40 = call i32 @CMSG_DATA(%struct.cmsghdr* %39)
%41 = call i32 @memmove(i32 %40, i32* %5, i32 4)
%42 = load i32, i32* %4
%43 = call i32 @sendmsg(i32 %42, %struct.msghdr* %8, i32 0)
store i32 %43, i32* %10
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
%45 = load i32, i32* %44
%46 = icmp ne i32 %43, %45
br i1 %46, label %47, label %48
47: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %13
br label %51
48: ; preds = %2
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
store i32 1, i32* %13
br label %51
51: ; preds = %48, %47
%52 = load i8*, i8** %11
%53 = load i32, i32* %3
ret i32 %53
}

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