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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
struct cmsghdr *CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
int MSG_CTRUNC;
int MSG_TRUNC;
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int testfd(int);
int recv_fd(int desc) {
  unsigned char buf[(sizeof(struct cmsghdr) + sizeof(int))];
  struct msghdr msg;
  struct cmsghdr *cmsg;
  struct iovec iov[1];
  char dummy;
  int ret = 2;
  memset(&msg, 0, sizeof(msg));
  iov[0].iov_base = &dummy;
  iov[0].iov_len = 1;
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  msg.msg_control = buf;
  msg.msg_controllen = sizeof(buf);
  if (recvmsg(desc, &msg, 0) == -1) {
    perror("recvmsg failed!");
    return -1;
  }
  if ((msg.msg_flags & MSG_TRUNC) || (msg.msg_flags & MSG_CTRUNC)) {
    fprintf(stderr, "control message truncated");
    return -1;
  }
  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
       cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_len == (sizeof(struct cmsghdr) + sizeof(int)) &&
        cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
      int fd = *(int *)CMSG_DATA(cmsg);
      ret = testfd(fd);
      close(fd);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<recv_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x2,-0x78(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    -0x71(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x1,-0x68(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1c,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
cmp    $0xffffffff,%eax
jne    1211 <recv_fd+0x81>
lea    0xe00(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <recv_fd+0x167>
mov    -0x3c(%rbp),%eax
mov    0x2dc5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    123b <recv_fd+0xab>
mov    -0x3c(%rbp),%eax
mov    0x2d98(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     125c <recv_fd+0xcc>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <recv_fd+0x167>
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12f1 <recv_fd+0x161>
mov    -0x58(%rbp),%rax
movslq (%rax),%rax
cmp    $0x1c,%rax
jne    12d6 <recv_fd+0x146>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    12d6 <recv_fd+0x146>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2d14(%rip),%rcx
cmp    (%rcx),%rax
jne    12d6 <recv_fd+0x146>
mov    -0x58(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%edi
call   10b0 <testfd@plt>
mov    %eax,-0x78(%rbp)
mov    -0x7c(%rbp),%edi
call   1050 <close@plt>
jmp    12db <recv_fd+0x14b>
mov    -0x58(%rbp),%rsi
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_NXTHDR@plt>
mov    %rax,-0x58(%rbp)
jmp    1269 <recv_fd+0xd9>
mov    -0x78(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
lea    0xf(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x1,0x38(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x1,0x10(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1c,0x20(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
cmp    $0xffffffff,%eax
je     12c9 <recv_fd+0x139>
mov    0x24(%rsp),%eax
mov    0x2dd6(%rip),%rcx
test   %eax,(%rcx)
jne    1219 <recv_fd+0x89>
mov    0x2db3(%rip),%rcx
test   %eax,(%rcx)
je     1246 <recv_fd+0xb6>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
test   %rax,%rax
je     12da <recv_fd+0x14a>
mov    %rax,%rbx
mov    $0x2,%r15d
mov    0x2d7f(%rip),%r12
mov    0x2d50(%rip),%r13
lea    0x10(%rsp),%r14
jmp    1293 <recv_fd+0x103>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <CMSG_NXTHDR@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1234 <recv_fd+0xa4>
cmpl   $0x1c,(%rbx)
jne    1280 <recv_fd+0xf0>
mov    0x8(%rbx),%rax
cmp    (%r12),%rax
jne    1280 <recv_fd+0xf0>
mov    0x10(%rbx),%rax
cmp    0x0(%r13),%rax
jne    1280 <recv_fd+0xf0>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <testfd@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1280 <recv_fd+0xf0>
lea    0xd30(%rip),%rdi
call   1090 <perror@plt>
jmp    122e <recv_fd+0x9e>
mov    $0x2,%r15d
jmp    1234 <recv_fd+0xa4>

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i64, i64 }
@.str = [16 x i8] c"recvmsg failed!\00"
@MSG_TRUNC = global i32 0
@MSG_CTRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"control message truncated\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @recv_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [28 x i8]
%5 = alloca %struct.msghdr
%6 = alloca %struct.cmsghdr*
%7 = alloca [1 x %struct.iovec]
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 2, i32* %9
%11 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 32)
%12 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 0
store i8* %8, i8** %13
%14 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 1
store i32 1, i32* %15
%16 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
store %struct.iovec* %16, %struct.iovec** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds [28 x i8], [28 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 1
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 28, i32* %21
%22 = load i32, i32* %3
%23 = call i32 @recvmsg(i32 %22, %struct.msghdr* %5, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %1
%26 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %80
27: ; preds = %1
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
%29 = load i32, i32* %28
%30 = load i32, i32* @MSG_TRUNC
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %27
%34 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
%35 = load i32, i32* %34
%36 = load i32, i32* @MSG_CTRUNC
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33, %27
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %80
42: ; preds = %33
%43 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
store %struct.cmsghdr* %43, %struct.cmsghdr** %6
br label %44
44: ; preds = %75, %42
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%46 = icmp ne %struct.cmsghdr* %45, null
br i1 %46, label %47, label %78
47: ; preds = %44
%48 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = icmp eq i64 %51, 28
br i1 %52, label %53, label %74
53: ; preds = %47
%54 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%55 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = load i64, i64* @SOL_SOCKET
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %74
59: ; preds = %53
%60 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%61 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %60, i32 0, i32 2
%62 = load i64, i64* %61
%63 = load i64, i64* @SCM_RIGHTS
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %74
65: ; preds = %59
%66 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%67 = call i64 @CMSG_DATA(%struct.cmsghdr* %66)
%68 = inttoptr i64 %67 to i32*
%69 = load i32, i32* %68
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = call i32 @testfd(i32 %70)
store i32 %71, i32* %9
%72 = load i32, i32* %10
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %65, %59, %53, %47
br label %75
75: ; preds = %74
%76 = load %struct.cmsghdr*, %struct.cmsghdr** %6
%77 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %5, %struct.cmsghdr* %76)
store %struct.cmsghdr* %77, %struct.cmsghdr** %6
br label %44
78: ; preds = %44
%79 = load i32, i32* %9
store i32 %79, i32* %2
br label %80
80: ; preds = %78, %39, %25
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i32, i64, i64 }
@.str = [16 x i8] c"recvmsg failed!\00"
@MSG_TRUNC = global i32 0
@MSG_CTRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"control message truncated\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
define i32 @recv_fd(i32 %0) {
%2 = alloca [28 x i8]
%3 = alloca %struct.msghdr
%4 = alloca [1 x %struct.iovec]
%5 = alloca i8
%6 = getelementptr inbounds [28 x i8], [28 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.msghdr* %3 to i8*
%8 = bitcast [1 x %struct.iovec]* %4 to i8*
%9 = call i32 @memset(%struct.msghdr* %3, i32 0, i32 32)
%10 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0
%11 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* %5, i8** %11
%12 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 4
store %struct.iovec* %10, %struct.iovec** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 0
store i32 1, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 1
store i8* %6, i8** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
store i32 28, i32* %16
%17 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %63
21: ; preds = %1
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
%23 = load i32, i32* %22
%24 = load i32, i32* @MSG_TRUNC
%25 = and i32 %24, %23
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @MSG_CTRUNC
%29 = and i32 %28, %23
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27, %21
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %63
34: ; preds = %27
%35 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %3)
%36 = icmp eq %struct.cmsghdr* %35, null
br i1 %36, label %63, label %37
37: ; preds = %34, %59
%38 = phi i32 [ %60, %59 ], [ 2, %34 ]
%39 = phi %struct.cmsghdr* [ %61, %59 ], [ %35, %34 ]
%40 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i64 0, i32 0
%41 = load i32, i32* %40
%42 = icmp eq i32 %41, 28
br i1 %42, label %43, label %59
43: ; preds = %37
%44 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i64 0, i32 1
%45 = load i64, i64* %44
%46 = load i64, i64* @SOL_SOCKET
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %59
48: ; preds = %43
%49 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %39, i64 0, i32 2
%50 = load i64, i64* %49
%51 = load i64, i64* @SCM_RIGHTS
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = call i64 @CMSG_DATA(%struct.cmsghdr* %39)
%55 = inttoptr i64 %54 to i32*
%56 = load i32, i32* %55
%57 = call i32 @testfd(i32 %56)
%58 = call i32 @close(i32 %56)
br label %59
59: ; preds = %37, %43, %48, %53
%60 = phi i32 [ %57, %53 ], [ %38, %48 ], [ %38, %43 ], [ %38, %37 ]
%61 = call %struct.cmsghdr* @CMSG_NXTHDR(%struct.msghdr* %3, %struct.cmsghdr* %39)
%62 = icmp eq %struct.cmsghdr* %61, null
br i1 %62, label %63, label %37
63: ; preds = %59, %34, %31, %19
%64 = phi i32 [ -1, %19 ], [ -1, %31 ], [ 2, %34 ], [ %60, %59 ]
ret i32 %64
}