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

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int p, int fd) {
  struct msghdr hdr;
  struct iovec iov;
  struct cmsghdr *cmsg;
  char buf[((((sizeof(struct cmsghdr)) + sizeof(intptr_t) - 1) &
             ~(sizeof(intptr_t) - 1)) +
            (((sizeof(fd)) + sizeof(intptr_t) - 1) & ~(sizeof(intptr_t) - 1)))];
  int val = 0;
  hdr.msg_name = NULL;
  hdr.msg_namelen = 0;
  hdr.msg_iov = &iov;
  hdr.msg_iovlen = 1;
  hdr.msg_control = buf;
  hdr.msg_controllen = sizeof(buf);
  iov.iov_base = &val;
  iov.iov_len = sizeof(val);
  cmsg = CMSG_FIRSTHDR(&hdr);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = ((((sizeof(struct cmsghdr)) + sizeof(intptr_t) - 1) &
                     ~(sizeof(intptr_t) - 1)) +
                    (sizeof(fd)));
  memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
  hdr.msg_controllen = cmsg->cmsg_len;
  return sendmsg(p, &hdr, 0) == sizeof(val) ? 0 : -1;
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x74(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x18,-0x28(%rbp)
lea    -0x74(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x4,-0x40(%rbp)
lea    -0x38(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x50(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x50(%rbp),%rax
movl   $0x14,(%rax)
mov    -0x50(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
movslq %eax,%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x4,%rdx
cmove  %ecx,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %edi,%ebp
mov    %esi,0xc(%rsp)
movl   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x40(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x38(%rsp)
movl   $0x1,0x20(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x18,0x30(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x4,0x18(%rsp)
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
movl   $0x14,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    (%rbx),%eax
mov    %eax,0x30(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
xor    %ecx,%ecx
cmp    $0x4,%eax
setne  %cl
neg    %ecx
mov    %ecx,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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