#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 EXIT_SUCCESS;
int execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
char *fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int pid, longueur, r;
  char tab[512], *s;
  for (;;) {
    fputs("petit_shell...> ", stdout);
    s = fgets(tab, 512, stdin);
    if (s == NULL) {
      fprintf(stderr, "Fin du Shell\n");
      exit(EXIT_SUCCESS);
    }
    longueur = strlen(s);
    tab[longueur - 1] = '\0';
    pid = fork();
    switch (pid) {
    case -1:
      fprintf(stderr, "Error when creating child thread!\n");
      break;
    case 0:
      r = execlp(s, s, NULL);
      if (r) {
        printf("Error when executing: %s\n", s);
      }
      exit(r);
    default:
      wait(NULL);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1050 <fputs@plt>
lea    -0x210(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1060 <fgets@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    1209 <main+0x79>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x218(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x21c(%rbp)
sub    $0xffffffff,%eax
je     125c <main+0xcc>
jmp    1249 <main+0xb9>
mov    -0x21c(%rbp),%eax
test   %eax,%eax
je     1276 <main+0xe6>
jmp    12b5 <main+0x125>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12be <main+0x12e>
mov    -0x218(%rbp),%rdi
mov    -0x218(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execlp@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12ad <main+0x11d>
mov    -0x218(%rbp),%rsi
lea    0xd9a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1080 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    11a2 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    0x2e28(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe4d(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2e29(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x200,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11fa <main+0x6a>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %rax,%rbx
lea    0xe1b(%rip),%r14
lea    0xdf5(%rip),%r15
jmp    1245 <main+0xb5>
nopl   (%rax)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1070 <fprintf@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1050 <fputs@plt>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
mov    $0x200,%esi
call   1060 <fgets@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d7 <main+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x80>
test   %eax,%eax
je     126e <main+0xde>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    1221 <main+0x91>
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <execlp@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1290 <main+0x100>
lea    0xdba(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"petit_shell...> \00"
@stdout = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"Fin du Shell\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [35 x i8] c"Error when creating child thread!\0A\00"
@.str.3 = [26 x i8] c"Error when executing: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca i8*
store i32 0, i32* %1
br label %7
7: ; preds = %46, %0
%8 = load i32, i32* @stdout
%9 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i8* @fgets(i8* %10, i32 512, i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %7
%21 = load i8*, i8** %6
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 (...) @fork()
store i32 %27, i32* %2
%28 = load i32, i32* %2
switch i32 %28, label %44 [
i32 -1, label %29
i32 0, label %32
]
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %46
32: ; preds = %20
%33 = load i8*, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @execlp(i8* %33, i8* %34, i32* null)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i8*, i8** %6
%40 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %32
%42 = load i32, i32* %4
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %20
%45 = call i32 @wait(i32* null)
br label %46
46: ; preds = %44, %29
br label %7
}

LLVM IR (-O2):
@.str = [17 x i8] c"petit_shell...> \00"
@stdout = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [14 x i8] c"Fin du Shell\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [35 x i8] c"Error when creating child thread!\0A\00"
@.str.3 = [26 x i8] c"Error when executing: %s\0A\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdout
%4 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @stdin
%6 = call i8* @fgets(i8* %2, i32 512, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %32, %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_SUCCESS
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0, %32
%14 = phi i8* [ %36, %32 ], [ %6, %0 ]
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 (...) @fork()
switch i32 %19, label %30 [
i32 -1, label %20
i32 0, label %23
]
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %32
23: ; preds = %13
%24 = call i32 @execlp(i8* %14, i8* %14, i32* null)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %28
28: ; preds = %26, %23
%29 = call i32 @exit(i32 %24)
unreachable
30: ; preds = %13
%31 = call i32 @wait(i32* null)
br label %32
32: ; preds = %30, %20
%33 = load i32, i32* @stdout
%34 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @stdin
%36 = call i8* @fgets(i8* %2, i32 512, i32 %35)
%37 = icmp eq i8* %36, null
br i1 %37, label %8, label %13
}