#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 {
  void *iov_base;
  scalar_t__ iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef scalar_t__ ssize_t;

/* 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__ recv(int, void *, scalar_t__, int /*<<< orphan*/);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
ssize_t receive_fd(int socket, int *fd, void *buffer, ssize_t buffer_size) {
  ssize_t size;
  if (fd) {
    char control[CMSG_SPACE(sizeof(int))];
    struct iovec iov = {.iov_base = buffer, .iov_len = buffer_size};
    struct msghdr message = {.msg_name = NULL,
                             .msg_namelen = 0,
                             .msg_iov = &iov,
                             .msg_iovlen = 1,
                             .msg_control = &control,
                             .msg_controllen = sizeof control};
    struct cmsghdr *cmsg;
    size = recvmsg(socket, &message, 0);
    if (size < 0)
      goto nofd;
    cmsg = CMSG_FIRSTHDR(&message);
    if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
        cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
      goto nofd;
    }
    memcpy(fd, CMSG_DATA(cmsg), sizeof *fd);
  } else {
    size = recv(socket, buffer, buffer_size, 0);
  }
  goto done;
nofd:
  *fd = -1;
done:
  return size;
}


x86 assembly (clang, -O0):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12f8 <receive_fd+0x188>
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdx
mov    %rdx,-0x48(%rbp)
mov    -0x20(%rbp),%rdx
mov    %rdx,-0x40(%rbp)
movl   $0x1,-0x78(%rbp)
mov    %rcx,-0x70(%rbp)
mov    %eax,-0x68(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x78(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1221 <receive_fd+0xb1>
movl   $0x2,-0x84(%rbp)
jmp    12d4 <receive_fd+0x164>
lea    -0x78(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
je     1294 <receive_fd+0x124>
mov    -0x80(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x90(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x90(%rbp),%rax
cmp    %rcx,%rax
jne    1294 <receive_fd+0x124>
mov    -0x80(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
jne    1294 <receive_fd+0x124>
mov    -0x80(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     12a3 <receive_fd+0x133>
movl   $0x2,-0x84(%rbp)
jmp    12d4 <receive_fd+0x164>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x80(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x84(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x84(%rbp),%eax
test   %eax,%eax
je     12f3 <receive_fd+0x183>
jmp    12ee <receive_fd+0x17e>
jmp    1313 <receive_fd+0x1a3>
jmp    130e <receive_fd+0x19e>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x28(%rbp)
jmp    131d <receive_fd+0x1ad>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x28(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %edi,%r15d
test   %rsi,%rsi
je     1274 <receive_fd+0x104>
mov    %rsi,%r14
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r13
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rbx,-0x38(%rbp)
mov    %r12,-0x30(%rbp)
movl   $0x1,-0x68(%rbp)
mov    %rdx,-0x60(%rbp)
mov    %eax,-0x58(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x68(%rbp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1258 <receive_fd+0xe8>
lea    -0x68(%rbp),%rdi
call   1090 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     1258 <receive_fd+0xe8>
mov    %rax,%r15
mov    (%rax),%r12
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%r12
jne    1258 <receive_fd+0xe8>
mov    0x8(%r15),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <receive_fd+0xe8>
mov    0x10(%r15),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <receive_fd+0xe8>
mov    %r15,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %r13,%rsp
jmp    1262 <receive_fd+0xf2>
mov    %r13,%rsp
movl   $0xffffffff,(%r14)
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <recv@plt>

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

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