#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 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 CMSG_LEN(int);
int /*<<< orphan*/ CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int ancil_recv_fds_with_buffer(int sock, 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] = -1;
  if (recvmsg(sock, &msghdr, 0) < 0) {
    return (-1);
  }
  for (i = 0; i < n_fds; i++) {
    fds[i] = ((int *)CMSG_DATA(cmsg))[i];
  }
  n_fds = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
  return (n_fds);
}


x86 assembly (clang, -O0):
<ancil_recv_fds_with_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%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   1050 <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   1040 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2de6(%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    1230 <ancil_recv_fds_with_buffer+0xe0>
mov    -0x70(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x74(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1202 <ancil_recv_fds_with_buffer+0xb2>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0x0,%rax
jge    1254 <ancil_recv_fds_with_buffer+0x104>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <ancil_recv_fds_with_buffer+0x158>
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1290 <ancil_recv_fds_with_buffer+0x140>
mov    -0x70(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x74(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x74(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    125b <ancil_recv_fds_with_buffer+0x10b>
mov    -0x70(%rbp),%rax
movslq (%rax),%rax
sub    $0xc,%rax
shr    $0x2,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ancil_recv_fds_with_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%r12d
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    0x0(,%r15,4),%ebp
mov    %ebp,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%r13
mov    %ebp,%edi
call   1040 <CMSG_LEN@plt>
mov    %eax,0x0(%r13)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r13)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r13)
test   %r15d,%r15d
je     124a <ancil_recv_fds_with_buffer+0xfa>
mov    %r15d,%ebp
xor    %ebx,%ebx
mov    %r13,%rdi
call   1030 <CMSG_DATA@plt>
movl   $0xffffffff,(%rax,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11f0 <ancil_recv_fds_with_buffer+0xa0>
lea    0x18(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %rax,%rax
js     1275 <ancil_recv_fds_with_buffer+0x125>
test   %r15d,%r15d
je     125e <ancil_recv_fds_with_buffer+0x10e>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax,%rbx,4),%eax
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1230 <ancil_recv_fds_with_buffer+0xe0>
jmp    125e <ancil_recv_fds_with_buffer+0x10e>
lea    0x18(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %rax,%rax
js     1275 <ancil_recv_fds_with_buffer+0x125>
movslq 0x0(%r13),%rcx
movabs $0x3fffffff4,%rax
add    %rcx,%rax
shr    $0x2,%rax
jmp    127a <ancil_recv_fds_with_buffer+0x12a>
mov    $0xffffffff,%eax
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_recv_fds_with_buffer(i32 %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.msghdr
%11 = alloca i8
%12 = alloca %struct.iovec
%13 = alloca %struct.cmsghdr*
%14 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 0
store i8* %11, i8** %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 1
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 6
store i32* null, i32** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 5
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 4
store %struct.iovec* %12, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i32 1, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store i64 0, i64* %21
%22 = load i8*, i8** %9
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i8* %22, i8** %23
%24 = load i32, i32* %8
%25 = zext i32 %24 to i64
%26 = mul i64 4, %25
%27 = trunc i64 %26 to i32
%28 = call i32 @CMSG_SPACE(i32 %27)
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i32 %28, i32* %29
%30 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %10)
store %struct.cmsghdr* %30, %struct.cmsghdr** %13
%31 = load i32, i32* %8
%32 = zext i32 %31 to i64
%33 = mul i64 4, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @CMSG_LEN(i32 %34)
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* @SOL_SOCKET
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 2
store i32 %38, i32* %40
%41 = load i32, i32* @SCM_RIGHTS
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 1
store i32 %41, i32* %43
store i32 0, i32* %14
br label %44
44: ; preds = %55, %4
%45 = load i32, i32* %14
%46 = load i32, i32* %8
%47 = icmp ult i32 %45, %46
br i1 %47, label %48, label %58
48: ; preds = %44
%49 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%50 = call i64 @CMSG_DATA(%struct.cmsghdr* %49)
%51 = inttoptr i64 %50 to i32*
%52 = load i32, i32* %14
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
store i32 -1, i32* %54
br label %55
55: ; preds = %48
%56 = load i32, i32* %14
%57 = add nsw i32 %56, 1
store i32 %57, i32* %14
br label %44
58: ; preds = %44
%59 = load i32, i32* %6
%60 = call i64 @recvmsg(i32 %59, %struct.msghdr* %10, i32 0)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 -1, i32* %5
br label %92
63: ; preds = %58
store i32 0, i32* %14
br label %64
64: ; preds = %80, %63
%65 = load i32, i32* %14
%66 = load i32, i32* %8
%67 = icmp ult i32 %65, %66
br i1 %67, label %68, label %83
68: ; preds = %64
%69 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%70 = call i64 @CMSG_DATA(%struct.cmsghdr* %69)
%71 = inttoptr i64 %70 to i32*
%72 = load i32, i32* %14
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = load i32*, i32** %7
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %76, i64 %78
store i32 %75, i32* %79
br label %80
80: ; preds = %68
%81 = load i32, i32* %14
%82 = add nsw i32 %81, 1
store i32 %82, i32* %14
br label %64
83: ; preds = %64
%84 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%85 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = sext i32 %86 to i64
%88 = sub i64 %87, 12
%89 = udiv i64 %88, 4
%90 = trunc i64 %89 to i32
store i32 %90, i32* %8
%91 = load i32, i32* %8
store i32 %91, i32* %5
br label %92
92: ; preds = %83, %62
%93 = load i32, i32* %5
ret i32 %93
}

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_recv_fds_with_buffer(i32 %0, i32* nocapture writeonly %1, i32 %2, i8* %3) {
%5 = alloca %struct.msghdr
%6 = alloca i8
%7 = alloca %struct.iovec
%8 = bitcast %struct.msghdr* %5 to i8*
%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 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 %41, label %29
29: ; preds = %4
%30 = zext i32 %2 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %36, %31 ]
%33 = call i64 @CMSG_DATA(%struct.cmsghdr* %21)
%34 = inttoptr i64 %33 to i32*
%35 = getelementptr inbounds i32, i32* %34, i64 %32
store i32 -1, i32* %35
%36 = add nuw nsw i64 %32, 1
%37 = icmp eq i64 %36, %30
br i1 %37, label %38, label %31
38: ; preds = %31
%39 = call i64 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %62, label %44
41: ; preds = %4
%42 = call i64 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%43 = icmp slt i64 %42, 0
br i1 %43, label %62, label %56
44: ; preds = %38
br i1 %28, label %56, label %45
45: ; preds = %44
%46 = zext i32 %2 to i64
br label %47
47: ; preds = %45, %47
%48 = phi i64 [ 0, %45 ], [ %54, %47 ]
%49 = call i64 @CMSG_DATA(%struct.cmsghdr* %21)
%50 = inttoptr i64 %49 to i32*
%51 = getelementptr inbounds i32, i32* %50, i64 %48
%52 = load i32, i32* %51
%53 = getelementptr inbounds i32, i32* %1, i64 %48
store i32 %52, i32* %53
%54 = add nuw nsw i64 %48, 1
%55 = icmp eq i64 %54, %46
br i1 %55, label %56, label %47
56: ; preds = %47, %41, %44
%57 = load i32, i32* %23
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, 17179869172
%60 = lshr i64 %59, 2
%61 = trunc i64 %60 to i32
br label %62
62: ; preds = %41, %38, %56
%63 = phi i32 [ %61, %56 ], [ -1, %38 ], [ -1, %41 ]
ret i32 %63
}