#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);
int ancil_recv_fds_with_buffer_ex(int sock, int *fds, unsigned n_fds,
                                  void *buffer, int flags) {
  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, flags) < 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_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x59(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x1,-0x68(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x58(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
add    $0xc,%rax
mov    %eax,-0x54(%rbp)
lea    -0x58(%rbp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x78(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    -0x78(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1204 <ancil_recv_fds_with_buffer_ex+0xd4>
mov    -0x78(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x7c(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    11d6 <ancil_recv_fds_with_buffer_ex+0xa6>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    -0x58(%rbp),%rsi
call   1040 <recvmsg@plt>
cmp    $0x0,%rax
jge    1229 <ancil_recv_fds_with_buffer_ex+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <ancil_recv_fds_with_buffer_ex+0x14a>
movl   $0x0,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1265 <ancil_recv_fds_with_buffer_ex+0x135>
mov    -0x78(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x7c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x7c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    1230 <ancil_recv_fds_with_buffer_ex+0x100>
movslq -0x54(%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_ex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,%ebx
mov    %edx,%r13d
mov    %rsi,%r14
mov    %edi,%r12d
lea    0x3(%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(,%r13,4),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <CMSG_FIRSTHDR@plt>
mov    %rax,%r15
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%r15)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%r15)
test   %r13d,%r13d
je     122a <ancil_recv_fds_with_buffer_ex+0xfa>
mov    %ebx,0x4(%rsp)
mov    %r13d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <CMSG_DATA@plt>
movl   $0xffffffff,(%rax,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <ancil_recv_fds_with_buffer_ex+0xa0>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    0x4(%rsp),%edx
call   1040 <recvmsg@plt>
test   %rax,%rax
js     1256 <ancil_recv_fds_with_buffer_ex+0x126>
test   %r13d,%r13d
je     123e <ancil_recv_fds_with_buffer_ex+0x10e>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%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_ex+0xe0>
jmp    123e <ancil_recv_fds_with_buffer_ex+0x10e>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    %ebx,%edx
call   1040 <recvmsg@plt>
test   %rax,%rax
js     1256 <ancil_recv_fds_with_buffer_ex+0x126>
movslq 0xc(%rsp),%rcx
movabs $0x3fffffff4,%rax
add    %rcx,%rax
shr    $0x2,%rax
jmp    125b <ancil_recv_fds_with_buffer_ex+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_ex(i32 %0, i32* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.msghdr
%13 = alloca i8
%14 = alloca %struct.iovec
%15 = alloca %struct.cmsghdr*
%16 = alloca i32
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 0
store i8* %13, i8** %17
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 1
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 6
store i32* null, i32** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 5
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 4
store %struct.iovec* %14, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 0
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 3
store i64 0, i64* %23
%24 = load i8*, i8** %10
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 2
store i8* %24, i8** %25
%26 = load i32, i32* %9
%27 = zext i32 %26 to i64
%28 = mul i64 4, %27
%29 = add i64 12, %28
%30 = trunc i64 %29 to i32
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
store i32 %30, i32* %31
%32 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %12)
store %struct.cmsghdr* %32, %struct.cmsghdr** %15
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* @SOL_SOCKET
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 2
store i32 %37, i32* %39
%40 = load i32, i32* @SCM_RIGHTS
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
store i32 %40, i32* %42
store i32 0, i32* %16
br label %43
43: ; preds = %54, %5
%44 = load i32, i32* %16
%45 = load i32, i32* %9
%46 = icmp ult i32 %44, %45
br i1 %46, label %47, label %57
47: ; preds = %43
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%49 = call i64 @CMSG_DATA(%struct.cmsghdr* %48)
%50 = inttoptr i64 %49 to i32*
%51 = load i32, i32* %16
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 -1, i32* %53
br label %54
54: ; preds = %47
%55 = load i32, i32* %16
%56 = add nsw i32 %55, 1
store i32 %56, i32* %16
br label %43
57: ; preds = %43
%58 = load i32, i32* %7
%59 = load i32, i32* %11
%60 = call i64 @recvmsg(i32 %58, %struct.msghdr* %12, i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %57
store i32 -1, i32* %6
br label %91
63: ; preds = %57
store i32 0, i32* %16
br label %64
64: ; preds = %80, %63
%65 = load i32, i32* %16
%66 = load i32, i32* %9
%67 = icmp ult i32 %65, %66
br i1 %67, label %68, label %83
68: ; preds = %64
%69 = load %struct.cmsghdr*, %struct.cmsghdr** %15
%70 = call i64 @CMSG_DATA(%struct.cmsghdr* %69)
%71 = inttoptr i64 %70 to i32*
%72 = load i32, i32* %16
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = load i32*, i32** %8
%77 = load i32, i32* %16
%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* %16
%82 = add nsw i32 %81, 1
store i32 %82, i32* %16
br label %64
83: ; preds = %64
%84 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
%85 = load i32, i32* %84
%86 = sext i32 %85 to i64
%87 = sub i64 %86, 12
%88 = udiv i64 %87, 4
%89 = trunc i64 %88 to i32
store i32 %89, i32* %9
%90 = load i32, i32* %9
store i32 %90, i32* %6
br label %91
91: ; preds = %83, %62
%92 = load i32, i32* %6
ret i32 %92
}

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