#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 pid_map {
  int dummy;
};
struct cmsghdr {
  scalar_t__ cmsg_level;
  scalar_t__ cmsg_type;
  scalar_t__ cmsg_len;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ int16_t;

/* Variables and functions */
scalar_t__ CMSG_DATA(struct cmsghdr *);
scalar_t__ CMSG_LEN(int);
scalar_t__ SCM_RIGHTS;
scalar_t__ SOL_SOCKET;
int /*<<< orphan*/ debugf(char *, scalar_t__);
int /*<<< orphan*/ die(int);
scalar_t__ pid_map_replace(struct pid_map *, scalar_t__, int /*<<< orphan*/,
                           int /*<<< orphan*/ *);
scalar_t__ spawn_child(int const, int const);
int /*<<< orphan*/ xclose(int const);
pid_t create_process(struct cmsghdr *cmsg, struct pid_map *map, int16_t new_id,
                     int16_t *old_id_out) {
  if (cmsg->cmsg_level != SOL_SOCKET)
    die(24);
  if (cmsg->cmsg_type != SCM_RIGHTS)
    die(25);
  if (cmsg->cmsg_len != CMSG_LEN(2 * sizeof(int)))
    die(26);
  const int *fds = (int *)CMSG_DATA(cmsg);
  pid_t pid = spawn_child(fds[0], fds[1]);
  if (pid <= 0)
    die(22);
  debugf("executor: pid %d created", pid);
  if (pid_map_replace(map, pid, new_id, old_id_out) < 0)
    die(50);
  xclose(fds[1]);
  xclose(fds[0]);
  return pid;
}


x86 assembly (clang, -O0):
<create_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <create_process+0x38>
mov    $0x18,%edi
call   1070 <die@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <create_process+0x5a>
mov    $0x19,%edi
call   1070 <die@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    $0x8,%edi
call   1060 <CMSG_LEN@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     11fa <create_process+0x8a>
mov    $0x1a,%edi
call   1070 <die@plt>
mov    -0x8(%rbp),%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
call   1080 <spawn_child@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jg     1232 <create_process+0xc2>
mov    $0x16,%edi
call   1070 <die@plt>
mov    -0x30(%rbp),%rsi
lea    0xdc3(%rip),%rdi
call   1050 <debugf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1090 <pid_map_replace@plt>
cmp    $0x0,%rax
jge    126a <create_process+0xfa>
mov    $0x32,%edi
call   1070 <die@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <xclose@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <xclose@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_process>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
mov    (%rdi),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
je     119d <create_process+0x2d>
mov    $0x18,%edi
call   1070 <die@plt>
mov    0x8(%rbx),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
je     11b7 <create_process+0x47>
mov    $0x19,%edi
call   1070 <die@plt>
mov    0x10(%rbx),%rbp
mov    $0x8,%edi
call   1060 <CMSG_LEN@plt>
cmp    %rax,%rbp
je     11d4 <create_process+0x64>
mov    $0x1a,%edi
call   1070 <die@plt>
mov    %rbx,%rdi
call   1030 <CMSG_DATA@plt>
mov    %rax,%rbp
mov    (%rax),%edi
mov    0x4(%rax),%esi
call   1080 <spawn_child@plt>
mov    %rax,%rbx
test   %rax,%rax
jg     11fb <create_process+0x8b>
mov    $0x16,%edi
call   1070 <die@plt>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
call   1050 <debugf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14,%rcx
call   1090 <pid_map_replace@plt>
test   %rax,%rax
jns    122a <create_process+0xba>
mov    $0x32,%edi
call   1070 <die@plt>
mov    0x4(%rbp),%edi
call   1040 <xclose@plt>
mov    0x0(%rbp),%edi
call   1040 <xclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmsghdr = type { i64, i64, i64 }
%struct.pid_map = type { i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str = [25 x i8] c"executor: pid %d created\00"
define i64 @create_process(%struct.cmsghdr* %0, %struct.pid_map* %1, i32 %2, i32* %3) {
%5 = alloca %struct.cmsghdr*
%6 = alloca %struct.pid_map*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
store %struct.cmsghdr* %0, %struct.cmsghdr** %5
store %struct.pid_map* %1, %struct.pid_map** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%12 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @SOL_SOCKET
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @die(i32 24)
br label %18
18: ; preds = %16, %4
%19 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%20 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = load i64, i64* @SCM_RIGHTS
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @die(i32 25)
br label %26
26: ; preds = %24, %18
%27 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%28 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %27, i32 0, i32 2
%29 = load i64, i64* %28
%30 = call i64 @CMSG_LEN(i32 8)
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @die(i32 26)
br label %34
34: ; preds = %32, %26
%35 = load %struct.cmsghdr*, %struct.cmsghdr** %5
%36 = call i64 @CMSG_DATA(%struct.cmsghdr* %35)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %9
%38 = load i32*, i32** %9
%39 = getelementptr inbounds i32, i32* %38, i64 0
%40 = load i32, i32* %39
%41 = load i32*, i32** %9
%42 = getelementptr inbounds i32, i32* %41, i64 1
%43 = load i32, i32* %42
%44 = call i64 @spawn_child(i32 %40, i32 %43)
store i64 %44, i64* %10
%45 = load i64, i64* %10
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %34
%48 = call i32 @die(i32 22)
br label %49
49: ; preds = %47, %34
%50 = load i64, i64* %10
%51 = call i32 @debugf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %50)
%52 = load %struct.pid_map*, %struct.pid_map** %6
%53 = load i64, i64* %10
%54 = load i32, i32* %7
%55 = load i32*, i32** %8
%56 = call i64 @pid_map_replace(%struct.pid_map* %52, i64 %53, i32 %54, i32* %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @die(i32 50)
br label %60
60: ; preds = %58, %49
%61 = load i32*, i32** %9
%62 = getelementptr inbounds i32, i32* %61, i64 1
%63 = load i32, i32* %62
%64 = call i32 @xclose(i32 %63)
%65 = load i32*, i32** %9
%66 = getelementptr inbounds i32, i32* %65, i64 0
%67 = load i32, i32* %66
%68 = call i32 @xclose(i32 %67)
%69 = load i64, i64* %10
ret i64 %69
}

LLVM IR (-O2):
%struct.cmsghdr = type { i64, i64, i64 }
%struct.pid_map = type { i32 }
@SOL_SOCKET = global i64 0
@SCM_RIGHTS = global i64 0
@.str = [25 x i8] c"executor: pid %d created\00"
define i64 @create_process(%struct.cmsghdr* %0, %struct.pid_map* %1, i32 %2, i32* %3) {
%5 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @SOL_SOCKET
%8 = icmp eq i64 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @die(i32 24)
br label %11
11: ; preds = %9, %4
%12 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 1
%13 = load i64, i64* %12
%14 = load i64, i64* @SCM_RIGHTS
%15 = icmp eq i64 %13, %14
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @die(i32 25)
br label %18
18: ; preds = %16, %11
%19 = getelementptr inbounds %struct.cmsghdr, %struct.cmsghdr* %0, i64 0, i32 2
%20 = load i64, i64* %19
%21 = tail call i64 @CMSG_LEN(i32 8)
%22 = icmp eq i64 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = tail call i32 @die(i32 26)
br label %25
25: ; preds = %23, %18
%26 = tail call i64 @CMSG_DATA(%struct.cmsghdr* %0)
%27 = inttoptr i64 %26 to i32*
%28 = load i32, i32* %27
%29 = getelementptr inbounds i32, i32* %27, i64 1
%30 = load i32, i32* %29
%31 = tail call i64 @spawn_child(i32 %28, i32 %30)
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = tail call i32 @die(i32 22)
br label %35
35: ; preds = %33, %25
%36 = tail call i32 @debugf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %31)
%37 = tail call i64 @pid_map_replace(%struct.pid_map* %1, i64 %31, i32 %2, i32* %3)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = tail call i32 @die(i32 50)
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* %29
%43 = tail call i32 @xclose(i32 %42)
%44 = load i32, i32* %27
%45 = tail call i32 @xclose(i32 %44)
ret i64 %31
}