#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *const);
scalar_t__ start_process(char *const *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int run_app(char *const argv[]) {
  pid_t pid = start_process(argv);
  if (pid > 0) {
    int status;
    if (waitpid(pid, &status, 0) != pid || !WIFEXITED(status)) {
      fprintf(stderr, "WARNING: Application %s did not return exit code\n",
              argv[0]);
      return 0xff;
    }
    return WEXITSTATUS(status);
  } else {
    exit(2);
  }
}


x86 assembly (clang, -O0):
<run_app>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <start_process@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11e6 <run_app+0x86>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x18(%rbp),%rax
jne    11ae <run_app+0x4e>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    11d6 <run_app+0x76>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xff,-0x4(%rbp)
jmp    11f0 <run_app+0x90>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11f0 <run_app+0x90>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_app>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <start_process@plt>
test   %rax,%rax
jle    11c5 <run_app+0x65>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
jne    11a0 <run_app+0x40>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
je     11a0 <run_app+0x40>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    11bd <run_app+0x5d>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe4d(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"WARNING: Application %s did not return exit code\0A\00"
define i32 @run_app(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
store i8** %0, i8*** %3
%6 = load i8**, i8*** %3
%7 = call i64 @start_process(i8** %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %1
%11 = load i64, i64* %4
%12 = call i64 @waitpid(i64 %11, i32* %5, i32 0)
%13 = load i64, i64* %4
%14 = icmp ne i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = load i32, i32* %5
%17 = call i32 @WIFEXITED(i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %15, %10
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 255, i32* %2
br label %30
25: ; preds = %15
%26 = load i32, i32* %5
%27 = call i32 @WEXITSTATUS(i32 %26)
store i32 %27, i32* %2
br label %30
28: ; preds = %1
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %25, %19
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"WARNING: Application %s did not return exit code\0A\00"
define i32 @run_app(i8** %0) {
%2 = alloca i32
%3 = tail call i64 @start_process(i8** %0)
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = call i64 @waitpid(i64 %3, i32* %2, i32 0)
%8 = icmp eq i64 %7, %3
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* %2
%11 = call i32 @WIFEXITED(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9, %5
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %0
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %20
17: ; preds = %9
%18 = load i32, i32* %2
%19 = call i32 @WEXITSTATUS(i32 %18)
br label %20
20: ; preds = %17, %13
%21 = phi i32 [ 255, %13 ], [ %19, %17 ]
ret i32 %21
22: ; preds = %1
%23 = tail call i32 @exit(i32 2)
unreachable
}