#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;
  scalar_t__ msg_control;
  scalar_t__ msg_namelen;
  int /*<<< orphan*/ *msg_name;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  int dummy;
};
typedef scalar_t__ caddr_t;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *);
struct cmsghdr *xmalloc(int);
int recv_fd(int sockfd) {
  struct cmsghdr *cmptr = NULL;
  struct iovec iov;
  struct msghdr msg;
  char ch;
  int nread;
  iov.iov_base = &ch;
  iov.iov_len = 1;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_name = NULL;
  msg.msg_namelen = 0;
  if (!cmptr)
    cmptr = xmalloc((sizeof(struct cmsghdr) + sizeof(int)));
  msg.msg_control = (caddr_t)cmptr;
  msg.msg_controllen = (sizeof(struct cmsghdr) + sizeof(int));
  nread = recvmsg(sockfd, &msg, 0);
  if (nread < 0) {
    warn("recv_fd recvmsg");
    return -1;
  }
  if (nread == 0) {
    warnx("recv_fd: connection closed by server");
    return -2;
  }
  if (ch == 0)
    if (msg.msg_controllen == (sizeof(struct cmsghdr) + sizeof(int)))
      return *(int *)CMSG_DATA(cmptr);
  warnx("recv_fd: protocol error");
  return -3;
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x49(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11aa <recv_fd+0x5a>
mov    $0x8,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x8,-0x44(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    11ec <recv_fd+0x9c>
lea    0xe25(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <recv_fd+0x104>
cmpl   $0x0,-0x50(%rbp)
jne    120e <recv_fd+0xbe>
lea    0xe13(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1254 <recv_fd+0x104>
movsbl -0x49(%rbp),%eax
cmp    $0x0,%eax
jne    1241 <recv_fd+0xf1>
movslq -0x44(%rbp),%rax
cmp    $0x8,%rax
jne    123c <recv_fd+0xec>
mov    -0x10(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <recv_fd+0x104>
jmp    1241 <recv_fd+0xf1>
lea    0xded(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xfffffffd,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebp
lea    0xf(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x1,0x18(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x40(%rsp)
movl   $0x1,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x30(%rsp)
mov    $0x8,%edi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,0x28(%rsp)
movl   $0x8,0x24(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <recvmsg@plt>
test   %eax,%eax
js     11cc <recv_fd+0x7c>
je     11df <recv_fd+0x8f>
cmpb   $0x0,0xf(%rsp)
jne    11f2 <recv_fd+0xa2>
cmpl   $0x8,0x24(%rsp)
jne    11f2 <recv_fd+0xa2>
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    (%rax),%eax
jmp    1203 <recv_fd+0xb3>
lea    0xe2d(%rip),%rdi
call   1030 <warn@plt>
mov    $0xffffffff,%eax
jmp    1203 <recv_fd+0xb3>
lea    0xe2a(%rip),%rdi
call   1050 <warnx@plt>
mov    $0xfffffffe,%eax
jmp    1203 <recv_fd+0xb3>
lea    0xe3c(%rip),%rdi
call   1050 <warnx@plt>
mov    $0xfffffffd,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i32 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i64, i64, i32*, %struct.iovec* }
@.str = [16 x i8] c"recv_fd recvmsg\00"
@.str.1 = [37 x i8] c"recv_fd: connection closed by server\00"
@.str.2 = [24 x i8] c"recv_fd: protocol error\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.cmsghdr*
%5 = alloca %struct.iovec
%6 = alloca %struct.msghdr
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %3
store %struct.cmsghdr* null, %struct.cmsghdr** %4
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 0
store i8* %7, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i32 0, i32 1
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 5
store %struct.iovec* %5, %struct.iovec** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 4
store i32* null, i32** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 3
store i64 0, i64* %14
%15 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%16 = icmp ne %struct.cmsghdr* %15, null
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = call %struct.cmsghdr* @xmalloc(i32 8)
store %struct.cmsghdr* %18, %struct.cmsghdr** %4
br label %19
19: ; preds = %17, %1
%20 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%21 = ptrtoint %struct.cmsghdr* %20 to i64
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 2
store i64 %21, i64* %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
store i32 8, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @recvmsg(i32 %24, %struct.msghdr* %6, i32 0)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
30: ; preds = %19
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @warnx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %52
35: ; preds = %30
%36 = load i8, i8* %7
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i32 0, i32 1
%41 = load i32, i32* %40
%42 = sext i32 %41 to i64
%43 = icmp eq i64 %42, 8
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %4
%46 = call i64 @CMSG_DATA(%struct.cmsghdr* %45)
%47 = inttoptr i64 %46 to i32*
%48 = load i32, i32* %47
store i32 %48, i32* %2
br label %52
49: ; preds = %39
br label %50
50: ; preds = %49, %35
%51 = call i32 @warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %2
br label %52
52: ; preds = %50, %44, %33, %28
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, i32, i64, i64, i32*, %struct.iovec* }
%struct.cmsghdr = type { i32 }
@.str = [16 x i8] c"recv_fd recvmsg\00"
@.str.1 = [37 x i8] c"recv_fd: connection closed by server\00"
@.str.2 = [24 x i8] c"recv_fd: protocol error\00"
define i32 @recv_fd(i32 %0) {
%2 = alloca %struct.iovec
%3 = alloca %struct.msghdr
%4 = alloca i8
%5 = bitcast %struct.iovec* %2 to i8*
%6 = bitcast %struct.msghdr* %3 to i8*
%7 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 0
store i8* %4, i8** %7
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 1
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 5
store %struct.iovec* %2, %struct.iovec** %9
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
%12 = bitcast i64* %11 to i8*
%13 = call %struct.cmsghdr* @xmalloc(i32 8)
%14 = ptrtoint %struct.cmsghdr* %13 to i64
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i64 %14, i64* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i32 8, i32* %16
%17 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @warn(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %37
21: ; preds = %1
%22 = icmp eq i32 %17, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 @warnx(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %37
25: ; preds = %21
%26 = load i8, i8* %4
%27 = icmp eq i8 %26, 0
%28 = load i32, i32* %16
%29 = icmp eq i32 %28, 8
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i64 @CMSG_DATA(%struct.cmsghdr* %13)
%33 = inttoptr i64 %32 to i32*
%34 = load i32, i32* %33
br label %37
35: ; preds = %25
%36 = call i32 @warnx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %31, %23, %19
%38 = phi i32 [ -1, %19 ], [ -2, %23 ], [ %34, %31 ], [ -3, %35 ]
ret i32 %38
}