#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;
  struct iovec *msg_iov;
};
struct iovec {
  int iov_len;
  void *iov_base;
};
struct cmsghdr {
  int cmsg_len;
  int /*<<< orphan*/ cmsg_type;
  int /*<<< orphan*/ cmsg_level;
};
typedef int /*<<< orphan*/ msg;
typedef scalar_t__ caddr_t;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
scalar_t__ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int recv_rresvport(int pipefd, int *lptr) {
  int fd = -1;
  struct iovec iov[1];
  struct msghdr msg;
  struct cmsghdr *cmsg;
  char *buf[sizeof(struct cmsghdr) + sizeof(int)];
  cmsg = (struct cmsghdr *)&buf;
  memset(&msg, 0, sizeof(msg));
  iov->iov_base = (void *)lptr;
  iov->iov_len = sizeof(int);
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(int);
  msg.msg_control = (caddr_t)cmsg;
  msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(int);
  if (recvmsg(pipefd, &msg, 0) < 0)
    err("%p: privsep: recvmsg: %m\n");
  if (*lptr < 0)
    return (-1);
  fd = *(int *)CMSG_DATA(cmsg);
  return (fd);
}


x86 assembly (clang, -O0):
<recv_rresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    -0xd0(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x4,-0x30(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x48(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x50(%rbp),%rax
movl   $0x10,(%rax)
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x10,-0x44(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0x0,%rax
jge    11ef <recv_rresvport+0xaf>
lea    0xe16(%rip),%rdi
call   1050 <err@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1208 <recv_rresvport+0xc8>
movl   $0xffffffff,-0x4(%rbp)
jmp    121c <recv_rresvport+0xdc>
mov    -0x50(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_rresvport>:
push   %rbp
push   %r14
push   %rbx
sub    $0xb0,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %rbx,0x8(%rsp)
movl   $0x4,(%rsp)
mov    %rsp,%rax
mov    %rax,0x28(%rsp)
movabs $0x1000000001,%rax
mov    %rax,0x18(%rsp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
movl   $0x10,0x30(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x20(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
test   %rax,%rax
js     11d7 <recv_rresvport+0x97>
cmpl   $0x0,(%rbx)
js     11e8 <recv_rresvport+0xa8>
lea    0x30(%rsp),%rdi
call   1030 <CMSG_DATA@plt>
mov    (%rax),%eax
jmp    11ed <recv_rresvport+0xad>
lea    0xe22(%rip),%rdi
call   1050 <err@plt>
cmpl   $0x0,(%rbx)
jns    11c9 <recv_rresvport+0x89>
mov    $0xffffffff,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, i8* }
%struct.msghdr = type { i32, i32, i64, %struct.iovec* }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
@.str = [26 x i8] c"%p: privsep: recvmsg: %m\0A\00"
define i32 @recv_rresvport(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [1 x %struct.iovec]
%8 = alloca %struct.msghdr
%9 = alloca %struct.cmsghdr*
%10 = alloca [16 x i8*]
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 -1, i32* %6
%11 = bitcast [16 x i8*]* %10 to %struct.cmsghdr*
store %struct.cmsghdr* %11, %struct.cmsghdr** %9
%12 = call i32 @memset(%struct.msghdr* %8, i32 0, i32 24)
%13 = load i32*, i32** %5
%14 = bitcast i32* %13 to i8*
%15 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 1
store i8* %14, i8** %16
%17 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i32 0, i32 0
store i32 4, i32* %18
%19 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %7, i64 0, i64 0
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store %struct.iovec* %19, %struct.iovec** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %21
%22 = load i32, i32* @SOL_SOCKET
%23 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%24 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %23, i32 0, i32 2
store i32 %22, i32* %24
%25 = load i32, i32* @SCM_RIGHTS
%26 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%29 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %28, i32 0, i32 0
store i32 16, i32* %29
%30 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%31 = ptrtoint %struct.cmsghdr* %30 to i64
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i64 %31, i64* %32
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i32 16, i32* %33
%34 = load i32, i32* %4
%35 = call i64 @recvmsg(i32 %34, %struct.msghdr* %8, i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %2
%38 = call i32 @err(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %2
%40 = load i32*, i32** %5
%41 = load i32, i32* %40
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 -1, i32* %3
br label %50
44: ; preds = %39
%45 = load %struct.cmsghdr*, %struct.cmsghdr** %9
%46 = call i64 @CMSG_DATA(%struct.cmsghdr* %45)
%47 = inttoptr i64 %46 to i32*
%48 = load i32, i32* %47
store i32 %48, i32* %6
%49 = load i32, i32* %6
store i32 %49, i32* %3
br label %50
50: ; preds = %44, %43
%51 = load i32, i32* %3
ret i32 %51
}

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