#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;
  int msg_controllen;
  struct cmsghdr *msg_control;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  void *member_0;
  size_t member_1;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int CMSG_LEN(int);
scalar_t__ EINTR;
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
scalar_t__ errno;
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
ssize_t recvfd(int socket, void *buffer, size_t length, int *fd) {
  struct iovec iov = {buffer, length};
  char buf[CMSG_LEN(sizeof(int))];
  struct cmsghdr *cmsg = (struct cmsghdr *)buf;
  ssize_t r;
  cmsg->cmsg_len = sizeof(buf);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  struct msghdr msg = {0};
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = cmsg;
  msg.msg_controllen = cmsg->cmsg_len;
again:
  r = recvmsg(socket, &msg, 0);
  if (r < 0 && errno == EINTR)
    goto again;
  if (r < 0)
    warn("recvmsg");
  else
    *fd = *((int *)CMSG_DATA(cmsg));
  return r;
}


x86 assembly (clang, -O0):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_LEN@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x1,-0x70(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jge    123a <recvfd+0xea>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <recvfd+0xea>
jmp    11fe <recvfd+0xae>
cmpq   $0x0,-0x50(%rbp)
jge    1256 <recvfd+0x106>
lea    0xdb4(%rip),%rdi
call   1030 <warn@plt>
jmp    1267 <recvfd+0x117>
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x50(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,-0x38(%rbp)
mov    %edi,%r12d
mov    %rsi,-0x70(%rbp)
mov    %rdx,-0x68(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rsp,-0x40(%rbp)
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rdx,%rdi
sub    %rcx,%rdi
mov    %rdi,%rsp
mov    %eax,(%rdi)
mov    0x2e5b(%rip),%rsi
mov    (%rsi),%esi
mov    %esi,0x8(%rdi)
mov    0x2e37(%rip),%rsi
mov    (%rsi),%esi
neg    %rcx
mov    %esi,0x4(%rdx,%rcx,1)
movq   $0x0,-0x48(%rbp)
lea    -0x70(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
movl   $0x1,-0x60(%rbp)
mov    %rdi,-0x30(%rbp)
mov    %rdi,-0x58(%rbp)
mov    %eax,-0x5c(%rbp)
lea    -0x60(%rbp),%r13
mov    0x2e09(%rip),%r14
mov    0x2df2(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r13,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    120b <recvfd+0xcb>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11e0 <recvfd+0xa0>
lea    0xdfc(%rip),%rdi
call   1030 <warn@plt>
jmp    121c <recvfd+0xdc>
mov    -0x30(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    -0x38(%rbp),%rcx
mov    %eax,(%rcx)
mov    -0x40(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i64 }
%struct.cmsghdr = type { i32, i32, i32 }
%struct.msghdr = type { i32, i32, %struct.cmsghdr*, %struct.iovec*, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [8 x i8] c"recvmsg\00"
define i64 @recvfd(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca %struct.iovec
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.cmsghdr*
%13 = alloca i64
%14 = alloca %struct.msghdr
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
%16 = load i8*, i8** %6
store i8* %16, i8** %15
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
%18 = load i64, i64* %7
store i64 %18, i64* %17
%19 = call i32 @CMSG_LEN(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 = bitcast i8* %22 to %struct.cmsghdr*
store %struct.cmsghdr* %23, %struct.cmsghdr** %12
%24 = trunc i64 %20 to i32
%25 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%26 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* @SOL_SOCKET
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 2
store i32 %27, i32* %29
%30 = load i32, i32* @SCM_RIGHTS
%31 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%32 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = bitcast %struct.msghdr* %14 to i8*
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 3
store %struct.iovec* %9, %struct.iovec** %34
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 0
store i32 1, i32* %35
%36 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%37 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 2
store %struct.cmsghdr* %36, %struct.cmsghdr** %37
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%39 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %14, i32 0, i32 1
store i32 %40, i32* %41
br label %42
42: ; preds = %51, %4
%43 = load i32, i32* %5
%44 = call i64 @recvmsg(i32 %43, %struct.msghdr* %14, i32 0)
store i64 %44, i64* %13
%45 = load i64, i64* %13
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i64, i64* @errno
%49 = load i64, i64* @EINTR
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
br label %42
52: ; preds = %47, %42
%53 = load i64, i64* %13
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %63
57: ; preds = %52
%58 = load %struct.cmsghdr*, %struct.cmsghdr** %12
%59 = call i64 @CMSG_DATA(%struct.cmsghdr* %58)
%60 = inttoptr i64 %59 to i32*
%61 = load i32, i32* %60
%62 = load i32*, i32** %8
store i32 %61, i32* %62
br label %63
63: ; preds = %57, %55
%64 = load i64, i64* %13
%65 = load i8*, i8** %10
ret i64 %64
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, i32, %struct.cmsghdr*, %struct.iovec*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [8 x i8] c"recvmsg\00"
define i64 @recvfd(i32 %0, i8* %1, i64 %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = bitcast %struct.iovec* %5 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %1, i8** %8
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i64 %2, i64* %9
%10 = tail call i32 @CMSG_LEN(i32 4)
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = bitcast i8* %13 to %struct.cmsghdr*
%15 = bitcast i8* %13 to i32*
store i32 %10, i32* %15
%16 = load i32, i32* @SOL_SOCKET
%17 = getelementptr inbounds i8, i8* %13, i64 8
%18 = bitcast i8* %17 to i32*
store i32 %16, i32* %18
%19 = load i32, i32* @SCM_RIGHTS
%20 = getelementptr inbounds i8, i8* %13, i64 4
%21 = bitcast i8* %20 to i32*
store i32 %19, i32* %21
%22 = bitcast %struct.msghdr* %6 to i8*
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 4
%24 = bitcast i32* %23 to i64*
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 3
store %struct.iovec* %5, %struct.iovec** %25
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 2
%28 = bitcast %struct.cmsghdr** %27 to i8**
store i8* %13, i8** %28
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store i32 %10, i32* %29
br label %30
30: ; preds = %33, %4
%31 = call i64 @recvmsg(i32 %0, %struct.msghdr* %6, i32 0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %30, label %37
37: ; preds = %33
%38 = call i32 @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %43
39: ; preds = %30
%40 = call i64 @CMSG_DATA(%struct.cmsghdr* %14)
%41 = inttoptr i64 %40 to i32*
%42 = load i32, i32* %41
store i32 %42, i32* %3
br label %43
43: ; preds = %39, %37
ret i64 %31
}