#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WUNTRACED;
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int mysh_launch(char **args) {
  pid_t pid, wpid;
  int status;
  pid = fork();
  if (pid == 0) {
    if (execvp(args[0], args) < 0) {
      perror("Failed to exec: ");
      return 1;
    }
  } else if (pid < 0) {
    perror("Failed to fork: ");
    exit(EXIT_FAILURE);
  } else {
    do {
      wpid = waitpid(pid, &status, WUNTRACED);
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  }
  return 1;
}


x86 assembly (clang, -O0):
<mysh_launch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <mysh_launch+0x59>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
cmp    $0x0,%rax
jge    11c4 <mysh_launch+0x54>
lea    0xe4d(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1254 <mysh_launch+0xe4>
jmp    124d <mysh_launch+0xdd>
cmpq   $0x0,-0x18(%rbp)
jge    11ee <mysh_launch+0x7e>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11f3 <mysh_launch+0x83>
mov    -0x18(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jne    1238 <mysh_launch+0xc8>
mov    -0x24(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11f3 <mysh_launch+0x83>
jmp    1248 <mysh_launch+0xd8>
jmp    124d <mysh_launch+0xdd>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysh_launch>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11ca <mysh_launch+0x5a>
js     11f7 <mysh_launch+0x87>
mov    %rax,%rbx
mov    0x2e4c(%rip),%r15
lea    0xc(%rsp),%r14
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
jne    11da <mysh_launch+0x6a>
mov    0xc(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %eax,%eax
je     11a0 <mysh_launch+0x30>
jmp    11da <mysh_launch+0x6a>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
test   %rax,%rax
js     11e9 <mysh_launch+0x79>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe10(%rip),%rdi
call   1050 <perror@plt>
jmp    11da <mysh_launch+0x6a>
lea    0xe13(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Failed to exec: \00"
@.str.1 = [17 x i8] c"Failed to fork: \00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
define i32 @mysh_launch(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8** %0, i8*** %3
%7 = call i64 (...) @fork()
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = load i8**, i8*** %3
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = load i8**, i8*** %3
%15 = call i64 @execvp(i8* %13, i8** %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %46
19: ; preds = %10
br label %45
20: ; preds = %1
%21 = load i64, i64* %4
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
br label %28
28: ; preds = %41, %27
%29 = load i64, i64* %4
%30 = load i32, i32* @WUNTRACED
%31 = call i64 @waitpid(i64 %29, i32* %6, i32 %30)
store i64 %31, i64* %5
br label %32
32: ; preds = %28
%33 = load i32, i32* %6
%34 = call i32 @WIFEXITED(i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* %6
%38 = call i32 @WIFSIGNALED(i32 %37)
%39 = icmp ne i32 %38, 0
%40 = xor i1 %39, true
br label %41
41: ; preds = %36, %32
%42 = phi i1 [ false, %32 ], [ %40, %36 ]
br i1 %42, label %28, label %43
43: ; preds = %41
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44, %19
store i32 1, i32* %2
br label %46
46: ; preds = %45, %17
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@.str = [17 x i8] c"Failed to exec: \00"
@.str.1 = [17 x i8] c"Failed to fork: \00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
define i32 @mysh_launch(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i64 @execvp(i8* %7, i8** %0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %28
12: ; preds = %1
%13 = icmp slt i64 %4, 0
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %12, %24
%19 = load i32, i32* @WUNTRACED
%20 = call i64 @waitpid(i64 %4, i32* %2, i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @WIFEXITED(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i32, i32* %2
%26 = call i32 @WIFSIGNALED(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %18, label %28
28: ; preds = %24, %18, %6, %10
ret i32 1
}