#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__ 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 = 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] = -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 = (msghdr.msg_controllen - 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
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    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e16(%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    1200 <ancil_recv_fds_with_buffer+0xd0>
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    11d2 <ancil_recv_fds_with_buffer+0xa2>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
cmp    $0x0,%rax
jge    1224 <ancil_recv_fds_with_buffer+0xf4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <ancil_recv_fds_with_buffer+0x145>
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1260 <ancil_recv_fds_with_buffer+0x130>
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    122b <ancil_recv_fds_with_buffer+0xfb>
movslq -0x4c(%rbp),%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,0x38(%rsp)
movl   $0x1,0x40(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,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    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r13)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r13)
test   %r15d,%r15d
je     122a <ancil_recv_fds_with_buffer+0xfa>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1030 <CMSG_DATA@plt>
movl   $0xffffffff,(%rax,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <ancil_recv_fds_with_buffer+0xa0>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1040 <recvmsg@plt>
test   %rax,%rax
js     1256 <ancil_recv_fds_with_buffer+0x126>
test   %r15d,%r15d
je     123e <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    1210 <ancil_recv_fds_with_buffer+0xe0>
jmp    123e <ancil_recv_fds_with_buffer+0x10e>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
xor    %edx,%edx
call   1040 <recvmsg@plt>
test   %rax,%rax
js     1256 <ancil_recv_fds_with_buffer+0x126>
movslq 0xc(%rsp),%rcx
movabs $0x3fffffff4,%rax
add    %rcx,%rax
shr    $0x2,%rax
jmp    125b <ancil_recv_fds_with_buffer+0x12b>
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 = add i64 12, %26
%28 = trunc i64 %27 to i32
%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 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%34 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* @SOL_SOCKET
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 2
store i32 %35, i32* %37
%38 = load i32, i32* @SCM_RIGHTS
%39 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i32 0, i32 1
store i32 %38, i32* %40
store i32 0, i32* %14
br label %41
41: ; preds = %52, %4
%42 = load i32, i32* %14
%43 = load i32, i32* %8
%44 = icmp ult i32 %42, %43
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%47 = call i64 @CMSG_DATA(%struct.cmsghdr* %46)
%48 = inttoptr i64 %47 to i32*
%49 = load i32, i32* %14
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
store i32 -1, i32* %51
br label %52
52: ; preds = %45
%53 = load i32, i32* %14
%54 = add nsw i32 %53, 1
store i32 %54, i32* %14
br label %41
55: ; preds = %41
%56 = load i32, i32* %6
%57 = call i64 @recvmsg(i32 %56, %struct.msghdr* %10, i32 0)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 -1, i32* %5
br label %88
60: ; preds = %55
store i32 0, i32* %14
br label %61
61: ; preds = %77, %60
%62 = load i32, i32* %14
%63 = load i32, i32* %8
%64 = icmp ult i32 %62, %63
br i1 %64, label %65, label %80
65: ; preds = %61
%66 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%67 = call i64 @CMSG_DATA(%struct.cmsghdr* %66)
%68 = inttoptr i64 %67 to i32*
%69 = load i32, i32* %14
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %68, i64 %70
%72 = load i32, i32* %71
%73 = load i32*, i32** %7
%74 = load i32, i32* %14
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %73, i64 %75
store i32 %72, i32* %76
br label %77
77: ; preds = %65
%78 = load i32, i32* %14
%79 = add nsw i32 %78, 1
store i32 %79, i32* %14
br label %61
80: ; preds = %61
%81 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
%82 = load i32, i32* %81
%83 = sext i32 %82 to i64
%84 = sub i64 %83, 12
%85 = udiv i64 %84, 4
%86 = trunc i64 %85 to i32
store i32 %86, i32* %8
%87 = load i32, i32* %8
store i32 %87, i32* %5
br label %88
88: ; preds = %80, %59
%89 = load i32, i32* %5
ret i32 %89
}

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 = 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 %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* %20
%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
}