#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*/ clear_screen();
int /*<<< orphan*/ execve(char *, char **, char **);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  clear_screen();
  char *shell_argv[] = {"/bin/bash", NULL};
  char *shell_envp[] = {"TERM="
                        "kosterm",
                        NULL};
  bool first_shell = true;
  while (1) {
    pid_t shell_id = fork();
    if (shell_id) {
      pid_t child;
      while ((child = wait(NULL)) != shell_id)
        ;
      first_shell = false;
    } else {
      if (!first_shell) {
        printf("\nShell died, respawning!\n");
      }
      execve("/bin/bash", shell_argv, shell_envp);
      perror("init");
      while (1)
        ;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <clear_screen@plt>
mov    0x2c7c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c79(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c76(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c73(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11f3 <main+0x93>
jmp    11cb <main+0x6b>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    %rax,-0x48(%rbp)
cmp    -0x40(%rbp),%rax
je     11e7 <main+0x87>
jmp    11cb <main+0x6b>
movl   $0x0,-0x34(%rbp)
jmp    122e <main+0xce>
cmpl   $0x0,-0x34(%rbp)
jne    1209 <main+0xa9>
lea    0xe13(%rip),%rdi
call   1030 <printf@plt>
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    0xde8(%rip),%rdi
call   1040 <execve@plt>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
jmp    1229 <main+0xc9>
jmp    11b0 <main+0x50>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   1050 <clear_screen@plt>
movaps 0x2c8d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2c91(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11c7 <main+0x67>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
call   1070 <wait@plt>
cmp    %rbx,%rax
jne    11a0 <main+0x40>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <main+0x40>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe32(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <execve@plt>
lea    0xe35(%rip),%rdi
call   1060 <perror@plt>
nopw   0x0(%rax,%rax,1)
jmp    11f0 <main+0x90>

LLVM IR (-O0):
@.str = [10 x i8] c"/bin/bash\00"
@__const.main.shell_argv = [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [13 x i8] c"TERM=kosterm\00"
@__const.main.shell_envp = [2 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.2 = [26 x i8] c"\0AShell died, respawning!\0A\00"
@.str.3 = [5 x i8] c"init\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i8*]
%7 = alloca [2 x i8*]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @clear_screen()
%12 = bitcast [2 x i8*]* %6 to i8*
%13 = bitcast [2 x i8*]* %7 to i8*
store i32 1, i32* %8
br label %14
14: ; preds = %2, %36
%15 = call i64 (...) @fork()
store i64 %15, i64* %9
%16 = load i64, i64* %9
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
br label %19
19: ; preds = %23, %18
%20 = call i64 @wait(i32* null)
store i64 %20, i64* %10
%21 = load i64, i64* %9
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %19
24: ; preds = %19
store i32 0, i32* %8
br label %36
25: ; preds = %14
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%32 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 0
%33 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %31, i8** %32)
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %30, %35
br label %35
36: ; preds = %24
br label %14
}

LLVM IR (-O2):
@.str = [10 x i8] c"/bin/bash\00"
@__const.main.shell_argv = [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [13 x i8] c"TERM=kosterm\00"
@__const.main.shell_envp = [2 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0), i8* null]
@.str.3 = [5 x i8] c"init\00"
@str = [25 x i8] c"\0AShell died, respawning!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i8*]
%4 = alloca [2 x i8*]
%5 = tail call i32 (...) @clear_screen()
%6 = bitcast [2 x i8*]* %3 to i8*
%7 = bitcast [2 x i8*]* %4 to i8*
%8 = tail call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %13
10: ; preds = %15
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %2, %10
%14 = phi i64 [ %11, %10 ], [ %8, %2 ]
br label %15
15: ; preds = %13, %15
%16 = tail call i64 @wait(i32* null)
%17 = icmp eq i64 %16, %14
br i1 %17, label %10, label %15
18: ; preds = %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %2, %18
%21 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%22 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%23 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %21, i8** %22)
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %25, %20
br label %25
}