#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;
  struct iovec *msg_iov;
  int /*<<< orphan*/ msg_namelen;
  int /*<<< orphan*/ *msg_name;
};
struct iovec {
  int dummy;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ control;

/* Variables and functions */
int /*<<< orphan*/ 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 /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
ssize_t receive_fd(int socket, int *fd, struct iovec *iov, int iovlen) {
  ssize_t size;
  char control[CMSG_SPACE(sizeof(*fd))];
  struct msghdr message = {
      .msg_name = NULL,
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
  };
  struct cmsghdr *cmsg;
  if (fd) {
    *fd = -1;
    message.msg_control = &control;
    message.msg_controllen = sizeof(control);
  }
  size = recvmsg(socket, &message, 0);
  if (size < 0)
    return -1;
  cmsg = CMSG_FIRSTHDR(&message);
  if (fd && cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(*fd)) &&
      cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
    memcpy(fd, CMSG_DATA(cmsg), sizeof(*fd));
  return size;
}


x86 assembly (clang, -O0):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x90(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120a <receive_fd+0x9a>
mov    -0x90(%rbp),%rax
mov    -0x88(%rbp),%rcx
mov    -0x18(%rbp),%rdx
movl   $0xffffffff,(%rdx)
mov    %rcx,-0x68(%rbp)
mov    %eax,-0x60(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    123b <receive_fd+0xcb>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x7c(%rbp)
jmp    12ef <receive_fd+0x17f>
lea    -0x70(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12e0 <receive_fd+0x170>
cmpq   $0x0,-0x78(%rbp)
je     12e0 <receive_fd+0x170>
mov    -0x78(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x98(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x98(%rbp),%rax
cmp    %rcx,%rax
jne    12e0 <receive_fd+0x170>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <receive_fd+0x170>
mov    -0x78(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2d28(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <receive_fd+0x170>
mov    -0x18(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x78(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x7c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%ebx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%edx
mov    %rsp,%rcx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rcx
mov    %rcx,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x30(%rbp)
mov    %ebx,-0x50(%rbp)
mov    %r12,-0x38(%rbp)
test   %r14,%r14
je     11c0 <receive_fd+0x60>
movl   $0xffffffff,(%r14)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <recvmsg@plt>
test   %rax,%rax
js     1237 <receive_fd+0xd7>
mov    %rax,%rbx
lea    -0x50(%rbp),%rdi
call   1080 <CMSG_FIRSTHDR@plt>
test   %r14,%r14
je     123e <receive_fd+0xde>
mov    %rax,%r15
test   %rax,%rax
je     123e <receive_fd+0xde>
mov    (%r15),%r12
mov    $0x4,%edi
call   1040 <CMSG_LEN@plt>
cmp    %rax,%r12
jne    123e <receive_fd+0xde>
mov    0x8(%r15),%rax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <receive_fd+0xde>
mov    0x10(%r15),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    123e <receive_fd+0xde>
mov    %r15,%rdi
call   1030 <CMSG_DATA@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    123e <receive_fd+0xde>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
%struct.msghdr = type { i32, i8**, i32, %struct.iovec*, i32, i32* }
%struct.cmsghdr = type { i64, i64, i64 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i64 @receive_fd(i32 %0, i32* %1, %struct.iovec* %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.iovec*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.msghdr
%14 = alloca %struct.cmsghdr*
%15 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store %struct.iovec* %2, %struct.iovec** %8
store i32 %3, i32* %9
%16 = call i32 @CMSG_SPACE(i32 4)
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %11
%19 = alloca i8, i64 %17
store i64 %17, i64* %12
%20 = bitcast %struct.msghdr* %13 to i8*
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 0
%22 = load i32, i32* %9
store i32 %22, i32* %21
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 3
%24 = load %struct.iovec*, %struct.iovec** %8
store %struct.iovec* %24, %struct.iovec** %23
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %33
27: ; preds = %4
%28 = load i32*, i32** %7
store i32 -1, i32* %28
%29 = bitcast i8* %19 to i8**
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 1
store i8** %29, i8*** %30
%31 = trunc i64 %17 to i32
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %13, i32 0, i32 2
store i32 %31, i32* %32
br label %33
33: ; preds = %27, %4
%34 = load i32, i32* %6
%35 = call i64 @recvmsg(i32 %34, %struct.msghdr* %13, i32 0)
store i64 %35, i64* %10
%36 = load i64, i64* %10
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
store i64 -1, i64* %5
store i32 1, i32* %15
br label %71
39: ; preds = %33
%40 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %13)
store %struct.cmsghdr* %40, %struct.cmsghdr** %14
%41 = load i32*, i32** %7
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %69
43: ; preds = %39
%44 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%45 = icmp ne %struct.cmsghdr* %44, null
br i1 %45, label %46, label %69
46: ; preds = %43
%47 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%48 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %47, i32 0, i32 0
%49 = load i64, i64* %48
%50 = call i64 @CMSG_LEN(i32 4)
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %69
52: ; preds = %46
%53 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%54 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %53, i32 0, i32 1
%55 = load i64, i64* %54
%56 = load i64, i64* @SOL_SOCKET
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %52
%59 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%60 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %59, i32 0, i32 2
%61 = load i64, i64* %60
%62 = load i64, i64* @SCM_RIGHTS
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load i32*, i32** %7
%66 = load %struct.cmsghdr*, %struct.cmsghdr** %14
%67 = call i32 @CMSG_DATA(%struct.cmsghdr* %66)
%68 = call i32 @memcpy(i32* %65, i32 %67, i32 4)
br label %69
69: ; preds = %64, %58, %52, %46, %43, %39
%70 = load i64, i64* %10
store i64 %70, i64* %5
store i32 1, i32* %15
br label %71
71: ; preds = %69, %38
%72 = load i8*, i8** %11
%73 = load i64, i64* %5
ret i64 %73
}

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