#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ pid_str;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
scalar_t__ close(int);
int dup(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ execv(char const *, char **);
int fork();
int getpid();
int /*<<< orphan*/ setenv(char *, char *, int);
int snprintf(char *, int, char *, int);
pid_t spawn_cockpit_ws(const char *ws_path, int fd,
                       const char **cockpit_ws_args) {
  pid_t pid = fork();
  if (pid == -1) {
    err(EXIT_FAILURE, "fork() failed");
  } else if (pid > 0) {
    ;
    return pid;
  } else {
    const char *args[5 + 1] = {ws_path};
    int res;
    int duped_fd;
    char pid_str[10];
    duped_fd = dup(fd);
    if (duped_fd < 0)
      err(EXIT_FAILURE, "dup() failed");
    if (duped_fd != 3) {
      if (dup2(duped_fd, 3) < 0)
        err(EXIT_FAILURE, "dup2() failed");
      assert(close(duped_fd) == 0);
    }
    setenv("LISTEN_FDS", "1", 1);
    res = snprintf(pid_str, sizeof(pid_str), "%i", getpid());
    assert(res < sizeof(pid_str));
    setenv("LISTEN_PID", pid_str, 1);
    for (int i = 0; cockpit_ws_args[i] != NULL; i++)
      args[i + 1] = cockpit_ws_args[i];
    execv(ws_path, (char **)args);
    err(EXIT_FAILURE, "spawning cockpit-ws instance failed");
  }
}


x86 assembly (clang, -O0):
<spawn_cockpit_ws>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f4 <spawn_cockpit_ws+0x44>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   10b0 <err@plt>
jmp    136d <spawn_cockpit_ws+0x1bd>
cmpl   $0x0,-0x24(%rbp)
jle    1209 <spawn_cockpit_ws+0x59>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136d <spawn_cockpit_ws+0x1bd>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <dup@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    124b <spawn_cockpit_ws+0x9b>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
call   10b0 <err@plt>
cmpl   $0x3,-0x68(%rbp)
je     129a <spawn_cockpit_ws+0xea>
mov    -0x68(%rbp),%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1281 <spawn_cockpit_ws+0xd1>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
call   10b0 <err@plt>
mov    -0x68(%rbp),%edi
call   1090 <close@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
lea    0xd88(%rip),%rdi
lea    0xd8c(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
lea    -0x72(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%ecx
mov    $0xa,%esi
lea    0xd63(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x64(%rbp)
movslq -0x64(%rbp),%rax
cmp    $0xa,%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
lea    -0x72(%rbp),%rsi
lea    0xd3e(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
movl   $0x0,-0x78(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x78(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1346 <spawn_cockpit_ws+0x196>
mov    -0x20(%rbp),%rax
movslq -0x78(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x78(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rcx,-0x60(%rbp,%rax,8)
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    130c <spawn_cockpit_ws+0x15c>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   10c0 <execv@plt>
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
lea    0xce1(%rip),%rsi
call   10b0 <err@plt>
jmp    136d <spawn_cockpit_ws+0x1bd>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_cockpit_ws>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     11f7 <spawn_cockpit_ws+0x57>
test   %r12d,%r12d
jg     1301 <spawn_cockpit_ws+0x161>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %r14,(%rsp)
mov    %ebp,%edi
call   1070 <dup@plt>
mov    %eax,%r15d
test   %eax,%eax
js     120c <spawn_cockpit_ws+0x6c>
cmp    $0x3,%r15d
jne    1221 <spawn_cockpit_ws+0x81>
jmp    125e <spawn_cockpit_ws+0xbe>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
jmp    12fc <spawn_cockpit_ws+0x15c>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   10a0 <err@plt>
mov    %r15d,%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
test   %rax,%rax
jns    1248 <spawn_cockpit_ws+0xa8>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   10a0 <err@plt>
mov    %r15d,%edi
call   1080 <close@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1090 <assert@plt>
lea    0xdc4(%rip),%rdi
lea    0xdc8(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb2(%rip),%rdx
lea    0x36(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
xor    %edi,%edi
cmp    $0xa,%eax
setb   %dil
call   1090 <assert@plt>
lea    0xd8c(%rip),%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    (%rbx),%rax
test   %rax,%rax
je     12e1 <spawn_cockpit_ws+0x141>
mov    $0x8,%ecx
nopw   0x0(%rax,%rax,1)
mov    %rax,(%rsp,%rcx,1)
mov    (%rbx,%rcx,1),%rax
add    $0x8,%rcx
test   %rax,%rax
jne    12d0 <spawn_cockpit_ws+0x130>
mov    %rsp,%rsi
mov    %r14,%rdi
call   10b0 <execv@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd48(%rip),%rsi
call   10a0 <err@plt>
mov    %r12d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [13 x i8] c"dup() failed\00"
@.str.2 = [14 x i8] c"dup2() failed\00"
@.str.3 = [11 x i8] c"LISTEN_FDS\00"
@.str.4 = [2 x i8] c"1\00"
@.str.5 = [3 x i8] c"%i\00"
@.str.6 = [11 x i8] c"LISTEN_PID\00"
@.str.7 = [36 x i8] c"spawning cockpit-ws instance failed\00"
define i32 @spawn_cockpit_ws(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [6 x i8*]
%10 = alloca i32
%11 = alloca i32
%12 = alloca [10 x i8]
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%14 = call i32 (...) @fork()
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @err(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %91
20: ; preds = %3
%21 = load i32, i32* %8
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %91
25: ; preds = %20
%26 = bitcast [6 x i8*]* %9 to i8*
%27 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 0
%28 = load i8*, i8** %5
store i8* %28, i8** %27
%29 = load i32, i32* %6
%30 = call i32 @dup(i32 %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @err(i32 %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %25
%37 = load i32, i32* %11
%38 = icmp ne i32 %37, 3
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = load i32, i32* %11
%41 = call i64 @dup2(i32 %40, i32 3)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @err(i32 %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %43, %39
%47 = load i32, i32* %11
%48 = call i64 @close(i32 %47)
%49 = icmp eq i64 %48, 0
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
br label %52
52: ; preds = %46, %36
%53 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%55 = call i32 (...) @getpid()
%56 = call i32 @snprintf(i8* %54, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %55)
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = icmp ult i64 %58, 10
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
%62 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%63 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %62, i32 1)
store i32 0, i32* %13
br label %64
64: ; preds = %81, %52
%65 = load i8**, i8*** %7
%66 = load i32, i32* %13
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
%69 = load i8*, i8** %68
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %84
71: ; preds = %64
%72 = load i8**, i8*** %7
%73 = load i32, i32* %13
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %72, i64 %74
%76 = load i8*, i8** %75
%77 = load i32, i32* %13
%78 = add nsw i32 %77, 1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 %79
store i8* %76, i8** %80
br label %81
81: ; preds = %71
%82 = load i32, i32* %13
%83 = add nsw i32 %82, 1
store i32 %83, i32* %13
br label %64
84: ; preds = %64
%85 = load i8*, i8** %5
%86 = getelementptr inbounds [6 x i8*], [6 x i8*]* %9, i64 0, i64 0
%87 = call i32 @execv(i8* %85, i8** %86)
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @err(i32 %88, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
br label %90
90: ; preds = %84
br label %91
91: ; preds = %23, %90, %17
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [14 x i8] c"fork() failed\00"
@.str.1 = [13 x i8] c"dup() failed\00"
@.str.2 = [14 x i8] c"dup2() failed\00"
@.str.3 = [11 x i8] c"LISTEN_FDS\00"
@.str.4 = [2 x i8] c"1\00"
@.str.5 = [3 x i8] c"%i\00"
@.str.6 = [11 x i8] c"LISTEN_PID\00"
@.str.7 = [36 x i8] c"spawning cockpit-ws instance failed\00"
define i32 @spawn_cockpit_ws(i8* %0, i32 %1, i8** nocapture %2) {
%4 = alloca [6 x i8*]
%5 = alloca [10 x i8]
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @err(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %57
11: ; preds = %3
%12 = icmp sgt i32 %6, 0
br i1 %12, label %57, label %13
13: ; preds = %11
%14 = bitcast [6 x i8*]* %4 to i8*
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
store i8* %0, i8** %15
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%17 = tail call i32 @dup(i32 %1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @err(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %13
%23 = icmp eq i32 %17, 3
br i1 %23, label %35, label %24
24: ; preds = %19, %22
%25 = tail call i64 @dup2(i32 %17, i32 3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @err(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %27, %24
%31 = tail call i64 @close(i32 %17)
%32 = icmp eq i64 %31, 0
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
br label %35
35: ; preds = %30, %22
%36 = tail call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%37 = tail call i32 (...) @getpid()
%38 = call i32 @snprintf(i8* %16, i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = icmp ult i32 %38, 10
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %16, i32 1)
%43 = load i8*, i8** %2
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %49, %35
%46 = call i32 @execv(i8* %0, i8** %15)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @err(i32 %47, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
br label %57
49: ; preds = %35, %49
%50 = phi i64 [ %52, %49 ], [ 0, %35 ]
%51 = phi i8* [ %55, %49 ], [ %43, %35 ]
%52 = add nuw nsw i64 %50, 1
%53 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %52
store i8* %51, i8** %53
%54 = getelementptr inbounds i8*, i8** %2, i64 %52
%55 = load i8*, i8** %54
%56 = icmp eq i8* %55, null
br i1 %56, label %45, label %49
57: ; preds = %8, %45, %11
ret i32 %6
}