#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*/ member_0;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};
typedef int /*<<< orphan*/ mbuf;
typedef int /*<<< orphan*/ cbuf;

/* Variables and functions */
int /*<<< orphan*/ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int /*<<< orphan*/ memmove(int *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int s, int *fd) {
  struct msghdr msg = {0};
  char mbuf[1];
  char cbuf[CMSG_SPACE(sizeof(int))];
  struct iovec io = {.iov_base = mbuf, .iov_len = sizeof(mbuf)};
  msg.msg_iov = &io;
  msg.msg_iovlen = 1;
  msg.msg_control = cbuf;
  msg.msg_controllen = sizeof(cbuf);
  if (recvmsg(s, &msg, 0) < 0) {
    return (-1);
  }
  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  memmove(fd, CMSG_DATA(cmsg), sizeof(int));
  return (0);
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%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,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
lea    -0x41(%rbp),%rdx
mov    %rdx,-0x68(%rbp)
movl   $0x1,-0x60(%rbp)
lea    -0x68(%rbp),%rdx
mov    %rdx,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
cmp    $0x0,%rax
jge    11f0 <recv_fd+0xa0>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    122c <recv_fd+0xdc>
lea    -0x40(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x78(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1060 <memmove@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
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
lea    -0x21(%rbp),%rcx
mov    %rcx,-0x60(%rbp)
movl   $0x1,-0x58(%rbp)
lea    -0x60(%rbp),%rcx
mov    %rcx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
mov    %rdx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
xor    %ebx,%ebx
mov    %r15d,%edi
xor    %edx,%edx
call   1050 <recvmsg@plt>
test   %rax,%rax
js     11f4 <recv_fd+0xa4>
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x4,%edx
call   1060 <memmove@plt>
jmp    11f9 <recv_fd+0xa9>
mov    $0xffffffff,%ebx
mov    %r12,%rsp
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
define i32 @recv_fd(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.msghdr
%7 = alloca [1 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.iovec
%11 = alloca i32
%12 = alloca %struct.cmsghdr*
store i32 %0, i32* %4
store i32* %1, i32** %5
%13 = bitcast %struct.msghdr* %6 to i8*
%14 = call i32 @CMSG_SPACE(i32 4)
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
store i8* %19, i8** %18
%20 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i32 1, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store %struct.iovec* %10, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i8* %17, i8** %23
%24 = trunc i64 %15 to i32
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = call i64 @recvmsg(i32 %26, %struct.msghdr* %6, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %11
br label %36
30: ; preds = %2
%31 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %6)
store %struct.cmsghdr* %31, %struct.cmsghdr** %12
%32 = load i32*, i32** %5
%33 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%34 = call i32 @CMSG_DATA(%struct.cmsghdr* %33)
%35 = call i32 @memmove(i32* %32, i32 %34, i32 4)
store i32 0, i32* %3
store i32 1, i32* %11
br label %36
36: ; preds = %30, %29
%37 = load i8*, i8** %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32 }
define i32 @recv_fd(i32 %0, i32* %1) {
%3 = alloca %struct.msghdr
%4 = alloca [1 x i8]
%5 = alloca %struct.iovec
%6 = bitcast %struct.msghdr* %3 to i8*
%7 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%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 = bitcast %struct.iovec* %5 to i8*
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %7, i8** %13
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i8* %11, i8** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 %8, i32* %18
%19 = call i64 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %2
%22 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%23 = call i32 @CMSG_DATA(%struct.cmsghdr* %22)
%24 = call i32 @memmove(i32* %1, i32 %23, i32 4)
br label %25
25: ; preds = %2, %21
%26 = phi i32 [ 0, %21 ], [ -1, %2 ]
ret i32 %26
}