#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;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ c;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_fd(int via, int fd) {
  struct msghdr h = {};
  struct cmsghdr *ch;
  struct iovec iov;
  char buf[CMSG_SPACE(sizeof(int))], c = '\0';
  int *fdp;
  h.msg_control = buf;
  h.msg_controllen = sizeof(buf);
  ch = CMSG_FIRSTHDR(&h);
  ch->cmsg_level = SOL_SOCKET;
  ch->cmsg_type = SCM_RIGHTS;
  ch->cmsg_len = CMSG_LEN(sizeof(int));
  fdp = (int *)CMSG_DATA(ch);
  *fdp = fd;
  h.msg_iov = &iov;
  h.msg_iovlen = 1;
  iov.iov_base = &c;
  iov.iov_len = sizeof(c);
  if (sendmsg(via, &h, 0) <= 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
movb   $0x0,-0x59(%rbp)
mov    %rcx,-0x30(%rbp)
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x38(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %rax,-0x68(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x68(%rbp),%rax
mov    %ecx,(%rax)
lea    -0x48(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
lea    -0x59(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jg     1241 <send_fd+0xf1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    124f <send_fd+0xff>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x50(%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    $0x38,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movups %xmm0,-0x40(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r13
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
movb   $0x0,-0x29(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x48(%rbp),%r15
mov    %r15,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r12d,(%rax)
lea    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
lea    -0x29(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
xor    %ecx,%ecx
test   %rax,%rax
setle  %cl
neg    %ecx
mov    %r13,%rsp
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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
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 %struct.iovec
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i32*
%13 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%14 = bitcast %struct.msghdr* %6 to i8*
%15 = call i32 @CMSG_SPACE(i32 4)
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %9
%18 = alloca i8, i64 %16
store i64 %16, i64* %10
store i8 0, i8* %11
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i8* %18, i8** %19
%20 = trunc i64 %16 to i32
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 %20, i32* %21
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
store %struct.cmsghdr* %22, %struct.cmsghdr** %7
%23 = load i32, i32* @SOL_SOCKET
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%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** %7
%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** %7
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%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 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store %struct.iovec* %8, %struct.iovec** %37
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
store i32 1, i32* %38
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
store i8* %11, i8** %39
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
store i32 1, i32* %40
%41 = load i32, i32* %4
%42 = call i64 @sendmsg(i32 %41, %struct.msghdr* %6, i32 0)
%43 = icmp sle i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %13
br label %46
45: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %46
46: ; preds = %45, %44
%47 = load i8*, i8** %9
%48 = load i32, i32* %3
ret i32 %48
}

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
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 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
%8 = bitcast i32* %7 to i8*
%9 = bitcast %struct.iovec* %4 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* %5
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 %10, i32* %15
%16 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%17 = load i32, i32* @SOL_SOCKET
%18 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %16, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @SCM_RIGHTS
%20 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %16, i64 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @CMSG_LEN(i32 4)
%22 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %16, i64 0, i32 0
store i32 %21, i32* %22
%23 = call i64 @CMSG_DATA(%struct.cmsghdr* %16)
%24 = inttoptr i64 %23 to i32*
store i32 %1, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %4, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %5, i8** %27
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i32 1, i32* %28
%29 = call i64 @sendmsg(i32 %0, %struct.msghdr* %3, i32 0)
%30 = icmp slt i64 %29, 1
%31 = sext i1 %30 to i32
ret i32 %31
}