#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ add_to_history(char *);
int /*<<< orphan*/ do_getcwd(char *, int);
int /*<<< orphan*/ do_setcolor(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ proccmd(char *, char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int strlen(char *);
void readcmd(FILE *in, char **pathv) {
  char cmd[513 + 1] = {0};
  int i = -1;
  while (1) {
    if (in == stdin) {
      do_setcolor(1);
      printf("rash");
      do_setcolor(6);
      printf(":");
      do_setcolor(2);
      do_getcwd(cmd, 513);
      printf(&cmd[i + 1]);
      do_setcolor(1);
      printf("$ ");
      do_setcolor(6);
    }
    if (!fgets(cmd, 513, in)) {
      if (in == stdin)
        printf("\n");
      return;
    }
    if (strlen(cmd) == 513 - 1 && !feof(in) && cmd[strlen(cmd) - 1] != '\n') {
      while (fgets(cmd, 513, in) && cmd[strlen(cmd) - 1] != '\n')
        ;
      fprintf(stderr, "Error: Command is too long.\n");
      continue;
    }
    add_to_history(cmd);
    proccmd(cmd, pathv);
  }
}


x86 assembly (clang, -O0):
<readcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x202,%edx
call   1080 <memset@plt>
movl   $0xffffffff,-0x224(%rbp)
ff ff ff
mov    -0x8(%rbp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    1265 <readcmd+0xc5>
mov    $0x1,%edi
call   1030 <do_setcolor@plt>
lea    0xe0b(%rip),%rdi
call   1070 <printf@plt>
mov    $0x6,%edi
call   1030 <do_setcolor@plt>
lea    0xdfa(%rip),%rdi
call   1070 <printf@plt>
mov    $0x2,%edi
call   1030 <do_setcolor@plt>
lea    -0x220(%rbp),%rdi
mov    $0x201,%esi
call   1090 <do_getcwd@plt>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
cltq
lea    -0x220(%rbp),%rdi
add    %rax,%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   1030 <do_setcolor@plt>
lea    0xdb1(%rip),%rdi
call   1070 <printf@plt>
mov    $0x6,%edi
call   1030 <do_setcolor@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x201,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
jne    12ad <readcmd+0x10d>
mov    -0x8(%rbp),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
jne    12a4 <readcmd+0x104>
lea    0xd86(%rip),%rdi
call   1070 <printf@plt>
add    $0x230,%rsp
pop    %rbp
ret
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x200,%eax
jne    137e <readcmd+0x1de>
mov    -0x8(%rbp),%rdi
call   10c0 <feof@plt>
cmp    $0x0,%eax
jne    137e <readcmd+0x1de>
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     137e <readcmd+0x1de>
jmp    12fd <readcmd+0x15d>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x201,%esi
call   10a0 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x225(%rbp)
je     134c <readcmd+0x1ac>
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
setne  %al
mov    %al,-0x225(%rbp)
mov    -0x225(%rbp),%al
test   $0x1,%al
jne    135f <readcmd+0x1bf>
jmp    1364 <readcmd+0x1c4>
jmp    12fd <readcmd+0x15d>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
lea    0xc96(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    11d0 <readcmd+0x30>
lea    -0x220(%rbp),%rdi
call   1040 <add_to_history@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <proccmd@plt>
jmp    11d0 <readcmd+0x30>

x86 assembly (clang, -O2):
<readcmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rbx
mov    $0x202,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
mov    0x2e08(%rip),%rbp
lea    0xe19(%rip),%r12
lea    0xe17(%rip),%r13
cmp    %r15,0x0(%rbp)
jne    127a <readcmd+0xca>
jmp    1219 <readcmd+0x69>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <add_to_history@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <proccmd@plt>
cmp    %r15,0x0(%rbp)
jne    127a <readcmd+0xca>
mov    $0x1,%edi
call   1040 <do_setcolor@plt>
mov    %r12,%rdi
call   1080 <printf@plt>
mov    $0x6,%edi
call   1040 <do_setcolor@plt>
mov    $0x3a,%edi
call   1030 <putchar@plt>
mov    $0x2,%edi
call   1040 <do_setcolor@plt>
mov    %rbx,%rdi
mov    $0x201,%esi
call   10a0 <do_getcwd@plt>
mov    %rbx,%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   1040 <do_setcolor@plt>
mov    %r13,%rdi
call   1080 <printf@plt>
mov    $0x6,%edi
call   1040 <do_setcolor@plt>
mov    %rbx,%rdi
mov    $0x201,%esi
mov    %r15,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     131a <readcmd+0x16a>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    $0x200,%eax
jne    1200 <readcmd+0x50>
mov    %r15,%rdi
call   10d0 <feof@plt>
test   %eax,%eax
jne    1200 <readcmd+0x50>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1200 <readcmd+0x50>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x201,%esi
mov    %r15,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     12f6 <readcmd+0x146>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    12d0 <readcmd+0x120>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
call   10c0 <fprintf@plt>
cmp    %r15,0x0(%rbp)
jne    127a <readcmd+0xca>
jmp    1219 <readcmd+0x69>
cmp    %r15,0x0(%rbp)
jne    132a <readcmd+0x17a>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32* null
@.str = [5 x i8] c"rash\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [3 x i8] c"$ \00"
@.str.3 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"Error: Command is too long.\0A\00"
define void @readcmd(i32* %0, i8** %1) {
%3 = alloca i32*
%4 = alloca i8**
%5 = alloca [514 x i8]
%6 = alloca i32
store i32* %0, i32** %3
store i8** %1, i8*** %4
%7 = bitcast [514 x i8]* %5 to i8*
store i32 -1, i32* %6
br label %8
8: ; preds = %2, %75, %78
%9 = load i32*, i32** %3
%10 = load i32*, i32** @stdin
%11 = icmp eq i32* %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = call i32 @do_setcolor(i32 1)
%14 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @do_setcolor(i32 6)
%16 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @do_setcolor(i32 2)
%18 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%19 = call i32 @do_getcwd(i8* %18, i32 513)
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 %22
%24 = call i32 @printf(i8* %23)
%25 = call i32 @do_setcolor(i32 1)
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @do_setcolor(i32 6)
br label %28
28: ; preds = %12, %8
%29 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%30 = load i32*, i32** %3
%31 = call i64 @fgets(i8* %29, i32 513, i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %28
%34 = load i32*, i32** %3
%35 = load i32*, i32** @stdin
%36 = icmp eq i32* %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
ret void
40: ; preds = %28
%41 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = icmp eq i32 %42, 512
br i1 %43, label %44, label %78
44: ; preds = %40
%45 = load i32*, i32** %3
%46 = call i32 @feof(i32* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %78, label %48
48: ; preds = %44
%49 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 10
br i1 %56, label %57, label %78
57: ; preds = %48
br label %58
58: ; preds = %74, %57
%59 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%60 = load i32*, i32** %3
%61 = call i64 @fgets(i8* %59, i32 513, i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %58
%64 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
%66 = sub nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 10
br label %72
72: ; preds = %63, %58
%73 = phi i1 [ false, %58 ], [ %71, %63 ]
br i1 %73, label %74, label %75
74: ; preds = %72
br label %58
75: ; preds = %72
%76 = load i32, i32* @stderr
%77 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %8
78: ; preds = %48, %44, %40
%79 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%80 = call i32 @add_to_history(i8* %79)
%81 = getelementptr inbounds [514 x i8], [514 x i8]* %5, i64 0, i64 0
%82 = load i8**, i8*** %4
%83 = call i32 @proccmd(i8* %81, i8** %82)
br label %8
}

LLVM IR (-O2):
@stdin = global i32* null
@.str = [5 x i8] c"rash\00"
@.str.2 = [3 x i8] c"$ \00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"Error: Command is too long.\0A\00"
define void @readcmd(i32* nocapture %0, i8** %1) {
%3 = alloca [514 x i8]
%4 = getelementptr inbounds [514 x i8], [514 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %54, %2
%6 = load i32*, i32** @stdin
%7 = icmp eq i32* %6, %0
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = call i32 @do_setcolor(i32 1)
%10 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @do_setcolor(i32 6)
%12 = call i32 @putchar(i32 58)
%13 = call i32 @do_setcolor(i32 2)
%14 = call i32 @do_getcwd(i8* %4, i32 513)
%15 = call i32 @printf(i8* %4)
%16 = call i32 @do_setcolor(i32 1)
%17 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @do_setcolor(i32 6)
br label %19
19: ; preds = %8, %5
%20 = call i64 @fgets(i8* %4, i32 513, i32* %0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32*, i32** @stdin
%24 = icmp eq i32* %23, %0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @putchar(i32 10)
br label %27
27: ; preds = %25, %22
ret void
28: ; preds = %19
%29 = call i32 @strlen(i8* %4)
%30 = icmp eq i32 %29, 512
br i1 %30, label %31, label %55
31: ; preds = %28
%32 = call i32 @feof(i32* %0)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %55
34: ; preds = %31
%35 = call i32 @strlen(i8* %4)
%36 = add nsw i32 %35, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [514 x i8], [514 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 10
br i1 %40, label %55, label %41
41: ; preds = %34, %44
%42 = call i64 @fgets(i8* %4, i32 513, i32* %0)
%43 = icmp eq i64 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %41
%45 = call i32 @strlen(i8* %4)
%46 = add nsw i32 %45, -1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [514 x i8], [514 x i8]* %3, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = icmp eq i8 %49, 10
br i1 %50, label %51, label %41
51: ; preds = %41, %44
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %51, %55
br label %5
55: ; preds = %34, %31, %28
%56 = call i32 @add_to_history(i8* %4)
%57 = call i32 @proccmd(i8* %4, i8** %1)
br label %54
}