#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__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int ancil_recv_fds_with_buffer_from(int sock, int *fds, unsigned n_fds,
                                    void *buffer, struct sockaddr *from,
                                    socklen_t fromlen) {
  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 = from;
  msghdr.msg_namelen = fromlen;
  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 = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
  return (n_fds);
}


x86 assembly (clang, -O0):
<ancil_recv_fds_with_buffer_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%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    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    -0x80(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e10(%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    1215 <ancil_recv_fds_with_buffer_from+0xe5>
mov    -0x80(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x84(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11db <ancil_recv_fds_with_buffer_from+0xab>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
cmp    $0x0,%rax
jge    1239 <ancil_recv_fds_with_buffer_from+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <ancil_recv_fds_with_buffer_from+0x16f>
movl   $0x0,-0x84(%rbp)
mov    -0x84(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1287 <ancil_recv_fds_with_buffer_from+0x157>
mov    -0x80(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
movslq -0x84(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x84(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    1243 <ancil_recv_fds_with_buffer_from+0x113>
mov    -0x80(%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    $0x90,%rsp
pop    %rbp
ret

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

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