#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGALRM;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int atoi(char *);
int execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int ret, timeout;
  if (argc == 2 && (strcmp(argv[1], "0") == 0)) {
    kill(getpid(), SIGALRM);
    sleep(1);
    return 2;
  }
  if (argc < 3) {
    printf("\nUsage: \n");
    printf("  $ %s n exe arglist\n", argv[0]);
    printf("  $ %s 0\n", argv[0]);
    printf("\nWhere:\n");
    printf("  n       is the timeout duration in seconds,\n");
    printf("  exe     is the executable filename to run,\n");
    printf("  arglist is the arguments to be passed to executable.\n\n");
    printf("  The second use case will emulate an immediate timeout.\n\n");
    return 2;
  }
  timeout = atoi(argv[1]);
  if (timeout < 1) {
    fprintf(
        stderr,
        "Invalid timeout value \"%s\". Timeout must be a positive integer.\n",
        argv[1]);
    return 2;
  }
  alarm(timeout);
  ret = execvp(argv[2], &argv[2]);
  if (ret == -1) {
    perror("Unable to run child application");
    return 2;
  }
  assert(0);
  perror("Should not see me");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    121b <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe21(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    121b <main+0x6b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
movl   $0x2,-0x4(%rbp)
jmp    134e <main+0x19e>
cmpl   $0x3,-0x8(%rbp)
jge    12af <main+0xff>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    134e <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    12f2 <main+0x142>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe25(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    134e <main+0x19e>
mov    -0x18(%rbp),%edi
call   1050 <alarm@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   10b0 <execvp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1334 <main+0x184>
lea    0xe24(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    134e <main+0x19e>
xor    %edi,%edi
call   1070 <assert@plt>
lea    0xe25(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1207 <main+0x47>
mov    0x8(%rbx),%rdi
lea    0xe29(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1209 <main+0x49>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2df9(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10a0 <kill@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
jmp    129d <main+0xdd>
jg     1275 <main+0xb5>
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%rsi
lea    0xde3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%rsi
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
lea    0xecb(%rip),%rdi
call   1030 <puts@plt>
jmp    129d <main+0xdd>
mov    0x8(%rbx),%r14
mov    %r14,%rdi
call   10d0 <atoi@plt>
test   %eax,%eax
jg     12aa <main+0xea>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    %r14,%rdx
call   1090 <fprintf@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%edi
call   1060 <alarm@plt>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   10c0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    12cf <main+0x10f>
lea    0xd96(%rip),%rdi
jmp    12dd <main+0x11d>
xor    %edi,%edi
call   1080 <assert@plt>
lea    0xda6(%rip),%rdi
call   10b0 <perror@plt>
jmp    129d <main+0xdd>

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@SIGALRM = global i32 0
@.str.1 = [10 x i8] c"\0AUsage: \0A\00"
@.str.2 = [22 x i8] c" $ %s n exe arglist\0A\00"
@.str.3 = [10 x i8] c" $ %s 0\0A\00"
@.str.4 = [9 x i8] c"\0AWhere:\0A\00"
@.str.5 = [47 x i8] c" n is the timeout duration in seconds,\0A\00"
@.str.6 = [46 x i8] c" exe is the executable filename to run,\0A\00"
@.str.7 = [57 x i8] c" arglist is the arguments to be passed to executable.\0A\0A\00"
@.str.8 = [59 x i8] c" The second use case will emulate an immediate timeout.\0A\0A\00"
@stderr = global i32 0
@.str.9 = [65 x i8] c"Invalid timeout value \22%s\22. Timeout must be a positive integer.\0A\00"
@.str.10 = [32 x i8] c"Unable to run child application\00"
@.str.11 = [18 x i8] c"Should not see me\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = call i32 (...) @getpid()
%18 = load i32, i32* @SIGALRM
%19 = call i32 @kill(i32 %17, i32 %18)
%20 = call i32 @sleep(i32 1)
store i32 2, i32* %3
br label %68
21: ; preds = %10, %2
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 3
br i1 %23, label %24, label %39
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.7, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.8, i64 0, i64 0))
store i32 2, i32* %3
br label %68
39: ; preds = %21
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %52
46: ; preds = %39
%47 = load i32, i32* @stderr
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.9, i64 0, i64 0), i8* %50)
store i32 2, i32* %3
br label %68
52: ; preds = %39
%53 = load i32, i32* %7
%54 = call i32 @alarm(i32 %53)
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 2
%60 = call i32 @execvp(i8* %57, i8** %59)
store i32 %60, i32* %6
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %52
%64 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0))
store i32 2, i32* %3
br label %68
65: ; preds = %52
%66 = call i32 @assert(i32 0)
%67 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
store i32 2, i32* %3
br label %68
68: ; preds = %65, %63, %46, %24, %16
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@SIGALRM = global i32 0
@.str.2 = [22 x i8] c" $ %s n exe arglist\0A\00"
@.str.3 = [10 x i8] c" $ %s 0\0A\00"
@stderr = global i32 0
@.str.9 = [65 x i8] c"Invalid timeout value \22%s\22. Timeout must be a positive integer.\0A\00"
@.str.10 = [32 x i8] c"Unable to run child application\00"
@.str.11 = [18 x i8] c"Should not see me\00"
@str = [9 x i8] c"\0AUsage: \00"
@str.12 = [8 x i8] c"\0AWhere:\00"
@str.13 = [46 x i8] c" n is the timeout duration in seconds,\00"
@str.14 = [45 x i8] c" exe is the executable filename to run,\00"
@str.15 = [56 x i8] c" arglist is the arguments to be passed to executable.\0A\00"
@str.16 = [58 x i8] c" The second use case will emulate an immediate timeout.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %14
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %4
%10 = tail call i32 (...) @getpid()
%11 = load i32, i32* @SIGALRM
%12 = tail call i32 @kill(i32 %10, i32 %11)
%13 = tail call i32 @sleep(i32 1)
br label %46
14: ; preds = %2
%15 = icmp slt i32 %0, 3
br i1 %15, label %16, label %27
16: ; preds = %4, %14
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%18 = load i8*, i8** %1
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %1
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.12, i64 0, i64 0))
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.13, i64 0, i64 0))
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.14, i64 0, i64 0))
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.15, i64 0, i64 0))
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str.16, i64 0, i64 0))
br label %46
27: ; preds = %14
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.9, i64 0, i64 0), i8* %29)
br label %46
35: ; preds = %27
%36 = tail call i32 @alarm(i32 %30)
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32 @execvp(i8* %38, i8** %37)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.10, i64 0, i64 0))
br label %46
43: ; preds = %35
%44 = tail call i32 @assert(i32 0)
%45 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %46
46: ; preds = %43, %41, %32, %16, %9
ret i32 2
}