#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;
  unsigned int msg_controllen;
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  unsigned int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int sockfd, int fd) {
  ssize_t rv;
  char tmp[((((unsigned int)(sizeof(struct cmsghdr)) + (sizeof(int) - 1)) &
             ~(sizeof(int) - 1)) +
            (((unsigned int)(sizeof(int)) + (sizeof(int) - 1)) &
             ~(sizeof(int) - 1)))];
  struct cmsghdr *cmsg;
  struct iovec iov;
  struct msghdr msg;
  char ch = '\0';
  memset(&msg, 0, sizeof(msg));
  msg.msg_control = (char *)tmp;
  msg.msg_controllen =
      ((((unsigned int)(sizeof(struct cmsghdr)) + (sizeof(int) - 1)) &
        ~(sizeof(int) - 1)) +
       (((unsigned int)(sizeof(int)) + (sizeof(int) - 1)) &
        ~(sizeof(int) - 1)));
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len =
      ((((unsigned int)(sizeof(struct cmsghdr)) + (sizeof(int) - 1)) &
        ~(sizeof(int) - 1)) +
       (((unsigned int)(sizeof(int)) + (sizeof(int) - 1)) &
        ~(sizeof(int) - 1)));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  *(int *)CMSG_DATA(cmsg) = fd;
  iov.iov_base = &ch;
  iov.iov_len = 1;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  rv = sendmsg(sockfd, &msg, 0);
  if (rv != 1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x0,-0x51(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x10,-0x48(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x10,(%rax)
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x58(%rbp),%ecx
mov    %ecx,(%rax)
lea    -0x51(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
je     1201 <send_fd+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <send_fd+0xc8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
movb   $0x0,0x7(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    0x30(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x10,0x10(%rsp)
mov    %rbx,%rdi
call   1060 <CMSG_FIRSTHDR@plt>
movl   $0x10,(%rax)
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
mov    %rax,%rdi
call   1040 <CMSG_DATA@plt>
mov    %ebp,(%rax)
lea    0x7(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x28(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x14(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
setne  %cl
neg    %ecx
mov    %ecx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32, i32, i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
@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 i32
%7 = alloca [16 x i8]
%8 = alloca %struct.cmsghdr*
%9 = alloca %struct.iovec
%10 = alloca %struct.msghdr
%11 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8 0, i8* %11
%12 = call i32 @memset(%struct.msghdr* %10, i32 0, i32 24)
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i32 16, i32* %15
%16 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %10)
store %struct.cmsghdr* %16, %struct.cmsghdr** %8
%17 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%18 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %17, i32 0, i32 0
store i32 16, i32* %18
%19 = load i32, i32* @SOL_SOCKET
%20 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%21 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = load i32, i32* @SCM_RIGHTS
%23 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load i32, i32* %5
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%27 = call i64 @CMSG_DATA(%struct.cmsghdr* %26)
%28 = inttoptr i64 %27 to i32*
store i32 %25, i32* %28
%29 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
store i8* %11, i8** %29
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store %struct.iovec* %9, %struct.iovec** %31
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 1, i32* %32
%33 = load i32, i32* %4
%34 = call i32 @sendmsg(i32 %33, %struct.msghdr* %10, i32 0)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %2
store i32 -1, i32* %3
br label %39
38: ; preds = %2
store i32 0, i32* %3
br label %39
39: ; preds = %38, %37
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @send_fd(i32 %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = alloca i8
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.iovec* %4 to i8*
%9 = bitcast %struct.msghdr* %5 to i8*
store i8 0, i8* %6
%10 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 24)
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i8* %7, i8** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 16, i32* %12
%13 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%14 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 0
store i32 16, i32* %14
%15 = load i32, i32* @SOL_SOCKET
%16 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @SCM_RIGHTS
%18 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %13, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i64 @CMSG_DATA(%struct.cmsghdr* %13)
%20 = inttoptr i64 %19 to i32*
store i32 %1, i32* %20
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %6, i8** %21
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 1, i32* %24
%25 = call i32 @sendmsg(i32 %0, %struct.msghdr* %5, i32 0)
%26 = icmp ne i32 %25, 1
%27 = sext i1 %26 to i32
ret i32 %27
}