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

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *);
int /*<<< orphan*/ CMSG_LEN(int);
int CMSG_SPACE(int);
int /*<<< orphan*/ SCM_RIGHTS;
int /*<<< orphan*/ SOL_SOCKET;
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int send_pid_fd(int conn, long pid, int fd) {
  struct msghdr msg = {0};
  struct cmsghdr *cmsg;
  struct iovec iov;
  char buf[CMSG_SPACE(sizeof(int))];
  iov.iov_base = &pid;
  iov.iov_len = sizeof(pid);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  if (fd > 0) {
    msg.msg_control = buf;
    msg.msg_controllen = sizeof(buf);
    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    *((int *)CMSG_DATA(cmsg)) = fd;
  } else {
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
  }
  return sendmsg(conn, &msg, 0);
}


x86 assembly (clang, -O0):
<send_pid_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x30(%rbp)
movaps %xmm0,-0x40(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x60(%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,-0x68(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x8,-0x50(%rbp)
lea    -0x58(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x40(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    123a <send_pid_fd+0xea>
mov    -0x78(%rbp),%rax
mov    -0x70(%rbp),%rcx
mov    %rcx,-0x38(%rbp)
mov    %eax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,-0x48(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dd2(%rip),%rax
mov    (%rax),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <CMSG_DATA@plt>
mov    -0x7c(%rbp),%ecx
mov    %ecx,(%rax)
jmp    1249 <send_pid_fd+0xf9>
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x60(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_pid_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %edx,%r15d
mov    %edi,%r14d
mov    %rsi,-0x58(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %rsp,%r12
mov    %eax,%edx
mov    %rsp,%rcx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%rcx
mov    %rcx,%rsp
lea    -0x58(%rbp),%rdx
mov    %rdx,-0x68(%rbp)
movl   $0x8,-0x60(%rbp)
lea    -0x68(%rbp),%rdx
mov    %rdx,-0x38(%rbp)
movl   $0x1,-0x50(%rbp)
test   %r15d,%r15d
jle    1203 <send_pid_fd+0xb3>
mov    %rcx,-0x48(%rbp)
mov    %eax,-0x40(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <CMSG_FIRSTHDR@plt>
mov    %rax,%rbx
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rbx)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    $0x4,%edi
call   1050 <CMSG_LEN@plt>
mov    %eax,(%rbx)
mov    %rbx,%rdi
call   1040 <CMSG_DATA@plt>
mov    %r15d,(%rax)
jmp    1212 <send_pid_fd+0xc2>
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i64*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @send_pid_fd(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.msghdr
%8 = alloca %struct.cmsghdr*
%9 = alloca %struct.iovec
%10 = alloca i8*
%11 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%12 = bitcast %struct.msghdr* %7 to i8*
%13 = call i32 @CMSG_SPACE(i32 4)
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
store i64* %5, i64** %17
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
store i32 8, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 3
store %struct.iovec* %9, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 0
store i32 1, i32* %20
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %41
23: ; preds = %3
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i8* %16, i8** %24
%25 = trunc i64 %14 to i32
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %7)
store %struct.cmsghdr* %27, %struct.cmsghdr** %8
%28 = load i32, i32* @SOL_SOCKET
%29 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%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** %8
%33 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %32, i32 0, i32 1
store i32 %31, i32* %33
%34 = call i32 @CMSG_LEN(i32 4)
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%36 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = load %struct.cmsghdr*, %struct.cmsghdr** %8
%39 = call i64 @CMSG_DATA(%struct.cmsghdr* %38)
%40 = inttoptr i64 %39 to i32*
store i32 %37, i32* %40
br label %44
41: ; preds = %3
%42 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store i8* null, i8** %42
%43 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 0, i32* %43
br label %44
44: ; preds = %41, %23
%45 = load i32, i32* %4
%46 = call i32 @sendmsg(i32 %45, %struct.msghdr* %7, i32 0)
%47 = load i8*, i8** %10
ret i32 %46
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i32, %struct.iovec*, i32 }
%struct.iovec = type { i64*, i32 }
%struct.cmsghdr = type { i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SCM_RIGHTS = global i32 0
define i32 @send_pid_fd(i32 %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca %struct.msghdr
%6 = alloca %struct.iovec
store i64 %1, i64* %4
%7 = bitcast %struct.msghdr* %5 to i8*
%8 = bitcast %struct.iovec* %6 to i8*
%9 = tail call i32 @CMSG_SPACE(i32 4)
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
store i64* %4, i64** %13
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
store i32 8, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 3
store %struct.iovec* %6, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %16
%17 = icmp sgt i32 %2, 0
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
br i1 %17, label %19, label %30
19: ; preds = %3
store i8* %12, i8** %18
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 %9, i32* %20
%21 = call %struct.cmsghdr* @CMSG_FIRSTHDR(%struct.msghdr* %5)
%22 = load i32, i32* @SOL_SOCKET
%23 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @SCM_RIGHTS
%25 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 1
store i32 %24, i32* %25
%26 = call i32 @CMSG_LEN(i32 4)
%27 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %21, i64 0, i32 0
store i32 %26, i32* %27
%28 = call i64 @CMSG_DATA(%struct.cmsghdr* %21)
%29 = inttoptr i64 %28 to i32*
store i32 %2, i32* %29
br label %32
30: ; preds = %3
store i8* null, i8** %18
%31 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 2
store i32 0, i32* %31
br label %32
32: ; preds = %30, %19
%33 = call i32 @sendmsg(i32 %0, %struct.msghdr* %5, i32 0)
ret i32 %33
}