#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SIGTERM;
int execve(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
void restart_process(char **args) {
  int childpid;
  childpid = fork();
  if (childpid < 0) {
    perror("fork failed");
  } else if (childpid == 0) {
    printf("new process %d", getpid());
    int rv = execve(args[0], args, NULL);
    if (rv == -1) {
      perror("execve");
      exit(EXIT_FAILURE);
    }
  } else {
    sleep(5);
    printf("killing %d\n", getpid());
    kill(getpid(), SIGTERM);
  }
}


x86 assembly (clang, -O0):
<restart_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b1 <restart_process+0x31>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
jmp    124b <restart_process+0xcb>
cmpl   $0x0,-0xc(%rbp)
jne    1210 <restart_process+0x90>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <execve@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    120b <restart_process+0x8b>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1246 <restart_process+0xc6>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    124b <restart_process+0xcb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restart_process>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     11f1 <restart_process+0x71>
je     11c8 <restart_process+0x48>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe79(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
pop    %rbx
jmp    1060 <kill@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe36(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1050 <execve@plt>
cmp    $0xffffffff,%eax
je     11fe <restart_process+0x7e>
pop    %rbx
ret
lea    0xe08(%rip),%rdi
pop    %rbx
jmp    1070 <perror@plt>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [15 x i8] c"new process %d\00"
@.str.2 = [7 x i8] c"execve\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"killing %d\0A\00"
@SIGTERM = global i32 0
define void @restart_process(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
store i8** %0, i8*** %2
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %1
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = call i32 (...) @getpid()
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i8**, i8*** %2
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %2
%20 = call i32 @execve(i8* %18, i8** %19, i32* null)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
br label %35
28: ; preds = %10
%29 = call i32 @sleep(i32 5)
%30 = call i32 (...) @getpid()
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = call i32 (...) @getpid()
%33 = load i32, i32* @SIGTERM
%34 = call i32 @kill(i32 %32, i32 %33)
br label %35
35: ; preds = %28, %27
br label %36
36: ; preds = %35, %8
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [15 x i8] c"new process %d\00"
@.str.2 = [7 x i8] c"execve\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"killing %d\0A\00"
@SIGTERM = global i32 0
define void @restart_process(i8** %0) {
%2 = tail call i32 (...) @fork()
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %25
6: ; preds = %1
%7 = icmp eq i32 %2, 0
br i1 %7, label %8, label %18
8: ; preds = %6
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %0
%12 = tail call i32 @execve(i8* %11, i8** %0, i32* null)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %6
%19 = tail call i32 @sleep(i32 5)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = tail call i32 (...) @getpid()
%23 = load i32, i32* @SIGTERM
%24 = tail call i32 @kill(i32 %22, i32 %23)
br label %25
25: ; preds = %8, %18, %4
ret void
}