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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int sigemptyset(int /*<<< orphan*/ *);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
pid_t launch(char *name, int sock, int count, char **args) {
  fprintf(stderr, "%s: launching child %s\n", name, args[2]);
  pid_t pid = fork();
  while (pid == -1) {
    fprintf(stderr, "%s: fork(): %s\n", name, strerror(errno));
    sleep(1);
    pid = fork();
  }
  if (pid == 0) {
    close(sock);
    sigset_t child_mask;
    int ret = sigemptyset(&child_mask);
    if (ret != -1)
      ret = sigprocmask(SIG_SETMASK, &child_mask, NULL);
    if (ret == -1) {
      fprintf(stderr, "%s: setting the signal mask failed: %s\n", name,
              strerror(errno));
      exit(EXIT_FAILURE);
    }
    size_t i;
    for (i = 0; i < count - 2; i++) {
      args[i] = args[i + 2];
    }
    args[i] = (char *)NULL;
    execv(args[0], args);
    fprintf(stderr, "%s: execv(): %s\n", name, strerror(errno));
    exit(EXIT_FAILURE);
  }
  return pid;
}


x86 assembly (clang, -O0):
<launch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe3e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    122c <launch+0x9c>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
jmp    11d1 <launch+0x41>
cmpl   $0x0,-0x1c(%rbp)
jne    136b <launch+0x1db>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
lea    -0x20(%rbp),%rdi
call   1060 <sigemptyset@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     126d <launch+0xdd>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12bd <launch+0x12d>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd7e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1305 <launch+0x175>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    0x10(%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12c5 <launch+0x135>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <execv@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xcf8(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,0x4(%rsp)
mov    %esi,0x8(%rsp)
mov    %rdi,%rbx
mov    0x2e15(%rip),%r12
mov    (%r12),%edi
lea    0x10(%rcx),%rax
mov    %rax,0x10(%rsp)
mov    0x10(%rcx),%rcx
lea    0xe25(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    0x2de6(%rip),%r13
lea    0xe27(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
jne    1236 <launch+0x96>
mov    (%r12),%r15d
mov    0x0(%r13),%edi
call   1090 <strerror@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1200 <launch+0x60>
test   %eax,%eax
je     1249 <launch+0xa9>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xc(%rsp),%rdi
call   1060 <sigemptyset@plt>
cmp    $0xffffffff,%eax
je     127b <launch+0xdb>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    1291 <launch+0xf1>
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1090 <strerror@plt>
lea    0xd99(%rip),%rsi
jmp    12dd <launch+0x13d>
addl   $0xfffffffe,0x4(%rsp)
jne    129c <launch+0xfc>
xor    %ebp,%ebp
jmp    12b6 <launch+0x116>
movslq 0x4(%rsp),%rbp
lea    0x0(,%rbp,8),%rdx
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
call   1070 <memmove@plt>
movq   $0x0,(%r14,%rbp,8)
mov    (%r14),%rdi
mov    %r14,%rsi
call   10b0 <execv@plt>
mov    (%r12),%ebp
mov    0x0(%r13),%edi
call   1090 <strerror@plt>
lea    0xd73(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"%s: launching child %s\0A\00"
@.str.1 = [16 x i8] c"%s: fork(): %s\0A\00"
@errno = global i32 0
@SIG_SETMASK = global i32 0
@.str.2 = [40 x i8] c"%s: setting the signal mask failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [17 x i8] c"%s: execv(): %s\0A\00"
define i32 @launch(i8* %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %5
%15 = load i8**, i8*** %8
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %9
br label %20
20: ; preds = %23, %4
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %27)
%29 = call i32 @sleep(i32 1)
%30 = call i32 (...) @fork()
store i32 %30, i32* %9
br label %20
31: ; preds = %20
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %89
34: ; preds = %31
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = call i32 @sigemptyset(i32* %10)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp ne i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* @SIG_SETMASK
%42 = call i32 @sigprocmask(i32 %41, i32* %10, i32* null)
store i32 %42, i32* %11
br label %43
43: ; preds = %40, %34
%44 = load i32, i32* %11
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %5
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %43
store i64 0, i64* %12
br label %55
55: ; preds = %70, %54
%56 = load i64, i64* %12
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, 2
%59 = sext i32 %58 to i64
%60 = icmp ult i64 %56, %59
br i1 %60, label %61, label %73
61: ; preds = %55
%62 = load i8**, i8*** %8
%63 = load i64, i64* %12
%64 = add i64 %63, 2
%65 = getelementptr inbounds i8*, i8** %62, i64 %64
%66 = load i8*, i8** %65
%67 = load i8**, i8*** %8
%68 = load i64, i64* %12
%69 = getelementptr inbounds i8*, i8** %67, i64 %68
store i8* %66, i8** %69
br label %70
70: ; preds = %61
%71 = load i64, i64* %12
%72 = add i64 %71, 1
store i64 %72, i64* %12
br label %55
73: ; preds = %55
%74 = load i8**, i8*** %8
%75 = load i64, i64* %12
%76 = getelementptr inbounds i8*, i8** %74, i64 %75
store i8* null, i8** %76
%77 = load i8**, i8*** %8
%78 = getelementptr inbounds i8*, i8** %77, i64 0
%79 = load i8*, i8** %78
%80 = load i8**, i8*** %8
%81 = call i32 @execv(i8* %79, i8** %80)
%82 = load i32, i32* @stderr
%83 = load i8*, i8** %5
%84 = load i32, i32* @errno
%85 = call i8* @strerror(i32 %84)
%86 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %83, i8* %85)
%87 = load i32, i32* @EXIT_FAILURE
%88 = call i32 @exit(i32 %87)
unreachable
89: ; preds = %31
%90 = load i32, i32* %9
ret i32 %90
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"%s: launching child %s\0A\00"
@.str.1 = [16 x i8] c"%s: fork(): %s\0A\00"
@errno = global i32 0
@SIG_SETMASK = global i32 0
@.str.2 = [40 x i8] c"%s: setting the signal mask failed: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [17 x i8] c"%s: execv(): %s\0A\00"
define i32 @launch(i8* %0, i32 %1, i32 %2, i8** %3) {
%5 = bitcast i8** %3 to i8*
%6 = alloca i32
%7 = load i32, i32* @stderr
%8 = getelementptr i8*, i8** %3, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %11
11: ; preds = %13, %4
%12 = tail call i32 (...) @fork()
switch i32 %12, label %53 [
i32 -1, label %13
i32 0, label %19
]
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %16)
%18 = tail call i32 @sleep(i32 1)
br label %11
19: ; preds = %11
%20 = tail call i32 @close(i32 %1)
%21 = bitcast i32* %6 to i8*
%22 = call i32 @sigemptyset(i32* %6)
%23 = icmp eq i32 %22, -1
br i1 %23, label %35, label %24
24: ; preds = %19
%25 = load i32, i32* @SIG_SETMASK
%26 = call i32 @sigprocmask(i32 %25, i32* %6, i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %35, label %28
28: ; preds = %24
%29 = add nsw i32 %2, -2
%30 = icmp eq i32 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = bitcast i8** %8 to i8*
%33 = sext i32 %29 to i64
%34 = shl nuw nsw i64 %33, 3
br label %42
35: ; preds = %19, %24
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %31, %28
%43 = phi i64 [ 0, %28 ], [ %33, %31 ]
%44 = getelementptr inbounds i8*, i8** %3, i64 %43
store i8* null, i8** %44
%45 = load i8*, i8** %3
%46 = call i32 @execv(i8* %45, i8** %3)
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %11
ret i32 %12
}