#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;
  int msg_flags;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  int iov_len;
  void *iov_base;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ control;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int CMSG_SPACE(int);
int MSG_CTRUNC;
int /*<<< orphan*/ MSG_WAITALL;
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int recvmsg(int const, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int recvfd(const int sock_fd, void *data, const int data_len) {
  struct msghdr msgh = {0};
  struct cmsghdr *cmsg;
  struct iovec iov;
  char control[CMSG_SPACE(sizeof(int))];
  int n, buf_fd;
  iov.iov_base = data;
  iov.iov_len = data_len;
  msgh.msg_iov = &iov;
  msgh.msg_iovlen = 1;
  msgh.msg_control = control;
  msgh.msg_controllen = sizeof(control);
  n = recvmsg(sock_fd, &msgh, MSG_WAITALL);
  if (n < 0) {
    fprintf(stderr,
            "<3>"
            "recvmsg error: %s\n",
            strerror(errno));
    return -1;
  }
  if (msgh.msg_flags) {
    if (msgh.msg_flags & MSG_CTRUNC) {
      fprintf(stderr, "<3>"
                      "insufficient buffer space for ancillary data\n");
      return -1;
    }
  }
  for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
      buf_fd = *(int *)CMSG_DATA(cmsg);
      return buf_fd;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x30(%rbp)
movaps %xmm0,-0x40(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rdx
mov    %rdx,-0x50(%rbp)
mov    -0x14(%rbp),%edx
mov    %edx,-0x58(%rbp)
lea    -0x58(%rbp),%rdx
mov    %rdx,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rsi
call   1070 <recvmsg@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jge    124a <recvfd+0xda>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x78(%rbp),%edi
mov    %rax,%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1328 <recvfd+0x1b8>
cmpl   $0x0,-0x2c(%rbp)
je     1298 <recvfd+0x128>
mov    -0x2c(%rbp),%eax
mov    0x2d72(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1293 <recvfd+0x123>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1328 <recvfd+0x1b8>
jmp    1298 <recvfd+0x128>
lea    -0x40(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     131a <recvfd+0x1aa>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%rax
jne    12ff <recvfd+0x18f>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2cea(%rip),%rcx
cmp    (%rcx),%rax
jne    12ff <recvfd+0x18f>
mov    -0x48(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x70(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    1328 <recvfd+0x1b8>
jmp    1304 <recvfd+0x194>
mov    -0x48(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1060 <CMSG_NXTHDR@plt>
mov    %rax,-0x48(%rbp)
jmp    12a5 <recvfd+0x135>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x60(%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    $0x38,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x58(%rbp)
mov    %r15d,-0x60(%rbp)
lea    -0x60(%rbp),%rcx
mov    %rcx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
lea    -0x50(%rbp),%rsi
mov    %r14d,%edi
call   1070 <recvmsg@plt>
test   %eax,%eax
js     127f <recvfd+0x10f>
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     1226 <recvfd+0xb6>
mov    0x2dca(%rip),%rcx
test   %eax,(%rcx)
je     1226 <recvfd+0xb6>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12a9 <recvfd+0x139>
lea    -0x50(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12af <recvfd+0x13f>
mov    0x2da7(%rip),%r15
mov    0x2d78(%rip),%r13
lea    -0x50(%rbp),%rbx
jmp    1260 <recvfd+0xf0>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     12af <recvfd+0x13f>
mov    (%rax),%rcx
cmp    (%r15),%rcx
jne    1250 <recvfd+0xe0>
mov    0x8(%rax),%rcx
cmp    0x0(%r13),%rcx
jne    1250 <recvfd+0xe0>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%r14d
jmp    12af <recvfd+0x13f>
mov    0x2d52(%rip),%rax
mov    (%rax),%ebx
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd63(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
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.msghdr = type { i32, i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i32, i8* }
%struct.cmsghdr = type { i64, i64 }
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"<3>recvmsg error: %s\0A\00"
@errno = global i32 0
@MSG_CTRUNC = global i32 0
@.str.1 = [49 x i8] c"<3>insufficient buffer space for ancillary data\0A\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @recvfd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca %struct.iovec
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%16 = bitcast %struct.msghdr* %8 to i8*
%17 = call i32 @CMSG_SPACE(i32 4)
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %11
%20 = alloca i8, i64 %18
store i64 %18, i64* %12
%21 = load i8*, i8** %6
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i8* %21, i8** %22
%23 = load i32, i32* %7
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store %struct.iovec* %10, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i8* %20, i8** %27
%28 = trunc i64 %18 to i32
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = load i32, i32* @MSG_WAITALL
%32 = call i32 @recvmsg(i32 %30, %struct.msghdr* %8, i32 %31)
store i32 %32, i32* %13
%33 = load i32, i32* %13
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %3
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %38)
store i32 -1, i32* %4
store i32 1, i32* %15
br label %82
40: ; preds = %3
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %40
%45 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
%46 = load i32, i32* %45
%47 = load i32, i32* @MSG_CTRUNC
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %15
br label %82
53: ; preds = %44
br label %54
54: ; preds = %53, %40
%55 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %8)
store %struct.cmsghdr* %55, %struct.cmsghdr** %9
br label %56
56: ; preds = %78, %54
%57 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%58 = icmp ne %struct.cmsghdr* %57, null
br i1 %58, label %59, label %81
59: ; preds = %56
%60 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%61 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %60, i32 0, i32 0
%62 = load i64, i64* %61
%63 = load i64, i64* @SOL_SOCKET
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %77
65: ; preds = %59
%66 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%67 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %66, i32 0, i32 1
%68 = load i64, i64* %67
%69 = load i64, i64* @SCM_RIGHTS
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %77
71: ; preds = %65
%72 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%73 = call i64 @CMSG_DATA(%struct.cmsghdr* %72)
%74 = inttoptr i64 %73 to i32*
%75 = load i32, i32* %74
store i32 %75, i32* %14
%76 = load i32, i32* %14
store i32 %76, i32* %4
store i32 1, i32* %15
br label %82
77: ; preds = %65, %59
br label %78
78: ; preds = %77
%79 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%80 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %8, %struct.cmsghdr* %79)
store %struct.cmsghdr* %80, %struct.cmsghdr** %9
br label %56
81: ; preds = %56
store i32 -1, i32* %4
store i32 1, i32* %15
br label %82
82: ; preds = %81, %71, %50, %35
%83 = load i8*, i8** %11
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i32, i8* }
%struct.cmsghdr = type { i64, i64 }
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"<3>recvmsg error: %s\0A\00"
@errno = global i32 0
@MSG_CTRUNC = global i32 0
@.str.1 = [49 x i8] c"<3>insufficient buffer space for ancillary data\0A\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @recvfd(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = bitcast %struct.msghdr* %4 to i8*
%7 = bitcast %struct.iovec* %5 to i8*
%8 = tail call i32 @CMSG_SPACE(i32 4)
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i8* %1, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i32 %2, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 4
store %struct.iovec* %5, %struct.iovec** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i8* %11, i8** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 %8, i32* %17
%18 = load i32, i32* @MSG_WAITALL
%19 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %58
26: ; preds = %3
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %26
%31 = load i32, i32* @MSG_CTRUNC
%32 = and i32 %31, %28
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %58
37: ; preds = %30, %26
%38 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%39 = icmp eq %struct.cmsghdr* %38, null
br i1 %39, label %58, label %40
40: ; preds = %37, %55
%41 = phi %struct.cmsghdr* [ %56, %55 ], [ %38, %37 ]
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i64 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @SOL_SOCKET
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %55
46: ; preds = %40
%47 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i64 0, i32 1
%48 = load i64, i64* %47
%49 = load i64, i64* @SCM_RIGHTS
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = call i64 @CMSG_DATA(%struct.cmsghdr* %41)
%53 = inttoptr i64 %52 to i32*
%54 = load i32, i32* %53
br label %58
55: ; preds = %40, %46
%56 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %41)
%57 = icmp eq %struct.cmsghdr* %56, null
br i1 %57, label %58, label %40
58: ; preds = %55, %37, %51, %34, %21
%59 = phi i32 [ -1, %21 ], [ -1, %34 ], [ %54, %51 ], [ -1, %37 ], [ -1, %55 ]
ret i32 %59
}