#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 {
  char *msg_control;
  int msg_controllen;
  int msg_iovlen;
  int msg_flags;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_type;
};
typedef int /*<<< orphan*/ c;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int CMSG_SPACE(int);
int MSG_TRUNC;
scalar_t__ SCM_RIGHTS;
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_fd(int via) {
  struct msghdr h = {};
  struct cmsghdr *ch;
  struct iovec iov;
  char buf[CMSG_SPACE(sizeof(int))], c;
  int *fdp;
  h.msg_control = buf;
  h.msg_controllen = sizeof(buf);
  h.msg_iov = &iov;
  h.msg_iovlen = 1;
  iov.iov_base = &c;
  iov.iov_len = sizeof(c);
  if (recvmsg(via, &h, 0) <= 0)
    return -1;
  ch = CMSG_FIRSTHDR(&h);
  if (h.msg_flags & MSG_TRUNC)
    return -2;
  if (ch == NULL)
    return -3;
  if (ch->cmsg_type != SCM_RIGHTS)
    return -4;
  fdp = (int *)CMSG_DATA(ch);
  return *fdp;
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x20(%rbp)
movaps %xmm0,-0x30(%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)
mov    %rcx,-0x30(%rbp)
mov    %eax,-0x28(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
lea    -0x59(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1050 <recvmsg@plt>
cmp    $0x0,%rax
jg     11d1 <recv_fd+0x91>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    126b <recv_fd+0x12b>
lea    -0x30(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e00(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1206 <recv_fd+0xc6>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    126b <recv_fd+0x12b>
cmpq   $0x0,-0x38(%rbp)
jne    1224 <recv_fd+0xe4>
movl   $0xfffffffd,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    126b <recv_fd+0x12b>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
je     124e <recv_fd+0x10e>
movl   $0xfffffffc,-0x4(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    126b <recv_fd+0x12b>
mov    -0x38(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-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   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebx
movq   $0x0,-0x28(%rbp)
mov    $0x4,%edi
call   1040 <CMSG_SPACE@plt>
mov    %rsp,%r14
mov    %eax,%ecx
mov    %rsp,%rdx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rdx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x11(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x40(%rbp)
lea    -0x38(%rbp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <recvmsg@plt>
test   %rax,%rax
jle    11ed <recv_fd+0xad>
lea    -0x38(%rbp),%rdi
call   1060 <CMSG_FIRSTHDR@plt>
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
mov    $0xfffffffe,%ecx
test   %edx,-0x28(%rbp)
jne    11f2 <recv_fd+0xb2>
test   %rax,%rax
je     1200 <recv_fd+0xc0>
mov    (%rax),%rdx
mov    0x2e01(%rip),%rsi
mov    $0xfffffffc,%ecx
cmp    (%rsi),%rdx
jne    11f2 <recv_fd+0xb2>
mov    %rax,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ecx
jmp    11f2 <recv_fd+0xb2>
mov    $0xffffffff,%ecx
mov    %r14,%rsp
mov    %ecx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xfffffffd,%ecx
jmp    11f2 <recv_fd+0xb2>

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i64 }
@MSG_TRUNC = global i32 0
@SCM_RIGHTS = global i64 0
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.msghdr
%5 = alloca %struct.cmsghdr*
%6 = alloca %struct.iovec
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %3
%12 = bitcast %struct.msghdr* %4 to i8*
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 0
store i8* %16, i8** %17
%18 = trunc i64 %14 to i32
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 4
store %struct.iovec* %6, %struct.iovec** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 2
store i32 1, i32* %21
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
store i8* %9, i8** %22
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i32 1, i32* %23
%24 = load i32, i32* %3
%25 = call i64 @recvmsg(i32 %24, %struct.msghdr* %4, i32 0)
%26 = icmp sle i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %11
br label %53
28: ; preds = %1
%29 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
store %struct.cmsghdr* %29, %struct.cmsghdr** %5
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i32 0, i32 3
%31 = load i32, i32* %30
%32 = load i32, i32* @MSG_TRUNC
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 -2, i32* %2
store i32 1, i32* %11
br label %53
36: ; preds = %28
%37 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%38 = icmp eq %struct.cmsghdr* %37, null
br i1 %38, label %39, label %40
39: ; preds = %36
store i32 -3, i32* %2
store i32 1, i32* %11
br label %53
40: ; preds = %36
%41 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%42 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* @SCM_RIGHTS
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 -4, i32* %2
store i32 1, i32* %11
br label %53
47: ; preds = %40
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%49 = call i64 @CMSG_DATA(%struct.cmsghdr* %48)
%50 = inttoptr i64 %49 to i32*
store i32* %50, i32** %10
%51 = load i32*, i32** %10
%52 = load i32, i32* %51
store i32 %52, i32* %2
store i32 1, i32* %11
br label %53
53: ; preds = %47, %46, %39, %35, %27
%54 = load i8*, i8** %7
%55 = load i32, i32* %2
ret i32 %55
}

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