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

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int WEXITSTATUS(int);
int WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int execve(char *, char **, int /*<<< orphan*/ *);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int execute_program(char *argv[]) {
  pid_t my_pid;
  int status = 0;
  int timeout = 5;
  printf("%s %s %s\n", argv[0], argv[1], argv[2]);
  my_pid = fork();
  if (my_pid == 0) {
    printf("child process(%d) execute %s\n", getpid(), argv[0]);
    if (execve(argv[0], (char **)argv, NULL) == -1) {
      perror("child process execve failed");
      return -1;
    }
  }
  printf("parent process(%d) wait for child process(%d)\n", getpid(), my_pid);
  while (waitpid(my_pid, &status, WNOHANG) == 0) {
    timeout = timeout - 1;
    if (timeout < 0) {
      perror("timeout");
      kill(my_pid, SIGTERM);
      return -1;
    } else {
      sleep(1);
    }
  }
  printf("%s WEXITSTATUS %d WIFEXITED %d [status %d]\n", argv[0],
         WEXITSTATUS(status), WIFEXITED(status), status);
  if (WEXITSTATUS(status) != 0 || WIFEXITED(status) != 1) {
    perror("%s failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<execute_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x5,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    124b <execute_program+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execve@plt>
cmp    $0xffffffff,%eax
jne    1246 <execute_program+0xa6>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <execute_program+0x1a9>
jmp    124b <execute_program+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jne    12cf <execute_program+0x12f>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12c0 <execute_program+0x120>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <execute_program+0x1a9>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1265 <execute_program+0xc5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   10a0 <WIFEXITED@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
mov    -0x18(%rbp),%r8d
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    132a <execute_program+0x18a>
mov    -0x18(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x1,%eax
je     1342 <execute_program+0x1a2>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <execute_program+0x1a9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_program>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
mov    (%rdi),%rsi
mov    0x8(%rdi),%rdx
mov    0x10(%rdi),%rcx
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1209 <execute_program+0x69>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    (%rbx),%rdx
lea    0xe20(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1050 <execve@plt>
cmp    $0xffffffff,%eax
je     1346 <execute_program+0x1a6>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe2d(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2db7(%rip),%r14
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    12d7 <execute_program+0x137>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    12d7 <execute_program+0x137>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    12d7 <execute_program+0x137>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    12d7 <execute_program+0x137>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
jne    12d7 <execute_program+0x137>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r14),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1080 <waitpid@plt>
test   %rax,%rax
je     134f <execute_program+0x1af>
mov    (%rbx),%rbp
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0xc(%rsp),%r8d
lea    0xd81(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
jne    1332 <execute_program+0x192>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x1,%eax
jne    1332 <execute_program+0x192>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd6e(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1326 <execute_program+0x186>
lea    0xcdb(%rip),%rdi
jmp    1339 <execute_program+0x199>
lea    0xd1d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c86(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1060 <kill@plt>
jmp    133e <execute_program+0x19e>

LLVM IR (-O0):
@.str = [10 x i8] c"%s %s %s\0A\00"
@.str.1 = [30 x i8] c"child process(%d) execute %s\0A\00"
@.str.2 = [28 x i8] c"child process execve failed\00"
@.str.3 = [47 x i8] c"parent process(%d) wait for child process(%d)\0A\00"
@WNOHANG = global i32 0
@.str.4 = [8 x i8] c"timeout\00"
@SIGTERM = global i32 0
@.str.5 = [44 x i8] c"%s WEXITSTATUS %d WIFEXITED %d [status %d]\0A\00"
@.str.6 = [10 x i8] c"%s failed\00"
define i32 @execute_program(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 0, i32* %5
store i32 5, i32* %6
%7 = load i8**, i8*** %3
%8 = getelementptr inbounds i8*, i8** %7, i64 0
%9 = load i8*, i8** %8
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %3
%14 = getelementptr inbounds i8*, i8** %13, i64 2
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %12, i8* %15)
%17 = call i32 (...) @fork()
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %1
%21 = call i32 (...) @getpid()
%22 = load i8**, i8*** %3
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %24)
%26 = load i8**, i8*** %3
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %3
%30 = call i32 @execve(i8* %28, i8** %29, i32* null)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
34: ; preds = %20
br label %35
35: ; preds = %34, %1
%36 = call i32 (...) @getpid()
%37 = load i32, i32* %4
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %56, %35
%40 = load i32, i32* %4
%41 = load i32, i32* @WNOHANG
%42 = call i64 @waitpid(i32 %40, i32* %5, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %39
%45 = load i32, i32* %6
%46 = sub nsw i32 %45, 1
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* %4
%52 = load i32, i32* @SIGTERM
%53 = call i32 @kill(i32 %51, i32 %52)
store i32 -1, i32* %2
br label %77
54: ; preds = %44
%55 = call i32 @sleep(i32 1)
br label %56
56: ; preds = %54
br label %39
57: ; preds = %39
%58 = load i8**, i8*** %3
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = load i32, i32* %5
%62 = call i32 @WEXITSTATUS(i32 %61)
%63 = load i32, i32* %5
%64 = call i32 @WIFEXITED(i32 %63)
%65 = load i32, i32* %5
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %60, i32 %62, i32 %64, i32 %65)
%67 = load i32, i32* %5
%68 = call i32 @WEXITSTATUS(i32 %67)
%69 = icmp ne i32 %68, 0
br i1 %69, label %74, label %70
70: ; preds = %57
%71 = load i32, i32* %5
%72 = call i32 @WIFEXITED(i32 %71)
%73 = icmp ne i32 %72, 1
br i1 %73, label %74, label %76
74: ; preds = %70, %57
%75 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %2
br label %77
76: ; preds = %70
store i32 0, i32* %2
br label %77
77: ; preds = %76, %74, %49, %32
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s %s %s\0A\00"
@.str.1 = [30 x i8] c"child process(%d) execute %s\0A\00"
@.str.2 = [28 x i8] c"child process execve failed\00"
@.str.3 = [47 x i8] c"parent process(%d) wait for child process(%d)\0A\00"
@WNOHANG = global i32 0
@.str.4 = [8 x i8] c"timeout\00"
@SIGTERM = global i32 0
@.str.5 = [44 x i8] c"%s WEXITSTATUS %d WIFEXITED %d [status %d]\0A\00"
@.str.6 = [10 x i8] c"%s failed\00"
define i32 @execute_program(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i8*, i8** %0
%5 = getelementptr inbounds i8*, i8** %0, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %0, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %6, i8* %8)
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %1
%13 = tail call i32 (...) @getpid()
%14 = load i8*, i8** %0
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %14)
%16 = load i8*, i8** %0
%17 = tail call i32 @execve(i8* %16, i8** %0, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %73
21: ; preds = %12, %1
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %10)
%24 = load i32, i32* @WNOHANG
%25 = call i64 @waitpid(i32 %10, i32* %2, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %56
27: ; preds = %21
%28 = call i32 @sleep(i32 1)
%29 = load i32, i32* @WNOHANG
%30 = call i64 @waitpid(i32 %10, i32* %2, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %56
32: ; preds = %51
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i32 %10, i32 %34)
br label %73
36: ; preds = %27
%37 = call i32 @sleep(i32 1)
%38 = load i32, i32* @WNOHANG
%39 = call i64 @waitpid(i32 %10, i32* %2, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %56
41: ; preds = %36
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* @WNOHANG
%44 = call i64 @waitpid(i32 %10, i32* %2, i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %41
%47 = call i32 @sleep(i32 1)
%48 = load i32, i32* @WNOHANG
%49 = call i64 @waitpid(i32 %10, i32* %2, i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = call i32 @sleep(i32 1)
%53 = load i32, i32* @WNOHANG
%54 = call i64 @waitpid(i32 %10, i32* %2, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %32, label %56
56: ; preds = %27, %36, %41, %46, %51, %21
%57 = load i8*, i8** %0
%58 = load i32, i32* %2
%59 = call i32 @WEXITSTATUS(i32 %58)
%60 = load i32, i32* %2
%61 = call i32 @WIFEXITED(i32 %60)
%62 = load i32, i32* %2
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %57, i32 %59, i32 %61, i32 %62)
%64 = load i32, i32* %2
%65 = call i32 @WEXITSTATUS(i32 %64)
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %56
%68 = load i32, i32* %2
%69 = call i32 @WIFEXITED(i32 %68)
%70 = icmp eq i32 %69, 1
br i1 %70, label %73, label %71
71: ; preds = %67, %56
%72 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %73
73: ; preds = %67, %71, %32, %19
%74 = phi i32 [ -1, %19 ], [ -1, %32 ], [ -1, %71 ], [ 0, %67 ]
ret i32 %74
}