#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 {
  unsigned char *msg_control;
  int msg_iovlen;
  int msg_controllen;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
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*/ close(int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int s, int fd) {
  struct msghdr msg;
  struct cmsghdr *cmsg;
  unsigned char fdbuf[(sizeof(struct cmsghdr) + sizeof(int))];
  struct iovec iov[1];
  char dummy[] = "";
  iov[0].iov_base = dummy;
  iov[0].iov_len = 1;
  memset(&msg, 0, sizeof(msg));
  msg.msg_control = fdbuf;
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  msg.msg_controllen = (sizeof(struct cmsghdr) + sizeof(int));
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len = (sizeof(struct cmsghdr) + sizeof(int));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  *(int *)CMSG_DATA(cmsg) = fd;
  if (sendmsg(s, &msg, 0) == -1) {
    perror("sendmsg");
    close(s);
    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)
lea    -0x51(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
lea    -0x51(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x48(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x10,-0x1c(%rbp)
lea    -0x28(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x10,(%rax)
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x58(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
jne    123d <send_fd+0xdd>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <send_fd+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x1,0x18(%rsp)
lea    0x28(%rsp),%rbp
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    0x40(%rsp),%rax
mov    %rax,0x28(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x38(%rsp)
movabs $0x1000000001,%rax
mov    %rax,0x30(%rsp)
mov    %rbp,%rdi
call   1080 <CMSG_FIRSTHDR@plt>
movl   $0x10,(%rax)
mov    0x2e13(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x8(%rax)
mov    0x2df7(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rax)
mov    %rax,%rdi
call   1040 <CMSG_DATA@plt>
mov    %ebx,(%rax)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
je     1210 <send_fd+0xb0>
mov    %r15d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r15d
jmp    1202 <send_fd+0xa2>

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

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