#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 int /*<<< orphan*/ consulta;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  char consulta[256];
  char *p;
  char *argv[100];
  int pid, i;
  while (1) {
    printf("\x1B[36m"
           "ronaldo's terminal %s$"
           "\033[0m",
           "\x1B[32m");
    fgets(consulta, sizeof(consulta), stdin);
    for (i = 0; consulta[i] != '\n'; i++)
      ;
    consulta[i] = '\0';
    if (strcmp(consulta, "exit") == 0)
      break;
    i = 0;
    p = strtok(consulta, " ");
    while (p != NULL) {
      argv[i++] = p;
      p = strtok(NULL, " ");
    }
    argv[i++] = NULL;
    if (strcmp(argv[0], "cd") == 0) {
      chdir(argv[1]);
      continue;
    }
    pid = fork();
    if (!pid) {
      execvp(argv[0], argv);
      break;
    }
    wait(NULL);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1050 <fgets@plt>
movl   $0x0,-0x448(%rbp)
movslq -0x448(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     11fa <main+0x7a>
jmp    11e6 <main+0x66>
mov    -0x448(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x448(%rbp)
jmp    11c9 <main+0x49>
movslq -0x448(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    122b <main+0xab>
jmp    1328 <main+0x1a8>
movl   $0x0,-0x448(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     129b <main+0x11b>
mov    -0x118(%rbp),%rcx
mov    -0x448(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x448(%rbp)
cltq
mov    %rcx,-0x440(%rbp,%rax,8)
ff
xor    %eax,%eax
mov    %eax,%edi
lea    0xda1(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x118(%rbp)
jmp    124f <main+0xcf>
mov    -0x448(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x448(%rbp)
cltq
movq   $0x0,-0x440(%rbp,%rax,8)
mov    -0x440(%rbp),%rdi
lea    0xd65(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12e8 <main+0x168>
mov    -0x438(%rbp),%rdi
call   1030 <chdir@plt>
jmp    1192 <main+0x12>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jne    131a <main+0x19a>
mov    -0x440(%rbp),%rdi
lea    -0x440(%rbp),%rsi
call   1080 <execvp@plt>
jmp    1328 <main+0x1a8>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    1192 <main+0x12>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsp,%r13
lea    0xe8b(%rip),%r12
lea    0xe89(%rip),%rbx
lea    0xe84(%rip),%r15
jmp    11bd <main+0x3d>
nopl   0x0(%rax,%rax,1)
mov    0x108(%rsp),%rdi
call   1030 <chdir@plt>
lea    0xe3c(%rip),%rdi
lea    0xe55(%rip),%rsi
call   1040 <printf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0x100,%esi
call   1050 <fgets@plt>
mov    $0xffffffffffffffff,%rax
nopl   (%rax)
cmpb   $0xa,0x1(%rsp,%rax,1)
lea    0x1(%rax),%rax
jne    11f0 <main+0x70>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     129c <main+0x11c>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strtok@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     124b <main+0xcb>
nopw   0x0(%rax,%rax,1)
mov    %rax,0x100(%rsp,%rbp,8)
add    $0x1,%rbp
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
jne    1230 <main+0xb0>
mov    %ebp,%eax
movq   $0x0,0x100(%rsp,%rax,8)
mov    0x100(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b0 <main+0x30>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     128c <main+0x10c>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    11bd <main+0x3d>
lea    0x100(%rsp),%rsi
mov    %r14,%rdi
call   1080 <execvp@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"\1B[36mronaldo's terminal %s$\1B[0m\00"
@.str.1 = [6 x i8] c"\1B[32m\00"
@stdin = global i32 0
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [3 x i8] c"cd\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca [100 x i8*]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
br label %7
7: ; preds = %0, %53, %66
%8 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %9, i32 256, i32 %10)
store i32 0, i32* %6
br label %12
12: ; preds = %20, %7
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 10
br i1 %18, label %19, label %23
19: ; preds = %12
br label %20
20: ; preds = %19
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %12
23: ; preds = %12
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %68
31: ; preds = %23
store i32 0, i32* %6
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %33, i8** %3
br label %34
34: ; preds = %37, %31
%35 = load i8*, i8** %3
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 %41
store i8* %38, i8** %42
%43 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %43, i8** %3
br label %34
44: ; preds = %34
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 %47
store i8* null, i8** %48
%49 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%50 = load i8*, i8** %49
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %44
%54 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @chdir(i8* %55)
br label %7
57: ; preds = %44
%58 = call i32 (...) @fork()
store i32 %58, i32* %5
%59 = load i32, i32* %5
%60 = icmp ne i32 %59, 0
br i1 %60, label %66, label %61
61: ; preds = %57
%62 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%63 = load i8*, i8** %62
%64 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%65 = call i32 @execvp(i8* %63, i8** %64)
br label %68
66: ; preds = %57
%67 = call i32 @wait(i32* null)
br label %7
68: ; preds = %61, %30
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
@.str = [32 x i8] c"\1B[36mronaldo's terminal %s$\1B[0m\00"
@.str.1 = [6 x i8] c"\1B[32m\00"
@stdin = global i32 0
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [3 x i8] c"cd\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [100 x i8*]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast [100 x i8*]* %2 to i8*
%5 = getelementptr inbounds [100 x i8*], [100 x i8*]* %2, i64 0, i64 0
%6 = getelementptr inbounds [100 x i8*], [100 x i8*]* %2, i64 0, i64 1
br label %7
7: ; preds = %42, %0
%8 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %3, i32 256, i32 %9)
br label %11
11: ; preds = %11, %7
%12 = phi i64 [ %16, %11 ], [ 0, %7 ]
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
%16 = add nuw i64 %12, 1
br i1 %15, label %17, label %11
17: ; preds = %11
%18 = and i64 %12, 4294967295
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %50, label %22
22: ; preds = %17
%23 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %32, label %25
25: ; preds = %22, %25
%26 = phi i64 [ %28, %25 ], [ 0, %22 ]
%27 = phi i8* [ %30, %25 ], [ %23, %22 ]
%28 = add nuw i64 %26, 1
%29 = getelementptr inbounds [100 x i8*], [100 x i8*]* %2, i64 0, i64 %26
store i8* %27, i8** %29
%30 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %25
32: ; preds = %25, %22
%33 = phi i64 [ 0, %22 ], [ %28, %25 ]
%34 = and i64 %33, 4294967295
%35 = getelementptr inbounds [100 x i8*], [100 x i8*]* %2, i64 0, i64 %34
store i8* null, i8** %35
%36 = load i8*, i8** %5
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i8*, i8** %6
%41 = call i32 @chdir(i8* %40)
br label %42
42: ; preds = %39, %48
br label %7
43: ; preds = %32
%44 = call i32 (...) @fork()
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @execvp(i8* %36, i8** %5)
br label %50
48: ; preds = %43
%49 = call i32 @wait(i32* null)
br label %42
50: ; preds = %17, %46
ret i32 0
}