#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;
  struct iovec *msg_iov;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
struct cmsghdr {
  scalar_t__ cmsg_len;
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ msghdr;
typedef int /*<<< orphan*/ message;
typedef int /*<<< orphan*/ ch;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
scalar_t__ CMSG_LEN(int);
int CMSG_SPACE(int);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ bzero(struct msghdr *, int);
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ errx(int, char *, char const *, ...);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
void recvfd(const char *test, int sockfd, int *recvfd) {
  struct cmsghdr *cmsghdr;
  char message[CMSG_SPACE(sizeof(int))];
  struct msghdr msghdr;
  struct iovec iovec;
  ssize_t len;
  char ch;
  bzero(&msghdr, sizeof(msghdr));
  ch = 0;
  msghdr.msg_control = message;
  msghdr.msg_controllen = sizeof(message);
  iovec.iov_base = &ch;
  iovec.iov_len = sizeof(ch);
  msghdr.msg_iov = &iovec;
  msghdr.msg_iovlen = 1;
  iovec.iov_len = sizeof(ch);
  msghdr.msg_iov = &iovec;
  msghdr.msg_iovlen = 1;
  len = recvmsg(sockfd, &msghdr, 0);
  if (len < 0)
    err(-1, "%s: recvmsg", test);
  if (len != sizeof(ch))
    errx(-1, "%s: recvmsg: %zd bytes received", test, len);
  cmsghdr = CMSG_FIRSTHDR(&msghdr);
  if (cmsghdr == NULL)
    errx(-1, "%s: recvmsg: did not receive control message", test);
  if (cmsghdr->cmsg_len != CMSG_LEN(sizeof(int)) ||
      cmsghdr->cmsg_level != SOL_SOCKET || cmsghdr->cmsg_type != SCM_RIGHTS)
    errx(-1, "%s: recvmsg: did not receive single-fd message", test);
  *recvfd = *(int *)CMSG_DATA(cmsghdr);
  if (*recvfd == -1)
    errx(-1, "%s: recvmsg: received fd -1", test);
}


x86 assembly (clang, -O0):
<recvfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%rax
movb   $0x0,-0x5d(%rbp)
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x5d(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x1,-0x50(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
movl   $0x1,-0x50(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    124e <recvfd+0xce>
mov    -0x8(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xdb7(%rip),%rsi
call   1070 <err@plt>
movslq -0x5c(%rbp),%rax
cmp    $0x1,%rax
je     1276 <recvfd+0xf6>
mov    -0x8(%rbp),%rdx
mov    -0x5c(%rbp),%ecx
mov    $0xffffffff,%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
lea    -0x48(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12a5 <recvfd+0x125>
mov    -0x8(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x78(%rbp)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
cmp    %rcx,%rax
jne    12fa <recvfd+0x17a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2d0f(%rip),%rcx
cmp    (%rcx),%rax
jne    12fa <recvfd+0x17a>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    0x2ce7(%rip),%rcx
cmp    (%rcx),%rax
je     1311 <recvfd+0x191>
mov    -0x8(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xd4f(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x20(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1346 <recvfd+0x1c6>
mov    -0x8(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xd49(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%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    $0x38,%rsp
mov    %rdx,-0x38(%rbp)
mov    %esi,%r12d
mov    %rdi,%r15
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%ebx
mov    %eax,%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x50(%rbp),%r13
mov    %r13,%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
movb   $0x0,-0x29(%rbp)
mov    %r14,-0x50(%rbp)
mov    %ebx,-0x48(%rbp)
lea    -0x29(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x1,-0x58(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x44(%rbp)
mov    %r12d,%edi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,%ebx
test   %eax,%eax
js     120e <recvfd+0x8e>
cmp    $0x1,%ebx
jne    1222 <recvfd+0xa2>
jmp    123a <recvfd+0xba>
lea    0xdeb(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
call   1070 <err@plt>
lea    0xde3(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <errx@plt>
lea    -0x50(%rbp),%rdi
call   10a0 <CMSG_FIRSTHDR@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1261 <recvfd+0xe1>
lea    0xdda(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
mov    (%r14),%rbx
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
cmp    %rax,%rbx
jne    1293 <recvfd+0x113>
mov    0x8(%r14),%rax
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    1293 <recvfd+0x113>
mov    0x10(%r14),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
je     12a9 <recvfd+0x129>
lea    0xdbf(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
mov    %r14,%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    -0x38(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    12d4 <recvfd+0x154>
lea    0xdc3(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <errx@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i64, i64, i64 }
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
@.str = [12 x i8] c"%s: recvmsg\00"
@.str.1 = [32 x i8] c"%s: recvmsg: %zd bytes received\00"
@.str.2 = [45 x i8] c"%s: recvmsg: did not receive control message\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str.3 = [47 x i8] c"%s: recvmsg: did not receive single-fd message\00"
@.str.4 = [28 x i8] c"%s: recvmsg: received fd -1\00"
define void @recvfd(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.cmsghdr*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.msghdr
%11 = alloca %struct.iovec
%12 = alloca i32
%13 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%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 = call i32 @bzero(%struct.msghdr* %10, i32 24)
store i8 0, i8* %13
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i8* %17, i8** %19
%20 = trunc i64 %15 to i32
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 0
store i8* %13, i8** %22
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 1, i32* %25
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %11, i32 0, i32 1
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 3
store %struct.iovec* %11, %struct.iovec** %27
%28 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 2
store i32 1, i32* %28
%29 = load i32, i32* %5
%30 = call i32 @recvmsg(i32 %29, %struct.msghdr* %10, i32 0)
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %3
%34 = load i8*, i8** %4
%35 = call i32 @err(i32 -1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %33, %3
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = icmp ne i64 %38, 1
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = load i32, i32* %12
%43 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %41, i32 %42)
br label %44
44: ; preds = %40, %36
%45 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %10)
store %struct.cmsghdr* %45, %struct.cmsghdr** %7
%46 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%47 = icmp eq %struct.cmsghdr* %46, null
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %4
%50 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %48, %44
%52 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%53 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %52, i32 0, i32 0
%54 = load i64, i64* %53
%55 = call i64 @CMSG_LEN(i32 4)
%56 = icmp ne i64 %54, %55
br i1 %56, label %69, label %57
57: ; preds = %51
%58 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%59 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %58, i32 0, i32 1
%60 = load i64, i64* %59
%61 = load i64, i64* @SOL_SOCKET
%62 = icmp ne i64 %60, %61
br i1 %62, label %69, label %63
63: ; preds = %57
%64 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%65 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %64, i32 0, i32 2
%66 = load i64, i64* %65
%67 = load i64, i64* @SCM_RIGHTS
%68 = icmp ne i64 %66, %67
br i1 %68, label %69, label %72
69: ; preds = %63, %57, %51
%70 = load i8*, i8** %4
%71 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %70)
br label %72
72: ; preds = %69, %63
%73 = load %struct.cmsghdr*, %struct.cmsghdr** %7
%74 = call i64 @CMSG_DATA(%struct.cmsghdr* %73)
%75 = inttoptr i64 %74 to i32*
%76 = load i32, i32* %75
%77 = load i32*, i32** %6
store i32 %76, i32* %77
%78 = load i32*, i32** %6
%79 = load i32, i32* %78
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %84
81: ; preds = %72
%82 = load i8*, i8** %4
%83 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %81, %72
%85 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, %struct.iovec* }
%struct.iovec = type { i8*, i32 }
%struct.cmsghdr = type { i64, i64, i64 }
@.str = [12 x i8] c"%s: recvmsg\00"
@.str.1 = [32 x i8] c"%s: recvmsg: %zd bytes received\00"
@.str.2 = [45 x i8] c"%s: recvmsg: did not receive control message\00"
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str.3 = [47 x i8] c"%s: recvmsg: did not receive single-fd message\00"
@.str.4 = [28 x i8] c"%s: recvmsg: received fd -1\00"
define void @recvfd(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = alloca i8
%7 = tail call i32 @CMSG_SPACE(i32 4)
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast %struct.msghdr* %4 to i8*
%11 = bitcast %struct.iovec* %5 to i8*
%12 = call i32 @bzero(%struct.msghdr* %4, i32 24)
store i8 0, i8* %6
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i8* %9, i8** %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i32 %7, i32* %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %6, i8** %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 1, i32* %16
store %struct.iovec* %5, %struct.iovec** %17
store i32 1, i32* %18
%19 = call i32 @recvmsg(i32 %1, %struct.msghdr* %4, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @err(i32 -1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
23: ; preds = %3
%24 = icmp eq i32 %19, 1
br i1 %24, label %27, label %25
25: ; preds = %21, %23
%26 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %19)
br label %27
27: ; preds = %25, %23
%28 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %4)
%29 = icmp eq %struct.cmsghdr* %28, null
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %32
32: ; preds = %30, %27
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i64 0, i32 0
%34 = load i64, i64* %33
%35 = call i64 @CMSG_LEN(i32 4)
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i64 0, i32 1
%39 = load i64, i64* %38
%40 = load i64, i64* @SOL_SOCKET
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i64 0, i32 2
%44 = load i64, i64* %43
%45 = load i64, i64* @SCM_RIGHTS
%46 = icmp eq i64 %44, %45
br i1 %46, label %49, label %47
47: ; preds = %42, %37, %32
%48 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %49
49: ; preds = %47, %42
%50 = call i64 @CMSG_DATA(%struct.cmsghdr* %28)
%51 = inttoptr i64 %50 to i32*
%52 = load i32, i32* %51
store i32 %52, i32* %2
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 (i32, i8*, i8*, ...) @errx(i32 -1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %56
56: ; preds = %54, %49
ret void
}