#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int atoi(char const *);
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fidopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ process_builtin(int, char const **);
int /*<<< orphan*/ process_line(int, char const **);
scalar_t__ strcmp(char const *, char *);
char *strtok_r(char *, char *, char **);
int Shell(size_t argc, const char **argv) {
  int exitval = 0;
  char *cmdline = NULL;
  size_t cmdlinelen = 0;
  FILE *fin, *fout;
  fin = fidopen(0, "r");
  fout = fidopen(1, "w");
  fprintf(fout,
          "Starting tinyos shell\nType 'help' for help, 'exit' to quit.\n");
  const int ARGN = 128;
  for (;;) {
    const char *argv[ARGN];
    char *pos;
    int argc;
    fprintf(fout, "%% ");
    ssize_t rc;
  again:
    rc = getline(&cmdline, &cmdlinelen, fin);
    if (rc == -1 && ferror(fin) && errno == EINTR) {
      clearerr(fin);
      goto again;
    }
    if (rc == -1 && feof(fin)) {
      break;
    }
    for (argc = 0; argc < ARGN - 1; argc++) {
      argv[argc] = strtok_r((argc == 0 ? cmdline : NULL), " \n\t", &pos);
      if (argv[argc] == NULL)
        break;
    }
    if (argc == 0)
      continue;
    if (strcmp(argv[0], "exit") == 0) {
      if (argc >= 2)
        exitval = atoi(argv[1]);
      goto finished;
    }
    if (process_builtin(argc, argv))
      continue;
    process_line(argc, argv);
  }
  fprintf(fout, "Exiting\n");
finished:
  free(cmdline);
  fclose(fin);
  fclose(fout);
  return exitval;
}


