#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;
  int /*<<< orphan*/ member_0;
};
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*/ 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;
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_socket(int fd, int sfd) {
  int rc;
  struct iovec iov;
  struct msghdr msg = {0};
  struct cmsghdr *cmsg;
  char buf[CMSG_SPACE(sizeof(int))];
  iov.iov_base = "";
  iov.iov_len = 1;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  *(int *)CMSG_DATA(cmsg) = sfd;
  rc = sendmsg(fd, &msg, 0);
  if (rc == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<send_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x30(%rbp)
movaps %xmm0,-0x40(%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)
lea    0xe65(%rip),%rdx
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
mov    %rcx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2df8(%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    -0xc(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x60(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    123d <send_socket+0xed>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    124b <send_socket+0xfb>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_socket>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r15d
mov    %edi,%r14d
movq   $0x0,-0x30(%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
lea    0xe6a(%rip),%rcx
mov    %rcx,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
lea    -0x48(%rbp),%r12
mov    %r12,%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dff(%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    %r15d,(%rax)
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
sete   %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.iovec = type { i8*, i32 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@.str = [1 x i8] zeroinitializer
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @send_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%13 = bitcast %struct.msghdr* %8 to i8*
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i8* %17, i8** %20
%21 = trunc i64 %15 to i32
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %7, %struct.iovec** %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 1, i32* %24
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %25, %struct.cmsghdr** %9
%26 = load i32, i32* @SOL_SOCKET
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i32 0, i32 2
store i32 %26, i32* %28
%29 = load i32, i32* @SCM_RIGHTS
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 1
store i32 %29, i32* %31
%32 = call i32 @CMSG_LEN(i32 4)
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* %5
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%37 = call i64 @CMSG_DATA(%struct.cmsghdr* %36)
%38 = inttoptr i64 %37 to i32*
store i32 %35, i32* %38
%39 = load i32, i32* %4
%40 = call i32 @sendmsg(i32 %39, %struct.msghdr* %8, i32 0)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %45
44: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %12
br label %45
45: ; preds = %44, %43
%46 = load i8*, i8** %10
%47 = load i32, i32* %3
ret i32 %47
}

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