#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;
  void *msg_controllen;
  scalar_t__ msg_control;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
  void *cmsg_len;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
void *CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ handle_error(char *);
int /*<<< orphan*/ memmove(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int checked_sendfd(int s, int fd) {
  char buf[1];
  struct iovec iov;
  struct msghdr msg;
  struct cmsghdr *cmsg;
  int n;
  char cms[CMSG_SPACE(sizeof(int))];
  buf[0] = 0;
  iov.iov_base = buf;
  iov.iov_len = 1;
  memset(&msg, 0, sizeof msg);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = (caddr_t)cms;
  msg.msg_controllen = CMSG_LEN(sizeof(int));
  cmsg = CMSG_FIRSTHDR(&msg);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  memmove(CMSG_DATA(cmsg), &fd, sizeof(int));
  if ((n = sendmsg(s, &msg, 0)) != iov.iov_len) {
    handle_error("Could not send file descriptors");
  }
  return 0;
}


x86 assembly (clang, -O0):
<checked_sendfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
movb   $0x0,-0x9(%rbp)
lea    -0x9(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x68(%rbp),%rax
lea    -0x20(%rbp),%rcx
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
mov    %rax,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    0x2dba(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x4c(%rbp)
cmp    -0x18(%rbp),%eax
je     1285 <checked_sendfd+0x105>
lea    0xd80(%rip),%rdi
call   10a0 <handle_error@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_sendfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%r14d
mov    %esi,-0x28(%rbp)
mov    $0x4,%edi
call   1070 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movb   $0x0,-0x21(%rbp)
lea    -0x21(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x58(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x58(%rbp)
mov    %rbx,-0x48(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,-0x50(%rbp)
mov    %r15,%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,0x8(%rbx)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
lea    -0x28(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1080 <memmove@plt>
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    -0x30(%rbp),%eax
je     1262 <checked_sendfd+0xe2>
lea    0xda3(%rip),%rdi
call   10a0 <handle_error@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i64, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i8* }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [32 x i8] c"Could not send file descriptors\00"
define i32 @checked_sendfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i8]
%6 = alloca %struct.iovec
%7 = alloca %struct.msghdr
%8 = alloca %struct.cmsghdr*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%12 = call i32 @CMSG_SPACE(i32 4)
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %10
%15 = alloca i8, i64 %13
store i64 %13, i64* %11
%16 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
store i8 0, i8* %16
%17 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1, i32* %19
%20 = call i32 @memset(%struct.msghdr* %7, i32 0, i32 32)
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 3
store %struct.iovec* %6, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 0
store i32 1, i32* %22
%23 = ptrtoint i8* %15 to i64
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i64 %23, i64* %24
%25 = call i8* @CMSG_LEN(i32 4)
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i8* %25, i8** %26
%27 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
store %struct.cmsghdr* %27, %struct.cmsghdr** %8
%28 = call i8* @CMSG_LEN(i32 4)
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%30 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %29, i32 0, i32 2
store i8* %28, i8** %30
%31 = load i32, i32* @SOL_SOCKET
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 1
store i32 %31, i32* %33
%34 = load i32, i32* @SCM_RIGHTS
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%38 = call i32 @CMSG_DATA(%struct.cmsghdr* %37)
%39 = call i32 @memmove(i32 %38, i32* %4, i32 4)
%40 = load i32, i32* %3
%41 = call i32 @sendmsg(i32 %40, %struct.msghdr* %7, i32 0)
store i32 %41, i32* %9
%42 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
%43 = load i32, i32* %42
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %47
45: ; preds = %2
%46 = call i32 @handle_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %45, %2
%48 = load i8*, i8** %10
ret i32 0
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i64, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i8* }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [32 x i8] c"Could not send file descriptors\00"
define i32 @checked_sendfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [1 x i8]
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
store i32 %1, i32* %3
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.iovec* %5 to i8*
%9 = bitcast %struct.msghdr* %6 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* %7
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %7, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 1, i32* %15
%16 = call i32 @memset(%struct.msghdr* %6, i32 0, i32 32)
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %18
%19 = ptrtoint i8* %13 to i64
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i64 %19, i64* %20
%21 = call i8* @CMSG_LEN(i32 4)
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i8* %21, i8** %22
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%24 = call i8* @CMSG_LEN(i32 4)
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i64 0, i32 2
store i8* %24, i8** %25
%26 = load i32, i32* @SOL_SOCKET
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i64 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @SCM_RIGHTS
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i64 0, i32 0
store i32 %28, i32* %29
%30 = call i32 @CMSG_DATA(%struct.cmsghdr* %23)
%31 = call i32 @memmove(i32 %30, i32* %3, i32 4)
%32 = call i32 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
%33 = load i32, i32* %15
%34 = icmp eq i32 %32, %33
br i1 %34, label %37, label %35
35: ; preds = %2
%36 = call i32 @handle_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %2
ret i32 0
}