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

/* 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_to(int sock, const int *fds, unsigned n_fds,
                                  void *buffer, struct sockaddr *to,
                                  socklen_t tolen) {
  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 = to;
  msghdr.msg_namelen = tolen;
  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_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movb   $0x21,-0x61(%rbp)
lea    -0x61(%rbp),%rax
mov    %rax,-0x78(%rbp)
movl   $0x1,-0x70(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x78(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x60(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
add    $0xc,%rax
mov    %eax,-0x5c(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x80(%rbp)
mov    -0x5c(%rbp),%ecx
mov    -0x80(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    -0x80(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    -0x80(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x84(%rbp)
mov    -0x84(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    122f <ancil_send_fds_with_buffer_to+0xff>
mov    -0x10(%rbp),%rax
movslq -0x84(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x88(%rbp)
mov    -0x80(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x88(%rbp),%edx
movslq -0x84(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11df <ancil_send_fds_with_buffer_to+0xaf>
mov    -0x4(%rbp),%edi
lea    -0x60(%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    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ancil_send_fds_with_buffer_to>:
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,0x38(%rsp)
movl   $0x1,0x40(%rsp)
mov    %r8,0x30(%rsp)
mov    %r9d,0x28(%rsp)
lea    0x38(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    %rcx,0x10(%rsp)
lea    0xc(,%r15,4),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,%r13
mov    0xc(%rsp),%eax
mov    %eax,0x0(%r13)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r13)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r13)
test   %r15d,%r15d
je     11e8 <ancil_send_fds_with_buffer_to+0xb8>
mov    %r15d,%r15d
xor    %ebp,%ebp
nopl   (%rax)
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_to+0xa0>
lea    0x8(%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.sockaddr = type { i32 }
%struct.msghdr = type { i32, i32, i8*, i64, %struct.iovec*, i32, %struct.sockaddr* }
%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_to(i32 %0, i32* %1, i32 %2, i8* %3, %struct.sockaddr* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.sockaddr*
%12 = alloca i32
%13 = alloca %struct.msghdr
%14 = alloca i8
%15 = alloca %struct.iovec
%16 = alloca %struct.cmsghdr*
%17 = alloca i32
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store %struct.sockaddr* %4, %struct.sockaddr** %11
store i32 %5, i32* %12
store i8 33, i8* %14
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 0
store i8* %14, i8** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 1
store i32 1, i32* %19
%20 = load %struct.sockaddr*, %struct.sockaddr** %11
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 6
store %struct.sockaddr* %20, %struct.sockaddr** %21
%22 = load i32, i32* %12
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 5
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 4
store %struct.iovec* %15, %struct.iovec** %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 0
store i32 1, i32* %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
store i64 0, i64* %26
%27 = load i8*, i8** %10
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i8* %27, i8** %28
%29 = load i32, i32* %9
%30 = zext i32 %29 to i64
%31 = mul i64 4, %30
%32 = add i64 12, %31
%33 = trunc i64 %32 to i32
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
store i32 %33, i32* %34
%35 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %13)
store %struct.cmsghdr* %35, %struct.cmsghdr** %16
%36 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* @SOL_SOCKET
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 2
store i32 %40, i32* %42
%43 = load i32, i32* @SCM_RIGHTS
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%45 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %44, i32 0, i32 1
store i32 %43, i32* %45
store i32 0, i32* %17
br label %46
46: ; preds = %62, %6
%47 = load i32, i32* %17
%48 = load i32, i32* %9
%49 = icmp ult i32 %47, %48
br i1 %49, label %50, label %65
50: ; preds = %46
%51 = load i32*, i32** %8
%52 = load i32, i32* %17
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = load %struct.cmsghdr*, %struct.cmsghdr** %16
%57 = call i64 @CMSG_DATA(%struct.cmsghdr* %56)
%58 = inttoptr i64 %57 to i32*
%59 = load i32, i32* %17
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
store i32 %55, i32* %61
br label %62
62: ; preds = %50
%63 = load i32, i32* %17
%64 = add nsw i32 %63, 1
store i32 %64, i32* %17
br label %46
65: ; preds = %46
%66 = load i32, i32* %7
%67 = call i64 @sendmsg(i32 %66, %struct.msghdr* %13, i32 0)
%68 = icmp sge i64 %67, 0
%69 = zext i1 %68 to i64
%70 = select i1 %68, i32 0, i32 -1
ret i32 %70
}

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