#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 {
  char *msg_control;
  int msg_controllen;
  int msg_iovlen;
  scalar_t__ msg_flags;
  struct iovec *msg_iov;
  scalar_t__ msg_namelen;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ sendchar;
typedef int /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ cmsgbuf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int);
int CMSG_SPACE(int);
scalar_t__ EINTR;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int sock_fd, int fd) {
  int retval;
  struct msghdr msg;
  struct cmsghdr *p_cmsg;
  struct iovec vec;
  char cmsgbuf[CMSG_SPACE(sizeof(fd))];
  int *p_fds;
  char sendchar = 0;
  msg.msg_control = cmsgbuf;
  msg.msg_controllen = sizeof(cmsgbuf);
  p_cmsg = CMSG_FIRSTHDR(&msg);
  p_cmsg->cmsg_level = SOL_SOCKET;
  p_cmsg->cmsg_type = SCM_RIGHTS;
  p_cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  p_fds = (int *)CMSG_DATA(p_cmsg);
  *p_fds = fd;
  msg.msg_controllen = p_cmsg->cmsg_len;
  msg.msg_name = NULL;
  msg.msg_namelen = 0;
  msg.msg_iov = &vec;
  msg.msg_iovlen = 1;
  msg.msg_flags = 0;
  vec.iov_base = &sendchar;
  vec.iov_len = sizeof(sendchar);
  while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR)
    ;
  if (retval != 1) {
    perror("sending file descriptor");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
movb   $0x0,-0x71(%rbp)
mov    %rcx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x70(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x71(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x79(%rbp)
jne    1276 <send_fd+0x116>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x79(%rbp)
mov    -0x79(%rbp),%al
test   $0x1,%al
jne    1286 <send_fd+0x126>
jmp    128b <send_fd+0x12b>
jmp    123b <send_fd+0xdb>
cmpl   $0x1,-0x10(%rbp)
je     12b4 <send_fd+0x154>
lea    0xd64(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    12c2 <send_fd+0x162>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
mov    -0x60(%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   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %esi,%r15d
mov    %edi,%r13d
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,-0x38(%rbp)
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
movb   $0x0,-0x29(%rbp)
mov    %rdx,-0x78(%rbp)
mov    %eax,-0x70(%rbp)
lea    -0x78(%rbp),%r14
mov    %r14,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,%r12
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r12)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r12)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%r12)
mov    %r12,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r15d,(%rax)
mov    (%r12),%eax
mov    %eax,-0x70(%rbp)
xorps  %xmm0,%xmm0
movups %xmm0,-0x58(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x1,-0x6c(%rbp)
movq   $0x0,-0x68(%rbp)
lea    -0x29(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
mov    0x2db6(%rip),%r15
mov    0x2d9f(%rip),%rbx
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
jne    124a <send_fd+0xea>
mov    (%rbx),%rcx
cmp    %rcx,(%r15)
je     1230 <send_fd+0xd0>
xor    %ecx,%ecx
cmp    $0x1,%eax
jne    1266 <send_fd+0x106>
mov    -0x38(%rbp),%rsp
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd93(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ecx
jmp    1251 <send_fd+0xf1>

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, i64, %struct.iovec*, i64, i32* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [24 x i8] c"sending file descriptor\00"
define i32 @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.msghdr
%8 = alloca %struct.cmsghdr*
%9 = alloca %struct.iovec
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32*
%13 = alloca i8
%14 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%15 = call i32 @CMSG_SPACE(i32 4)
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %10
%18 = alloca i8, i64 %16
store i64 %16, i64* %11
store i8 0, i8* %13
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 0
store i8* %18, i8** %19
%20 = trunc i64 %16 to i32
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i32 %20, i32* %21
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
store %struct.cmsghdr* %22, %struct.cmsghdr** %8
%23 = load i32, i32* @SOL_SOCKET
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %24, i32 0, i32 2
store i32 %23, i32* %25
%26 = load i32, i32* @SCM_RIGHTS
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i32 0, i32 1
store i32 %26, i32* %28
%29 = call i32 @CMSG_LEN(i32 4)
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%33 = call i64 @CMSG_DATA(%struct.cmsghdr* %32)
%34 = inttoptr i64 %33 to i32*
store i32* %34, i32** %12
%35 = load i32, i32* %5
%36 = load i32*, i32** %12
store i32 %35, i32* %36
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 6
store i32* null, i32** %41
%42 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 5
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 4
store %struct.iovec* %9, %struct.iovec** %43
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 1, i32* %44
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 3
store i64 0, i64* %45
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
store i8* %13, i8** %46
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
store i32 1, i32* %47
br label %48
48: ; preds = %58, %2
%49 = load i32, i32* %4
%50 = call i32 @sendmsg(i32 %49, %struct.msghdr* %7, i32 0)
store i32 %50, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = load i64, i64* @errno
%54 = load i64, i64* @EINTR
%55 = icmp eq i64 %53, %54
br label %56
56: ; preds = %52, %48
%57 = phi i1 [ false, %48 ], [ %55, %52 ]
br i1 %57, label %58, label %59
58: ; preds = %56
br label %48
59: ; preds = %56
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %14
br label %65
64: ; preds = %59
store i32 0, i32* %3
store i32 1, i32* %14
br label %65
65: ; preds = %64, %62
%66 = load i8*, i8** %10
%67 = load i32, i32* %3
ret i32 %67
}

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