#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;
  char *msg_control;
  int msg_controllen;
  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 {
  scalar_t__ cmsg_len;
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ extrabuf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
scalar_t__ CMSG_LEN(int);
int CMSG_SPACE(int);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int _recvfd(int sock, size_t *len, void *buf) {
  struct iovec iov[1];
  struct msghdr msgh;
  char cmsgbuf[CMSG_SPACE(sizeof(int))];
  char extrabuf[4096];
  struct cmsghdr *h;
  int st, fd;
  if (*len < 1 || buf == NULL) {
    iov[0].iov_base = extrabuf;
    iov[0].iov_len = sizeof(extrabuf);
  } else {
    iov[0].iov_base = buf;
    iov[0].iov_len = *len;
  }
  msgh.msg_name = NULL;
  msgh.msg_namelen = 0;
  msgh.msg_iov = iov;
  msgh.msg_iovlen = 1;
  msgh.msg_control = cmsgbuf;
  msgh.msg_controllen = CMSG_SPACE(sizeof(int));
  msgh.msg_flags = 0;
  st = recvmsg(sock, &msgh, 0);
  if (st < 0) {
    return -1;
  }
  *len = st;
  h = CMSG_FIRSTHDR(&msgh);
  if ((h == NULL) || h->cmsg_len != CMSG_LEN(sizeof(int)) ||
      h->cmsg_level != SOL_SOCKET || h->cmsg_type != SCM_RIGHTS) {
    return -2;
  }
  fd = ((int *)CMSG_DATA(h))[0];
  if (fd < 0)
    return -3;
  return fd;
}


x86 assembly (clang, -O0):
<_recvfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10b0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x10a0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x1,(%rax)
jb     11b1 <_recvfd+0x61>
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <_recvfd+0x78>
lea    -0x1080(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1000,-0x28(%rbp)
jmp    11da <_recvfd+0x8a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x28(%rbp)
mov    -0x10a0(%rbp),%rax
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x30(%rbp),%rcx
mov    %rcx,-0x48(%rbp)
movl   $0x1,-0x68(%rbp)
mov    %rax,-0x60(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x68(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %eax,-0x108c(%rbp)
cmpl   $0x0,-0x108c(%rbp)
jge    1250 <_recvfd+0x100>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x1094(%rbp)
jmp    1340 <_recvfd+0x1f0>
movslq -0x108c(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
lea    -0x68(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x1088(%rbp)
cmpq   $0x0,-0x1088(%rbp)
je     12e0 <_recvfd+0x190>
mov    -0x1088(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10a8(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x10a8(%rbp),%rax
cmp    %rcx,%rax
jne    12e0 <_recvfd+0x190>
mov    -0x1088(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <_recvfd+0x190>
mov    -0x1088(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
je     12f6 <_recvfd+0x1a6>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x1094(%rbp)
jmp    1340 <_recvfd+0x1f0>
mov    -0x1088(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x1090(%rbp)
cmpl   $0x0,-0x1090(%rbp)
jge    132d <_recvfd+0x1dd>
movl   $0xfffffffd,-0x4(%rbp)
movl   $0x1,-0x1094(%rbp)
jmp    1340 <_recvfd+0x1f0>
mov    -0x1090(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x1094(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_recvfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1058,%rsp
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,%rsp
mov    (%r15),%rax
test   %rax,%rax
sete   %dl
test   %r13,%r13
sete   %bl
or     %dl,%bl
lea    -0x1080(%rbp),%rdx
cmove  %r13,%rdx
mov    $0x1000,%esi
cmove  %eax,%esi
mov    %rdx,-0x40(%rbp)
mov    %esi,-0x38(%rbp)
xorps  %xmm0,%xmm0
movups %xmm0,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x78(%rbp)
mov    %rcx,-0x70(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,-0x68(%rbp)
movq   $0x0,-0x60(%rbp)
lea    -0x78(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %eax,%eax
js     125f <_recvfd+0x10f>
mov    %eax,%eax
mov    %rax,(%r15)
lea    -0x78(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    $0xfffffffe,%r14d
test   %rax,%rax
je     1265 <_recvfd+0x115>
mov    %rax,%rbx
mov    (%rax),%r15
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
cmp    %rax,%r15
jne    1265 <_recvfd+0x115>
mov    0x8(%rbx),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <_recvfd+0x115>
mov    0x10(%rbx),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <_recvfd+0x115>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
test   %eax,%eax
mov    $0xfffffffd,%r14d
cmovns %eax,%r14d
jmp    1265 <_recvfd+0x115>
mov    $0xffffffff,%r14d
mov    %r12,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
%struct.cmsghdr = type { i64, i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @_recvfd(i32 %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca [1 x %struct.iovec]
%9 = alloca %struct.msghdr
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [4096 x i8]
%13 = alloca %struct.cmsghdr*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %5
store i64* %1, i64** %6
store i8* %2, i8** %7
%17 = call i32 @CMSG_SPACE(i32 4)
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
%21 = load i64*, i64** %6
%22 = load i64, i64* %21
%23 = icmp ult i64 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %3
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %33
27: ; preds = %24, %3
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%29 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
store i8* %28, i8** %30
%31 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%32 = getelementptr inbounds %struct.iovec, %struct.iovec* %31, i32 0, i32 1
store i32 4096, i32* %32
br label %42
33: ; preds = %24
%34 = load i8*, i8** %7
%35 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i32 0, i32 0
store i8* %34, i8** %36
%37 = load i64*, i64** %6
%38 = load i64, i64* %37
%39 = trunc i64 %38 to i32
%40 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%41 = getelementptr inbounds %struct.iovec, %struct.iovec* %40, i32 0, i32 1
store i32 %39, i32* %41
br label %42
42: ; preds = %33, %27
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 6
store i32* null, i32** %43
%44 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 5
store i64 0, i64* %44
%45 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %8, i64 0, i64 0
%46 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 4
store %struct.iovec* %45, %struct.iovec** %46
%47 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 1, i32* %47
%48 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i8* %20, i8** %48
%49 = call i32 @CMSG_SPACE(i32 4)
%50 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i32 %49, i32* %50
%51 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store i64 0, i64* %51
%52 = load i32, i32* %5
%53 = call i32 @recvmsg(i32 %52, %struct.msghdr* %9, i32 0)
store i32 %53, i32* %14
%54 = load i32, i32* %14
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %42
store i32 -1, i32* %4
store i32 1, i32* %16
br label %94
57: ; preds = %42
%58 = load i32, i32* %14
%59 = sext i32 %58 to i64
%60 = load i64*, i64** %6
store i64 %59, i64* %60
%61 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %9)
store %struct.cmsghdr* %61, %struct.cmsghdr** %13
%62 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%63 = icmp eq %struct.cmsghdr* %62, null
br i1 %63, label %82, label %64
64: ; preds = %57
%65 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%66 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %65, i32 0, i32 0
%67 = load i64, i64* %66
%68 = call i64 @CMSG_LEN(i32 4)
%69 = icmp ne i64 %67, %68
br i1 %69, label %82, label %70
70: ; preds = %64
%71 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%72 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %71, i32 0, i32 1
%73 = load i64, i64* %72
%74 = load i64, i64* @SOL_SOCKET
%75 = icmp ne i64 %73, %74
br i1 %75, label %82, label %76
76: ; preds = %70
%77 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%78 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %77, i32 0, i32 2
%79 = load i64, i64* %78
%80 = load i64, i64* @SCM_RIGHTS
%81 = icmp ne i64 %79, %80
br i1 %81, label %82, label %83
82: ; preds = %76, %70, %64, %57
store i32 -2, i32* %4
store i32 1, i32* %16
br label %94
83: ; preds = %76
%84 = load %struct.cmsghdr*, %struct.cmsghdr** %13
%85 = call i64 @CMSG_DATA(%struct.cmsghdr* %84)
%86 = inttoptr i64 %85 to i32*
%87 = getelementptr inbounds i32, i32* %86, i64 0
%88 = load i32, i32* %87
store i32 %88, i32* %15
%89 = load i32, i32* %15
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %83
store i32 -3, i32* %4
store i32 1, i32* %16
br label %94
92: ; preds = %83
%93 = load i32, i32* %15
store i32 %93, i32* %4
store i32 1, i32* %16
br label %94
94: ; preds = %92, %91, %82, %56
%95 = load i8*, i8** %10
%96 = load i32, i32* %4
ret i32 %96
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i8*, i32, i64, %struct.iovec*, i64, i32* }
%struct.cmsghdr = type { i64, i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @_recvfd(i32 %0, i64* nocapture %1, i8* %2) {
%4 = alloca [1 x %struct.iovec]
%5 = alloca %struct.msghdr
%6 = alloca [4096 x i8]
%7 = bitcast [1 x %struct.iovec]* %4 to i8*
%8 = bitcast %struct.msghdr* %5 to i8*
%9 = tail call i32 @CMSG_SPACE(i32 4)
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%14 = load i64, i64* %1
%15 = icmp eq i64 %14, 0
%16 = icmp eq i8* %2, null
%17 = or i1 %16, %15
%18 = trunc i64 %14 to i32
%19 = select i1 %17, i8* %13, i8* %2
%20 = select i1 %17, i32 4096, i32 %18
%21 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* %19, i8** %21
%22 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 %20, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 5
%24 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
%26 = bitcast i64* %23 to i8*
store %struct.iovec* %24, %struct.iovec** %25
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store i8* %12, i8** %28
%29 = call i32 @CMSG_SPACE(i32 4)
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store i64 0, i64* %31
%32 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %59, label %34
34: ; preds = %3
%35 = zext i32 %32 to i64
store i64 %35, i64* %1
%36 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%37 = icmp eq %struct.cmsghdr* %36, null
br i1 %37, label %59, label %38
38: ; preds = %34
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 0
%40 = load i64, i64* %39
%41 = call i64 @CMSG_LEN(i32 4)
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %59
43: ; preds = %38
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 1
%45 = load i64, i64* %44
%46 = load i64, i64* @SOL_SOCKET
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %59
48: ; preds = %43
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i64 0, i32 2
%50 = load i64, i64* %49
%51 = load i64, i64* @SCM_RIGHTS
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = call i64 @CMSG_DATA(%struct.cmsghdr* %36)
%55 = inttoptr i64 %54 to i32*
%56 = load i32, i32* %55
%57 = icmp slt i32 %56, 0
%58 = select i1 %57, i32 -3, i32 %56
br label %59
59: ; preds = %53, %34, %38, %43, %48, %3
%60 = phi i32 [ -1, %3 ], [ -2, %48 ], [ -2, %43 ], [ -2, %38 ], [ -2, %34 ], [ %58, %53 ]
ret i32 %60
}