#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 {
  int msg_iovlen;
  char *msg_control;
  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*/ fd_buf;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void send_fd(int dest, int fd_to_send) {
  char dummy = 'x';
  struct iovec dummy_vec = {.iov_base = &dummy, .iov_len = 1};
  char fd_buf[CMSG_SPACE(sizeof(int))];
  struct msghdr message = {.msg_iov = &dummy_vec,
                           .msg_iovlen = 1,
                           .msg_control = fd_buf,
                           .msg_controllen = sizeof(fd_buf)};
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&message);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  message.msg_controllen = cmsg->cmsg_len;
  memcpy(CMSG_DATA(cmsg), &fd_to_send, sizeof(int));
  if (sendmsg(dest, &message, 0) < 0)
    do {
      printf("[-] "
             "sendmsg"
             ": %s"
             "\n",
             strerror(errno));
      exit(57);
    } while (0);
}


x86 assembly (clang, -O0):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movb   $0x78,-0x9(%rbp)
lea    -0x9(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dba(%rip),%rax
mov    (%rax),%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x0,%rax
jge    1297 <send_fd+0x107>
jmp    1270 <send_fd+0xe0>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd78(%rip),%rdi
call   1050 <printf@plt>
mov    $0x39,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
mov    %esi,-0x28(%rbp)
movb   $0x78,-0x21(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
movl   $0x1,-0x48(%rbp)
mov    %rdx,-0x40(%rbp)
mov    %eax,-0x38(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x48(%rbp),%r15
mov    %r15,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %eax,-0x38(%rbp)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x28(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
js     125e <send_fd+0xce>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8d(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    $0x39,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [17 x i8] c"[-] sendmsg: %s\0A\00"
@errno = global i32 0
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca %struct.iovec
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.msghdr
%10 = alloca %struct.cmsghdr*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8 120, i8* %5
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* %5, i8** %11
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1, i32* %12
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 1, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i8* %16, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
%20 = trunc i64 %14 to i32
store i32 %20, i32* %19
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %21
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %9)
store %struct.cmsghdr* %22, %struct.cmsghdr** %10
%23 = load i32, i32* @SOL_SOCKET
%24 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%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** %10
%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** %10
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i32 %34, i32* %35
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %10
%37 = call i32 @CMSG_DATA(%struct.cmsghdr* %36)
%38 = call i32 @memcpy(i32 %37, i32* %4, i32 4)
%39 = load i32, i32* %3
%40 = call i64 @sendmsg(i32 %39, %struct.msghdr* %9, i32 0)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %2
br label %43
43: ; preds = %42
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 57)
unreachable
48: ; No predecessors!
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49, %2
%51 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [17 x i8] c"[-] sendmsg: %s\0A\00"
@errno = global i32 0
define void @send_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
store i32 %1, i32* %3
store i8 120, i8* %4
%7 = bitcast %struct.iovec* %5 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %4, i8** %8
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 1, i32* %9
%10 = call i32 @CMSG_SPACE(i32 4)
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = bitcast %struct.msghdr* %6 to i8*
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* %13, i8** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 %10, i32* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %18
%19 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%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
store i32 %24, i32* %17
%26 = call i32 @CMSG_DATA(%struct.cmsghdr* %19)
%27 = call i32 @memcpy(i32 %26, i32* %3, i32 4)
%28 = call i64 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %2
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 57)
unreachable
35: ; preds = %2
ret void
}