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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int /*<<< orphan*/ CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int ancil_send_fds_with_buffer(int sock, const int *fds, unsigned n_fds,
                               void *buffer) {
  struct msghdr msghdr;
  char nothing = '!';
  struct iovec nothing_ptr;
  struct cmsghdr *cmsg;
  int i;
  nothing_ptr.iov_base = &nothing;
  nothing_ptr.iov_len = 1;
  msghdr.msg_name = NULL;
  msghdr.msg_namelen = 0;
  msghdr.msg_iov = &nothing_ptr;
  msghdr.msg_iovlen = 1;
  msghdr.msg_flags = 0;
  msghdr.msg_control = buffer;
  msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds);
  cmsg = CMSG_FIRSTHDR(&msghdr);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  for (i = 0; i < n_fds; i++)
    ((int *)CMSG_DATA(cmsg))[i] = fds[i];
  return (sendmsg(sock, &msghdr, 0) >= 0 ? 0 : -1);
}


x86 assembly (clang, -O0):
<ancil_send_fds_with_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movb   $0x21,-0x51(%rbp)
lea    -0x51(%rbp),%rax
mov    %rax,-0x68(%rbp)
movl   $0x1,-0x60(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x68(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1241 <ancil_send_fds_with_buffer+0xf1>
mov    -0x10(%rbp),%rax
movslq -0x74(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x78(%rbp)
mov    -0x70(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x78(%rbp),%edx
movslq -0x74(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1206 <ancil_send_fds_with_buffer+0xb6>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %rax,%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovge %ecx,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ancil_send_fds_with_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r15d
mov    %rsi,%r13
mov    %edi,0x4(%rsp)
movb   $0x21,0x3(%rsp)
lea    0x3(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x1,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x38(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x1,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    %rcx,0x20(%rsp)
lea    0x0(,%r15,4),%r12d
mov    %r12d,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%r14
mov    %r12d,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,0x8(%r14)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r14)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
test   %r15d,%r15d
je     1219 <ancil_send_fds_with_buffer+0xc9>
mov    %r15d,%r15d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbp,4),%ebx
mov    %r14,%rdi
call   1040 <CMSG_DATA@plt>
mov    %ebx,(%rax,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1200 <ancil_send_fds_with_buffer+0xb0>
lea    0x18(%rsp),%rsi
mov    0x4(%rsp),%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
sar    $0x3f,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i32, i8*, i64, %struct.iovec*, i64, i32* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @ancil_send_fds_with_buffer(i32 %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.msghdr
%10 = alloca i8
%11 = alloca %struct.iovec
%12 = alloca %struct.cmsghdr*
%13 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i8 33, i8* %10
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i8* %10, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 6
store i32* null, i32** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 5
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 4
store %struct.iovec* %11, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store i64 0, i64* %20
%21 = load i8*, i8** %8
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i8* %21, i8** %22
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
%25 = mul i64 4, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @CMSG_SPACE(i32 %26)
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i32 %27, i32* %28
%29 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %9)
store %struct.cmsghdr* %29, %struct.cmsghdr** %12
%30 = load i32, i32* %7
%31 = zext i32 %30 to i64
%32 = mul i64 4, %31
%33 = trunc i64 %32 to i32
%34 = call i32 @CMSG_LEN(i32 %33)
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 2
store i32 %34, i32* %36
%37 = load i32, i32* @SOL_SOCKET
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 1
store i32 %37, i32* %39
%40 = load i32, i32* @SCM_RIGHTS
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 0
store i32 %40, i32* %42
store i32 0, i32* %13
br label %43
43: ; preds = %59, %4
%44 = load i32, i32* %13
%45 = load i32, i32* %7
%46 = icmp ult i32 %44, %45
br i1 %46, label %47, label %62
47: ; preds = %43
%48 = load i32*, i32** %6
%49 = load i32, i32* %13
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i32, i32* %51
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%54 = call i64 @CMSG_DATA(%struct.cmsghdr* %53)
%55 = inttoptr i64 %54 to i32*
%56 = load i32, i32* %13
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
store i32 %52, i32* %58
br label %59
59: ; preds = %47
%60 = load i32, i32* %13
%61 = add nsw i32 %60, 1
store i32 %61, i32* %13
br label %43
62: ; preds = %43
%63 = load i32, i32* %5
%64 = call i64 @sendmsg(i32 %63, %struct.msghdr* %9, i32 0)
%65 = icmp sge i64 %64, 0
%66 = zext i1 %65 to i64
%67 = select i1 %65, i32 0, i32 -1
ret i32 %67
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i32, i8*, i64, %struct.iovec*, i64, i32* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @ancil_send_fds_with_buffer(i32 %0, i32* nocapture %1, i32 %2, i8* %3) {
%5 = alloca %struct.msghdr
%6 = alloca i8
%7 = alloca %struct.iovec
%8 = bitcast %struct.msghdr* %5 to i8*
store i8 33, i8* %6
%9 = bitcast %struct.iovec* %7 to i8*
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 0
store i8* %6, i8** %10
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i64 0, i32 1
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 5
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
%14 = bitcast i64* %12 to i8*
store %struct.iovec* %7, %struct.iovec** %13
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i8* %3, i8** %17
%18 = shl i32 %2, 2
%19 = call i32 @CMSG_SPACE(i32 %18)
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i32 %19, i32* %20
%21 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%22 = call i32 @CMSG_LEN(i32 %18)
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @SOL_SOCKET
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @SCM_RIGHTS
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 0
store i32 %26, i32* %27
%28 = icmp eq i32 %2, 0
br i1 %28, label %40, label %29
29: ; preds = %4
%30 = zext i32 %2 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %38, %31 ]
%33 = getelementptr inbounds i32, i32* %1, i64 %32
%34 = load i32, i32* %33
%35 = call i64 @CMSG_DATA(%struct.cmsghdr* %21)
%36 = inttoptr i64 %35 to i32*
%37 = getelementptr inbounds i32, i32* %36, i64 %32
store i32 %34, i32* %37
%38 = add nuw nsw i64 %32, 1
%39 = icmp eq i64 %38, %30
br i1 %39, label %40, label %31
40: ; preds = %31, %4
%41 = call i64 @sendmsg(i32 %0, %struct.msghdr* %5, i32 0)
%42 = ashr i64 %41, 63
%43 = trunc i64 %42 to i32
ret i32 %43
}