#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_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");
    } 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    0x2e37(%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    0x2dd6(%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    12a2 <main+0x122>
cmpl   $0x0,-0x8(%rbp)
jne    1288 <main+0x108>
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    1283 <main+0x103>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
jmp    129d <main+0x11d>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12a2 <main+0x122>
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    0x2e38(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe5d(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2e39(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11ea <main+0x6a>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe0f(%rip),%r14
mov    0x2de0(%rip),%r15
lea    0xe35(%rip),%r12
jmp    1238 <main+0xb8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1090 <wait@plt>
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   1040 <fputs@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11c7 <main+0x47>
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     1261 <main+0xe1>
mov    (%r15),%edi
mov    %r12,%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    (%r15),%edi
lea    0xda2(%rip),%rsi
jmp    125a <main+0xda>

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"
@.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 = %49, %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 %49
32: ; preds = %20
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %45
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 %44
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))
br label %44
44: ; preds = %41, %35
br label %48
45: ; preds = %32
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %45, %44
br label %49
49: ; preds = %48, %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"
@.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 = %34, %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, %34
%14 = phi i8* [ %38, %34 ], [ %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 %34
23: ; preds = %13
%24 = icmp eq i32 %19, 0
br i1 %24, label %25, label %31
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 %34
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))
br label %34
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %25, %28, %31, %21
%35 = load i32, i32* @stdout
%36 = call i32 @fputs(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @stdin
%38 = call i8* @fgets(i8* %2, i32 512, i32 %37)
%39 = icmp eq i8* %38, null
br i1 %39, label %8, label %13
}