#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 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 cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
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 = sizeof(struct cmsghdr) + sizeof(int) * n_fds;
  cmsg = CMSG_FIRSTHDR(&msghdr);
  cmsg->cmsg_len = msghdr.msg_controllen;
  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
add    $0xc,%rax
mov    %eax,-0x4c(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x70(%rbp)
mov    -0x4c(%rbp),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1211 <ancil_send_fds_with_buffer+0xe1>
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    11d6 <ancil_send_fds_with_buffer+0xa6>
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,%r12
mov    %edi,%r14d
movb   $0x21,0x7(%rsp)
lea    0x7(%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    0xc(,%r15,4),%eax
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,%r13
mov    0x1c(%rsp),%eax
mov    %eax,0x0(%r13)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r13)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r13)
test   %r15d,%r15d
je     11e8 <ancil_send_fds_with_buffer+0xb8>
mov    %r15d,%r15d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbp,4),%ebx
mov    %r13,%rdi
call   1040 <CMSG_DATA@plt>
mov    %ebx,(%rax,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11d0 <ancil_send_fds_with_buffer+0xa0>
lea    0x18(%rsp),%rsi
mov    %r14d,%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 = add i64 12, %25
%27 = trunc i64 %26 to i32
%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 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* @SOL_SOCKET
%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* @SCM_RIGHTS
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 1
store i32 %37, i32* %39
store i32 0, i32* %13
br label %40
40: ; preds = %56, %4
%41 = load i32, i32* %13
%42 = load i32, i32* %7
%43 = icmp ult i32 %41, %42
br i1 %43, label %44, label %59
44: ; preds = %40
%45 = load i32*, i32** %6
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%51 = call i64 @CMSG_DATA(%struct.cmsghdr* %50)
%52 = inttoptr i64 %51 to i32*
%53 = load i32, i32* %13
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
store i32 %49, i32* %55
br label %56
56: ; preds = %44
%57 = load i32, i32* %13
%58 = add nsw i32 %57, 1
store i32 %58, i32* %13
br label %40
59: ; preds = %40
%60 = load i32, i32* %5
%61 = call i64 @sendmsg(i32 %60, %struct.msghdr* %9, i32 0)
%62 = icmp sge i64 %61, 0
%63 = zext i1 %62 to i64
%64 = select i1 %62, i32 0, i32 -1
ret i32 %64
}

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 = add i32 %18, 12
%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 = load i32, i32* %20
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @SOL_SOCKET
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @SCM_RIGHTS
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 1
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
}