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

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
size_t CMSG_LEN(int);
size_t CMSG_SPACE(int);
scalar_t__ EINTR;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int un_send_fds(int fd, char *data, int datalen, int *fds, int fdslen) {
  struct msghdr mh;
  struct cmsghdr *cmh = NULL;
  char buf[2048];
  size_t buflen = CMSG_SPACE(sizeof(int) * fdslen);
  struct iovec iov[1];
  assert(sizeof(buf) >= buflen);
  memset(&mh, 0, sizeof(mh));
  mh.msg_control = buf;
  mh.msg_controllen = buflen;
  mh.msg_iov = iov;
  mh.msg_iovlen = 1;
  iov[0].iov_base = data;
  iov[0].iov_len = datalen;
  cmh = CMSG_FIRSTHDR(&mh);
  cmh->cmsg_level = SOL_SOCKET;
  cmh->cmsg_type = SCM_RIGHTS;
  cmh->cmsg_len = CMSG_LEN(sizeof(int) * fdslen);
  memcpy(CMSG_DATA(cmh), fds, sizeof(int) * fdslen);
  mh.msg_controllen = cmh->cmsg_len;
  int rv;
  do {
    rv = sendmsg(fd, &mh, 0);
  } while (rv == -1 && errno == EINTR);
  return rv;
}


x86 assembly (clang, -O0):
<un_send_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x880,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x0,-0x50(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <CMSG_SPACE@plt>
mov    %rax,-0x858(%rbp)
mov    $0x800,%eax
cmp    -0x858(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x850(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x858(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x870(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x870(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x868(%rbp)
lea    -0x48(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x50(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x2d8c(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x8(%rax)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x50(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x874(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x874(%rbp)
mov    %al,-0x875(%rbp)
jne    12d5 <un_send_fds+0x155>
mov    0x2d19(%rip),%rax
mov    (%rax),%rax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x875(%rbp)
mov    -0x875(%rbp),%al
test   $0x1,%al
jne    128f <un_send_fds+0x10f>
mov    -0x874(%rbp),%eax
add    $0x880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<un_send_fds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
mov    %rcx,0x8(%rsp)
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r13d
lea    0x0(,%r8,4),%r12d
mov    %r12d,%edi
call   1080 <CMSG_SPACE@plt>
mov    %rax,%r15
xor    %edi,%edi
cmp    $0x801,%rax
setb   %dil
call   1070 <assert@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    0x40(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    %r15,0x28(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x38(%rsp)
movl   $0x1,0x30(%rsp)
mov    %rbx,0x10(%rsp)
mov    %ebp,0x18(%rsp)
mov    %r14,%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbp
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbp)
mov    %r12d,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,0x0(%rbp)
mov    %rbp,%rdi
call   1040 <CMSG_DATA@plt>
mov    %eax,%edi
mov    0x8(%rsp),%rsi
mov    %r12d,%edx
call   1090 <memcpy@plt>
mov    0x0(%rbp),%rax
mov    %rax,0x28(%rsp)
mov    0x2d82(%rip),%rbp
mov    0x2d6b(%rip),%rbx
nopl   (%rax)
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
jne    127b <un_send_fds+0xfb>
mov    (%rbx),%rcx
cmp    %rcx,0x0(%rbp)
je     1260 <un_send_fds+0xe0>
add    $0x848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i8*, i64, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i64, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @un_send_fds(i32 %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.msghdr
%12 = alloca %struct.cmsghdr*
%13 = alloca [2048 x i8]
%14 = alloca i64
%15 = alloca [1 x %struct.iovec]
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
store %struct.cmsghdr* null, %struct.cmsghdr** %12
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = mul i64 4, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @CMSG_SPACE(i32 %20)
store i64 %21, i64* %14
%22 = load i64, i64* %14
%23 = icmp uge i64 2048, %22
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = call i32 @memset(%struct.msghdr* %11, i32 0, i32 32)
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %13, i64 0, i64 0
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 0
store i8* %27, i8** %28
%29 = load i64, i64* %14
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 1
store i64 %29, i64* %30
%31 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %15, i64 0, i64 0
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 3
store %struct.iovec* %31, %struct.iovec** %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 2
store i32 1, i32* %33
%34 = load i8*, i8** %7
%35 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %15, i64 0, i64 0
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i32 0, i32 0
store i8* %34, i8** %36
%37 = load i32, i32* %8
%38 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %15, i64 0, i64 0
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 1
store i32 %37, i32* %39
%40 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %11)
store %struct.cmsghdr* %40, %struct.cmsghdr** %12
%41 = load i32, i32* @SOL_SOCKET
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 2
store i32 %41, i32* %43
%44 = load i32, i32* @SCM_RIGHTS
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%46 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %45, i32 0, i32 1
store i32 %44, i32* %46
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = mul i64 4, %48
%50 = trunc i64 %49 to i32
%51 = call i64 @CMSG_LEN(i32 %50)
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%53 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %52, i32 0, i32 0
store i64 %51, i64* %53
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%55 = call i32 @CMSG_DATA(%struct.cmsghdr* %54)
%56 = load i32*, i32** %9
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = mul i64 4, %58
%60 = trunc i64 %59 to i32
%61 = call i32 @memcpy(i32 %55, i32* %56, i32 %60)
%62 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%63 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %11, i32 0, i32 1
store i64 %64, i64* %65
br label %66
66: ; preds = %76, %5
%67 = load i32, i32* %6
%68 = call i32 @sendmsg(i32 %67, %struct.msghdr* %11, i32 0)
store i32 %68, i32* %16
br label %69
69: ; preds = %66
%70 = load i32, i32* %16
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i64, i64* @errno
%74 = load i64, i64* @EINTR
%75 = icmp eq i64 %73, %74
br label %76
76: ; preds = %72, %69
%77 = phi i1 [ false, %69 ], [ %75, %72 ]
br i1 %77, label %66, label %78
78: ; preds = %76
%79 = load i32, i32* %16
ret i32 %79
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i64, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i64, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @un_send_fds(i32 %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca %struct.msghdr
%7 = alloca [2048 x i8]
%8 = alloca [1 x %struct.iovec]
%9 = bitcast %struct.msghdr* %6 to i8*
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%11 = shl i32 %4, 2
%12 = tail call i64 @CMSG_SPACE(i32 %11)
%13 = bitcast [1 x %struct.iovec]* %8 to i8*
%14 = icmp ult i64 %12, 2049
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = call i32 @memset(%struct.msghdr* %6, i32 0, i32 32)
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i8* %10, i8** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i64 %12, i64* %19
%20 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %20, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
store i32 1, i32* %22
%23 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0, i32 0
store i8* %1, i8** %23
%24 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0, i32 1
store i32 %2, i32* %24
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
%26 = load i32, i32* @SOL_SOCKET
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @SCM_RIGHTS
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 1
store i32 %28, i32* %29
%30 = call i64 @CMSG_LEN(i32 %11)
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i64 0, i32 0
store i64 %30, i64* %31
%32 = call i32 @CMSG_DATA(%struct.cmsghdr* %25)
%33 = call i32 @memcpy(i32 %32, i32* %3, i32 %11)
%34 = load i64, i64* %31
store i64 %34, i64* %19
br label %35
35: ; preds = %35, %5
%36 = call i32 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
%37 = icmp eq i32 %36, -1
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
%41 = select i1 %37, i1 %40, i1 false
br i1 %41, label %35, label %42
42: ; preds = %35
ret i32 %36
}