#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*/ EXIT_SUCCESS;
int execlp(char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  int pid, longueur;
  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();
    if (pid > 0) {
      wait(NULL);
    } else if (pid == 0) {
      int r = execlp(tab, tab, (char *)0);
      if (r == -1) {
        fprintf(stderr, "Erreur dans le exec\n");
        exit(EXIT_FAILURE);
      }
    } else {
      fprintf(stderr, "Erreur dans le fork\n");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1040 <fputs@plt>
lea    -0x210(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11f9 <main+0x79>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x218(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    123a <main+0xba>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    12b0 <main+0x130>
cmpl   $0x0,-0x8(%rbp)
jne    1296 <main+0x116>
lea    -0x210(%rbp),%rdi
lea    -0x210(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <execlp@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0xffffffff,-0x21c(%rbp)
jne    1291 <main+0x111>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    12ab <main+0x12b>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12b0 <main+0x130>
jmp    1192 <main+0x12>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    0x2e30(%rip),%r12
mov    (%r12),%esi
lea    0xe5d(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2e39(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11eb <main+0x6b>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe0e(%rip),%r14
mov    0x2ddf(%rip),%rbp
lea    0xe34(%rip),%r15
jmp    1239 <main+0xb9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
call   1090 <wait@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1040 <fputs@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11c8 <main+0x48>
mov    %rax,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jg     1210 <main+0x90>
je     1262 <main+0xe2>
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1060 <fprintf@plt>
jmp    1217 <main+0x97>
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0x97>
mov    0x0(%rbp),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1070 <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 = [21 x i8] c"Erreur dans le exec\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"Erreur dans le fork\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* %1
br label %7
7: ; preds = %51, %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]* %4, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i8* @fgets(i8* %10, i32 512, i32 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%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** %5
%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]* %4, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 (...) @fork()
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @wait(i32* null)
br label %51
32: ; preds = %20
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%38 = call i32 @execlp(i8* %36, i8* %37, i8* null)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %35
br label %50
47: ; preds = %32
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %47, %46
br label %51
51: ; preds = %50, %30
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 = [21 x i8] c"Erreur dans le exec\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [21 x i8] c"Erreur dans le fork\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 = %36, %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, %36
%14 = phi i8* [ %40, %36 ], [ %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()
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @wait(i32* null)
br label %36
23: ; preds = %13
%24 = icmp eq i32 %19, 0
br i1 %24, label %25, label %33
25: ; preds = %23
%26 = call i32 @execlp(i8* %2, i8* %2, i8* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %25, %33, %21
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @stdin
%40 = call i8* @fgets(i8* %2, i32 512, i32 %39)
%41 = icmp eq i8* %40, null
br i1 %41, label %8, label %13
}