#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;
  scalar_t__ cmsg_type;
  int cmsg_len;
};
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 *);
scalar_t__ 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    $0x80,%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    -0x80(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x20,-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     11c1 <recv_fd+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <recv_fd+0x11c>
lea    -0x38(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1259 <recv_fd+0x109>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <recv_fd+0xee>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <recv_fd+0xee>
mov    -0x50(%rbp),%rax
movslq 0x10(%rax),%rax
cmp    $0x1c,%rax
jb     123e <recv_fd+0xee>
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    126c <recv_fd+0x11c>
jmp    1243 <recv_fd+0xf3>
mov    -0x50(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1050 <CMSG_NXTHDR@plt>
mov    %rax,-0x50(%rbp)
jmp    11ce <recv_fd+0x7e>
mov    -0x54(%rbp),%ecx
mov    0x2d75(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%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   $0x20,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    1235 <recv_fd+0xe5>
lea    0x20(%rsp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1228 <recv_fd+0xd8>
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     1228 <recv_fd+0xd8>
mov    (%rax),%rcx
cmp    0x0(%rbp),%rcx
jne    11e0 <recv_fd+0x90>
mov    0x8(%rax),%rcx
cmp    (%r15),%rcx
jne    11e0 <recv_fd+0x90>
cmpl   $0x1c,0x10(%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    1235 <recv_fd+0xe5>
mov    0xc(%rsp),%eax
mov    0x2da5(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x78,%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, i64, i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 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 [32 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 [32 x i8], [32 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 32, 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 %58
24: ; preds = %1
%25 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
store %struct.cmsghdr* %25, %struct.cmsghdr** %6
br label %26
26: ; preds = %53, %24
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%28 = icmp ne %struct.cmsghdr* %27, null
br i1 %28, label %29, label %56
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 %52
35: ; preds = %29
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = load i64, i64* @SCM_RIGHTS
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %52
41: ; preds = %35
%42 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %42, i32 0, i32 2
%44 = load i32, i32* %43
%45 = sext i32 %44 to i64
%46 = icmp uge i64 %45, 28
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%49 = call i32 @CMSG_DATA(%struct.cmsghdr* %48)
%50 = call i32 @memcpy(i32* %8, i32 %49, i32 4)
%51 = load i32, i32* %8
store i32 %51, i32* %2
br label %58
52: ; preds = %41, %35, %29
br label %53
53: ; preds = %52
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%55 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %4, %struct.cmsghdr* %54)
store %struct.cmsghdr* %55, %struct.cmsghdr** %6
br label %26
56: ; preds = %26
%57 = load i32, i32* %7
store i32 %57, i32* @errno
store i32 -1, i32* %2
br label %58
58: ; preds = %56, %47, %23
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i64, i32* }
%struct.iovec = type { i32*, i32 }
%struct.cmsghdr = type { i64, i64, i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 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 [32 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 [32 x i8], [32 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 32, 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 %49
22: ; preds = %1
%23 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %2)
%24 = icmp eq %struct.cmsghdr* %23, null
br i1 %24, label %47, label %25
25: ; preds = %22, %44
%26 = phi %struct.cmsghdr* [ %45, %44 ], [ %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 %44
31: ; preds = %25
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i64 0, i32 1
%33 = load i64, i64* %32
%34 = load i64, i64* @SCM_RIGHTS
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i64 0, i32 2
%38 = load i32, i32* %37
%39 = icmp ugt i32 %38, 27
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 @CMSG_DATA(%struct.cmsghdr* %26)
%42 = call i32 @memcpy(i32* %5, i32 %41, i32 4)
%43 = load i32, i32* %5
br label %49
44: ; preds = %25, %31, %36
%45 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %2, %struct.cmsghdr* %26)
%46 = icmp eq %struct.cmsghdr* %45, null
br i1 %46, label %47, label %25
47: ; preds = %44, %22
%48 = load i32, i32* %4
store i32 %48, i32* @errno
br label %49
49: ; preds = %1, %47, %40
%50 = phi i32 [ %43, %40 ], [ -1, %47 ], [ -1, %1 ]
ret i32 %50
}