#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;
scalar_t__ WTERMSIG(int);
int /*<<< orphan*/ execvpe(char const *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ puts(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int run_script(const char *fname, char *args[]) {
  int status;
  pid_t pid = fork();
  if (pid == 0) {
    execvpe(fname, args, NULL);
    puts("Cannot exec");
    exit(EXIT_FAILURE);
  } else if (pid == (pid_t)-1) {
    puts("Cannot fork");
    return 1;
  }
  if (waitpid(pid, &status, 0) != pid) {
    puts("Wrong child");
    return 1;
  }
  if (WTERMSIG(status) != 0) {
    puts("Child terminated incorrectly");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b1 <run_script+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execvpe@plt>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11d4 <run_script+0x74>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <run_script+0xdb>
jmp    11d9 <run_script+0x79>
mov    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x28(%rbp),%rax
je     120a <run_script+0xaa>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <run_script+0xdb>
mov    -0x1c(%rbp),%edi
call   1050 <WTERMSIG@plt>
cmp    $0x0,%rax
je     1234 <run_script+0xd4>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <run_script+0xdb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_script>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     11b0 <run_script+0x50>
mov    %rax,%rbx
test   %rax,%rax
je     11d5 <run_script+0x75>
lea    0xc(%rsp),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
jne    11b9 <run_script+0x59>
mov    0xc(%rsp),%edi
call   1050 <WTERMSIG@plt>
test   %rax,%rax
je     11ca <run_script+0x6a>
lea    0xe76(%rip),%rdi
jmp    11c0 <run_script+0x60>
lea    0xe55(%rip),%rdi
jmp    11c0 <run_script+0x60>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <execvpe@plt>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Cannot exec\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"Cannot fork\00"
@.str.2 = [12 x i8] c"Wrong child\00"
@.str.3 = [29 x i8] c"Child terminated incorrectly\00"
define i32 @run_script(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i8**, i8*** %5
%14 = call i32 @execvpe(i8* %12, i8** %13, i32* null)
%15 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %38
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = load i64, i64* %7
%26 = call i64 @waitpid(i64 %25, i32* %6, i32 0)
%27 = load i64, i64* %7
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %38
31: ; preds = %24
%32 = load i32, i32* %6
%33 = call i64 @WTERMSIG(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %38
37: ; preds = %31
store i32 0, i32* %3
br label %38
38: ; preds = %37, %35, %29, %21
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [12 x i8] c"Cannot exec\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [12 x i8] c"Cannot fork\00"
@.str.2 = [12 x i8] c"Wrong child\00"
@.str.3 = [29 x i8] c"Child terminated incorrectly\00"
define i32 @run_script(i8* %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
switch i64 %5, label %13 [
i64 0, label %6
i64 -1, label %11
]
6: ; preds = %2
%7 = tail call i32 @execvpe(i8* %0, i8** %1, i32* null)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %24
13: ; preds = %2
%14 = call i64 @waitpid(i64 %5, i32* %3, i32 0)
%15 = icmp eq i64 %14, %5
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %24
18: ; preds = %13
%19 = load i32, i32* %3
%20 = call i64 @WTERMSIG(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %18, %22, %16, %11
%25 = phi i32 [ 1, %11 ], [ 1, %16 ], [ 1, %22 ], [ 0, %18 ]
ret i32 %25
}