#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 */
typedef int /*<<< orphan*/ val;
struct msghdr {
  int msg_iovlen;
  char *msg_control;
  int msg_controllen;
  struct iovec *msg_iov;
  scalar_t__ msg_namelen;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  int *iov_base;
  int iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
};
typedef int /*<<< orphan*/ fd;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int /*<<< orphan*/ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int errno;
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int p) {
  struct msghdr hdr;
  struct iovec iov;
  struct cmsghdr *cmsg;
  int val, fd;
  char buf[((((sizeof(struct cmsghdr)) + sizeof(intptr_t) - 1) &
             ~(sizeof(intptr_t) - 1)) +
            (((sizeof(fd)) + sizeof(intptr_t) - 1) & ~(sizeof(intptr_t) - 1)))];
  hdr.msg_name = NULL;
  hdr.msg_namelen = 0;
  hdr.msg_iov = &iov;
  hdr.msg_iovlen = 1;
  hdr.msg_control = buf;
  hdr.msg_controllen = sizeof(buf);
  iov.iov_base = &val;
  iov.iov_len = sizeof(val);
  if (recvmsg(p, &hdr, 0) != sizeof(val))
    return -1;
  for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
    if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type = SCM_RIGHTS) &&
        (cmsg->cmsg_len >= ((((sizeof(struct cmsghdr)) + sizeof(intptr_t) - 1) &
                             ~(sizeof(intptr_t) - 1)) +
                            (sizeof(fd))))) {
      memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
      return fd;
    }
  }
  errno = val;
  return -1;
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x18,-0x28(%rbp)
lea    -0x54(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x4,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cltq
cmp    $0x4,%rax
je     11be <recv_fd+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <recv_fd+0x11a>
lea    -0x38(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1257 <recv_fd+0x107>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    123c <recv_fd+0xec>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    -0x50(%rbp),%rcx
mov    %eax,0xc(%rcx)
cmp    $0x0,%eax
je     123c <recv_fd+0xec>
mov    -0x50(%rbp),%rax
movslq 0x8(%rax),%rax
cmp    $0x14,%rax
jb     123c <recv_fd+0xec>
mov    -0x50(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %eax,%esi
lea    -0x58(%rbp),%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <recv_fd+0x11a>
jmp    1241 <recv_fd+0xf1>
mov    -0x50(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x50(%rbp)
jmp    11cb <recv_fd+0x7b>
mov    -0x54(%rbp),%ecx
mov    0x2d77(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x40(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x38(%rsp)
movl   $0x1,0x20(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x18,0x30(%rsp)
lea    0xc(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x4,0x18(%rsp)
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    $0xffffffff,%r14d
cmp    $0x4,%eax
jne    1236 <recv_fd+0xe6>
lea    0x20(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1229 <recv_fd+0xd9>
mov    0x2e1f(%rip),%rbp
mov    0x2e00(%rip),%r15
lea    0x20(%rsp),%rbx
jmp    11f0 <recv_fd+0xa0>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <CMSG_NXTHDR@plt>
test   %rax,%rax
je     1229 <recv_fd+0xd9>
mov    (%rax),%rcx
cmp    0x0(%rbp),%rcx
jne    11e0 <recv_fd+0x90>
mov    (%r15),%ecx
mov    %ecx,0xc(%rax)
test   %ecx,%ecx
je     11e0 <recv_fd+0x90>
cmpl   $0x14,0x8(%rax)
jb     11e0 <recv_fd+0x90>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
lea    0x8(%rsp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x8(%rsp),%r14d
jmp    1236 <recv_fd+0xe6>
mov    0xc(%rsp),%eax
mov    0x2da4(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i32* }
%struct.iovec = type { i32*, i32 }
%struct.cmsghdr = type { i64, i32, i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i32 0
@errno = global i32 0
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = alloca %struct.cmsghdr*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [24 x i8]
store i32 %0, i32* %3
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 5
store i32* null, i32** %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 4
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 3
store %struct.iovec* %5, %struct.iovec** %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 0
store i32 1, i32* %13
%14 = getelementptr inbounds [24 x i8], [24 x i8]* %9, i64 0, i64 0
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 1
store i8* %14, i8** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 2
store i32 24, i32* %16
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
store i32* %7, i32** %17
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i32 4, i32* %18
%19 = load i32, i32* %3
%20 = call i32 @recvmsg(i32 %19, %struct.msghdr* %4, i32 0)
%21 = sext i32 %20 to i64
%22 = icmp ne i64 %21, 4
br i1 %22, label %23, label %24
23: ; preds = %1
store i32 -1, i32* %2
br label %57
24: ; preds = %1
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
store %struct.cmsghdr* %25, %struct.cmsghdr** %6
br label %26
26: ; preds = %52, %24
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%28 = icmp ne %struct.cmsghdr* %27, null
br i1 %28, label %29, label %55
29: ; preds = %26
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%31 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = load i64, i64* @SOL_SOCKET
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %51
35: ; preds = %29
%36 = load i32, i32* @SCM_RIGHTS
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %37, i32 0, i32 2
store i32 %36, i32* %38
%39 = icmp ne i32 %36, 0
br i1 %39, label %40, label %51
40: ; preds = %35
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = icmp uge i64 %44, 20
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%48 = call i32 @CMSG_DATA(%struct.cmsghdr* %47)
%49 = call i32 @memcpy(i32* %8, i32 %48, i32 4)
%50 = load i32, i32* %8
store i32 %50, i32* %2
br label %57
51: ; preds = %40, %35, %29
br label %52
52: ; preds = %51
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%54 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %53)
store %struct.cmsghdr* %54, %struct.cmsghdr** %6
br label %26
55: ; preds = %26
%56 = load i32, i32* %7
store i32 %56, i32* @errno
store i32 -1, i32* %2
br label %57
57: ; preds = %55, %46, %23
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i32* }
%struct.iovec = type { i32*, i32 }
%struct.cmsghdr = type { i64, i32, i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i32 0
@errno = global i32 0
define i32 @recv_fd(i32 %0) {
%2 = alloca %struct.msghdr
%3 = alloca %struct.iovec
%4 = alloca i32
%5 = alloca i32
%6 = alloca [24 x i8]
%7 = bitcast %struct.msghdr* %2 to i8*
%8 = bitcast %struct.iovec* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [24 x i8], [24 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 4
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 3
%14 = bitcast i64* %12 to i8*
store %struct.iovec* %3, %struct.iovec** %13
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 1
store i8* %11, i8** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %2, i64 0, i32 2
store i32 24, i32* %17
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
store i32* %4, i32** %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
store i32 4, i32* %19
%20 = call i32 @recvmsg(i32 %0, %struct.msghdr* %2, i32 0)
%21 = icmp eq i32 %20, 4
br i1 %21, label %22, label %48
22: ; preds = %1
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%24 = icmp eq %struct.cmsghdr* %23, null
br i1 %24, label %46, label %25
25: ; preds = %22, %43
%26 = phi %struct.cmsghdr* [ %44, %43 ], [ %23, %22 ]
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i64 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* @SOL_SOCKET
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %43
31: ; preds = %25
%32 = load i32, i32* @SCM_RIGHTS
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i64 0, i32 2
store i32 %32, i32* %33
%34 = icmp eq i32 %32, 0
br i1 %34, label %43, label %35
35: ; preds = %31
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i64 0, i32 1
%37 = load i32, i32* %36
%38 = icmp ugt i32 %37, 19
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = call i32 @CMSG_DATA(%struct.cmsghdr* %26)
%41 = call i32 @memcpy(i32* %5, i32 %40, i32 4)
%42 = load i32, i32* %5
br label %48
43: ; preds = %25, %31, %35
%44 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %2, %struct.cmsghdr* %26)
%45 = icmp eq %struct.cmsghdr* %44, null
br i1 %45, label %46, label %25
46: ; preds = %43, %22
%47 = load i32, i32* %4
store i32 %47, i32* @errno
br label %48
48: ; preds = %1, %46, %39
%49 = phi i32 [ %42, %39 ], [ -1, %46 ], [ -1, %1 ]
ret i32 %49
}