#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;
  int /*<<< orphan*/ *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 int /*<<< orphan*/ lport;
typedef int /*<<< orphan*/ *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);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_rresvport(int pipefd, int fd, int lport) {
  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 *)&lport;
  iov->iov_len = sizeof(lport);
  msg.msg_iov = iov;
  msg.msg_iovlen = 1;
  if (fd < 0) {
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
    lport = -1;
  } else {
    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);
    *(int *)CMSG_DATA(cmsg) = fd;
  }
  if (sendmsg(pipefd, &msg, 0) != sizeof(int)) {
    err("%p: privsep: sendmsg: %m\n");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<send_rresvport>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x4,-0x20(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x38(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b2 <send_rresvport+0x72>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0xffffffff,-0x10(%rbp)
jmp    1205 <send_rresvport+0xc5>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x40(%rbp),%rax
movl   $0x10,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x10,-0x34(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0xc4(%rbp),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cltq
cmp    $0x4,%rax
je     1237 <send_rresvport+0xf7>
lea    0xdda(%rip),%rdi
call   1060 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <send_rresvport+0xfe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_rresvport>:
push   %rbp
push   %rbx
sub    $0xb8,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    %edx,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    0x4(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x4,0x20(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x8(%rsp)
test   %ebp,%ebp
js     11c7 <send_rresvport+0x87>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
movl   $0x10,0x30(%rsp)
lea    0x30(%rsp),%rdi
mov    %rdi,0x10(%rsp)
movl   $0x10,0xc(%rsp)
call   1040 <CMSG_DATA@plt>
mov    %ebp,(%rax)
jmp    11e0 <send_rresvport+0xa0>
movq   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0x4,%eax
je     1206 <send_rresvport+0xc6>
lea    0xe04(%rip),%rdi
call   1060 <err@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %rbp
ret

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

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