x86 assembly (clang, -O0):
<Shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
xor    %edi,%edi
lea    0xe21(%rip),%rsi
call   10c0 <fidopen@plt>
mov    %rax,-0x30(%rbp)
mov    $0x1,%edi
lea    0xe0e(%rip),%rsi
call   10c0 <fidopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x80,-0x3c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rax
add    $0xfffffffffffffc00,%rax
mov    %rax,-0x68(%rbp)
mov    %rax,%rsp
mov    -0x38(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10e0 <getline@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    12b8 <Shell+0xe8>
mov    -0x30(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12b8 <Shell+0xe8>
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
jne    12b8 <Shell+0xe8>
mov    -0x30(%rbp),%rdi
call   10b0 <clearerr@plt>
jmp    125f <Shell+0x8f>
cmpl   $0xffffffff,-0x58(%rbp)
jne    12e1 <Shell+0x111>
mov    -0x30(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%rax
je     12e1 <Shell+0x111>
movl   $0x2,-0x5c(%rbp)
jmp    13ec <Shell+0x21c>
movl   $0x0,-0x54(%rbp)
cmpl   $0x7f,-0x54(%rbp)
jge    135e <Shell+0x18e>
cmpl   $0x0,-0x54(%rbp)
jne    1309 <Shell+0x139>
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    1314 <Shell+0x144>
xor    %eax,%eax
mov    %rax,-0x70(%rbp)
jmp    1314 <Shell+0x144>
mov    -0x70(%rbp),%rdi
lea    0xd4a(%rip),%rsi
lea    -0x50(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rdx
mov    -0x68(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movslq -0x54(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    134b <Shell+0x17b>
jmp    135e <Shell+0x18e>
jmp    1350 <Shell+0x180>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12e8 <Shell+0x118>
cmpl   $0x0,-0x54(%rbp)
jne    1374 <Shell+0x1a4>
movl   $0x3,-0x5c(%rbp)
jmp    13ec <Shell+0x21c>
mov    -0x68(%rbp),%rax
mov    (%rax),%rdi
lea    0xceb(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    13b7 <Shell+0x1e7>
cmpl   $0x2,-0x54(%rbp)
jl     13ab <Shell+0x1db>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0x8,-0x5c(%rbp)
jmp    13ec <Shell+0x21c>
mov    -0x68(%rbp),%rsi
mov    -0x54(%rbp),%edi
call   1080 <process_builtin@plt>
cmp    $0x0,%rax
je     13d9 <Shell+0x209>
movl   $0x3,-0x5c(%rbp)
jmp    13ec <Shell+0x21c>
mov    -0x68(%rbp),%rsi
mov    -0x54(%rbp),%edi
call   10f0 <process_line@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x5c(%rbp),%eax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
lea    0xbfb(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    1238 <Shell+0x68>
mov    -0x38(%rbp),%rdi
lea    0xc54(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Shell>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
lea    0xdf8(%rip),%rsi
xor    %edi,%edi
call   10c0 <fidopen@plt>
mov    %rax,%rbx
lea    0xde9(%rip),%rsi
mov    $0x1,%edi
call   10c0 <fidopen@plt>
lea    0xdda(%rip),%rdi
mov    $0x3c,%esi
mov    $0x1,%edx
mov    %rax,-0x38(%rbp)
mov    %rax,%rcx
call   10f0 <fwrite@plt>
lea    0xdfe(%rip),%r15
lea    -0x50(%rbp),%r14
jmp    1254 <Shell+0x74>
nopl   (%rax)
mov    -0x40(%rbp),%rsp
mov    %rsp,%r13
mov    %rsp,%r12
add    $0xfffffffffffffc00,%r12
mov    %r12,%rsp
mov    -0x38(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1090 <fprintf@plt>
lea    -0x30(%rbp),%rdi
lea    -0x48(%rbp),%rsi
mov    %rbx,%rdx
call   10e0 <getline@plt>
cmp    $0xffffffff,%eax
jne    12d1 <Shell+0xf1>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     12c0 <Shell+0xe0>
mov    0x2d43(%rip),%rax
mov    (%rax),%rax
mov    0x2d31(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <Shell+0xe0>
mov    %rbx,%rdi
call   10b0 <clearerr@plt>
jmp    1274 <Shell+0x94>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %rax,%rax
jne    137e <Shell+0x19e>
mov    %r13,-0x40(%rbp)
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
test   %r13,%r13
jne    12f0 <Shell+0x110>
mov    -0x30(%rbp),%rdi
jmp    12f2 <Shell+0x112>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <strtok_r@plt>
mov    %rax,(%r12,%r13,8)
test   %rax,%rax
je     1320 <Shell+0x140>
add    $0x1,%r13
cmp    $0x7f,%r13
jne    12e0 <Shell+0x100>
mov    $0x7f,%r13d
jmp    1329 <Shell+0x149>
nopl   0x0(%rax,%rax,1)
test   %r13d,%r13d
je     1250 <Shell+0x70>
mov    (%r12),%rdi
lea    0xd15(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1362 <Shell+0x182>
mov    %r13d,%edi
mov    %r12,%rsi
call   1080 <process_builtin@plt>
test   %rax,%rax
jne    1250 <Shell+0x70>
mov    %r13d,%edi
mov    %r12,%rsi
call   1100 <process_line@plt>
jmp    1250 <Shell+0x70>
xor    %r14d,%r14d
cmp    $0x2,%r13d
jb     1378 <Shell+0x198>
mov    0x8(%r12),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r14d
mov    -0x40(%rbp),%rsp
jmp    139e <Shell+0x1be>
mov    %r13,%rsp
lea    0xcc6(%rip),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    -0x38(%rbp),%rcx
call   10f0 <fwrite@plt>
xor    %r14d,%r14d
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [61 x i8] c"Starting tinyos shell\0AType 'help' for help, 'exit' to quit.\0A\00"
@.str.3 = [4 x i8] c"%% \00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [4 x i8] c" \0A\09\00"
@.str.5 = [5 x i8] c"exit\00"
@.str.6 = [9 x i8] c"Exiting\0A\00"
define i32 @Shell(i64 %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i64 %0, i64* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
store i8* null, i8** %6
store i64 0, i64* %7
%16 = call i32* @fidopen(i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = call i32* @fidopen(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0))
store i32 128, i32* %10
br label %20
20: ; preds = %103, %100, %2
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %11
%22 = alloca i8*, i64 128
%23 = load i32*, i32** %9
%24 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %38, %20
%26 = load i32*, i32** %8
%27 = call i32 @getline(i8** %6, i64* %7, i32* %26)
store i32 %27, i32* %14
%28 = load i32, i32* %14
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %41
30: ; preds = %25
%31 = load i32*, i32** %8
%32 = call i64 @ferror(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32*, i32** %8
%40 = call i32 @clearerr(i32* %39)
br label %25
41: ; preds = %34, %30, %25
%42 = load i32, i32* %14
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i32*, i32** %8
%46 = call i64 @feof(i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 2, i32* %15
br label %100
49: ; preds = %44, %41
store i32 0, i32* %13
br label %50
50: ; preds = %72, %49
%51 = load i32, i32* %13
%52 = icmp slt i32 %51, 127
br i1 %52, label %53, label %75
53: ; preds = %50
%54 = load i32, i32* %13
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = load i8*, i8** %6
br label %59
58: ; preds = %53
br label %59
59: ; preds = %58, %56
%60 = phi i8* [ %57, %56 ], [ null, %58 ]
%61 = call i8* @strtok_r(i8* %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %12)
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %22, i64 %63
store i8* %61, i8** %64
%65 = load i32, i32* %13
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8*, i8** %22, i64 %66
%68 = load i8*, i8** %67
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %71
70: ; preds = %59
br label %75
71: ; preds = %59
br label %72
72: ; preds = %71
%73 = load i32, i32* %13
%74 = add nsw i32 %73, 1
store i32 %74, i32* %13
br label %50
75: ; preds = %70, %50
%76 = load i32, i32* %13
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %75
store i32 3, i32* %15
br label %100
79: ; preds = %75
%80 = getelementptr inbounds i8*, i8** %22, i64 0
%81 = load i8*, i8** %80
%82 = call i64 @strcmp(i8* %81, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%83 = icmp eq i64 %82, 0
br i1 %83, label %84, label %92
84: ; preds = %79
%85 = load i32, i32* %13
%86 = icmp sge i32 %85, 2
br i1 %86, label %87, label %91
87: ; preds = %84
%88 = getelementptr inbounds i8*, i8** %22, i64 1
%89 = load i8*, i8** %88
%90 = call i32 @atoi(i8* %89)
store i32 %90, i32* %5
br label %91
91: ; preds = %87, %84
store i32 8, i32* %15
br label %100
92: ; preds = %79
%93 = load i32, i32* %13
%94 = call i64 @process_builtin(i32 %93, i8** %22)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %97
96: ; preds = %92
store i32 3, i32* %15
br label %100
97: ; preds = %92
%98 = load i32, i32* %13
%99 = call i32 @process_line(i32 %98, i8** %22)
store i32 0, i32* %15
br label %100
100: ; preds = %91, %97, %96, %78, %48
%101 = load i8*, i8** %11
%102 = load i32, i32* %15
switch i32 %102, label %115 [
i32 0, label %103
i32 2, label %104
i32 3, label %20
i32 8, label %107
]
103: ; preds = %100
br label %20
104: ; preds = %100
%105 = load i32*, i32** %9
%106 = call i32 @fprintf(i32* %105, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %107
107: ; preds = %104, %100
%108 = load i8*, i8** %6
%109 = call i32 @free(i8* %108)
%110 = load i32*, i32** %8
%111 = call i32 @fclose(i32* %110)
%112 = load i32*, i32** %9
%113 = call i32 @fclose(i32* %112)
%114 = load i32, i32* %5
ret i32 %114
115: ; preds = %100
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [61 x i8] c"Starting tinyos shell\0AType 'help' for help, 'exit' to quit.\0A\00"
@.str.3 = [4 x i8] c"%% \00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [4 x i8] c" \0A\09\00"
@.str.5 = [5 x i8] c"exit\00"
@.str.6 = [9 x i8] c"Exiting\0A\00"
define i32 @Shell(i64 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%8 = tail call i32* @fidopen(i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32* @fidopen(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i64 60, i64 1, i32* %9)
%11 = bitcast i8** %5 to i8*
br label %12
12: ; preds = %66, %2
%13 = call i8* @llvm.stacksave()
%14 = alloca [128 x i8*]
%15 = getelementptr inbounds [128 x i8*], [128 x i8*]* %14, i64 0, i64 0
%16 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @getline(i8** %3, i64* %4, i32* %8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %33
19: ; preds = %12, %26
%20 = call i64 @ferror(i32* %8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = call i32 @clearerr(i32* %8)
%28 = call i32 @getline(i8** %3, i64* %4, i32* %8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %19, label %33
30: ; preds = %19, %22
%31 = call i64 @feof(i32* %8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %67
33: ; preds = %26, %12, %30
br label %34
34: ; preds = %33, %42
%35 = phi i64 [ %43, %42 ], [ 0, %33 ]
%36 = icmp eq i64 %35, 0
%37 = load i8*, i8** %3
%38 = select i1 %36, i8* %37, i8* null
%39 = call i8* @strtok_r(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %5)
%40 = getelementptr inbounds [128 x i8*], [128 x i8*]* %14, i64 0, i64 %35
store i8* %39, i8** %40
%41 = icmp eq i8* %39, null
br i1 %41, label %45, label %42
42: ; preds = %34
%43 = add nuw nsw i64 %35, 1
%44 = icmp eq i64 %43, 127
br i1 %44, label %48, label %34
45: ; preds = %34
%46 = trunc i64 %35 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %66, label %48
48: ; preds = %42, %45
%49 = phi i32 [ %46, %45 ], [ 127, %42 ]
%50 = load i8*, i8** %15
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = icmp ugt i32 %49, 1
br i1 %54, label %55, label %64
55: ; preds = %53
%56 = getelementptr inbounds [128 x i8*], [128 x i8*]* %14, i64 0, i64 1
%57 = load i8*, i8** %56
%58 = call i32 @atoi(i8* %57)
br label %64
59: ; preds = %48
%60 = call i64 @process_builtin(i32 %49, i8** %15)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = call i32 @process_line(i32 %49, i8** %15)
br label %66
64: ; preds = %55, %53
%65 = phi i32 [ 0, %53 ], [ %58, %55 ]
br label %69
66: ; preds = %59, %45, %62
br label %12
67: ; preds = %30
%68 = call i64 @fwrite(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i64 8, i64 1, i32* %9)
br label %69
69: ; preds = %67, %64
%70 = phi i32 [ %65, %64 ], [ 0, %67 ]
%71 = load i8*, i8** %3
%72 = call i32 @free(i8* %71)
%73 = call i32 @fclose(i32* %8)
%74 = call i32 @fclose(i32* %9)
ret i32 %70
}