#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int get_command_string(char *command) {
  int cmdsize = 0;
  char cc;
  while (read(STDIN_FILENO, &cc, 1)) {
    if (cc == '\033') {
      command[0] = '\0';
      return 0;
    }
    if (cc == '\b' || cc == 0x7f) {
      command[cmdsize] = '\0';
      cmdsize--;
      if (cmdsize < 0)
        return 0;
      ;
      write(STDOUT_FILENO, "\b \b", 3);
      continue;
    }
    if (cc == '\n' || cc == '\r') {
      command[cmdsize] = '\0';
      break;
    }
    write(STDOUT_FILENO, &cc, 1);
    command[cmdsize] = cc;
    cmdsize++;
    if (cmdsize == 128)
      break;
  }
  return cmdsize;
}


x86 assembly (clang, -O0):
<get_command_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
je     1248 <get_command_string+0x128>
movsbl -0x15(%rbp),%eax
cmp    $0x1b,%eax
jne    1174 <get_command_string+0x54>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    124e <get_command_string+0x12e>
movsbl -0x15(%rbp),%eax
cmp    $0x8,%eax
je     118e <get_command_string+0x6e>
movsbl -0x15(%rbp),%eax
cmp    $0x7f,%eax
jne    11d8 <get_command_string+0xb8>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b9 <get_command_string+0x99>
movl   $0x0,-0x4(%rbp)
jmp    124e <get_command_string+0x12e>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    1133 <get_command_string+0x13>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
je     11f2 <get_command_string+0xd2>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    1203 <get_command_string+0xe3>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1248 <get_command_string+0x128>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x80,-0x14(%rbp)
jne    1243 <get_command_string+0x123>
jmp    1248 <get_command_string+0x128>
jmp    1133 <get_command_string+0x13>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_command_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ea3(%rip),%r12
mov    (%r12),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1218 <get_command_string+0xf8>
lea    0x7(%rsp),%r15
lea    0xea1(%rip),%r14
jmp    11ae <get_command_string+0x8e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movslq %ebp,%rax
mov    %r13b,(%rbx,%rax,1)
add    $0x1,%ebp
cmp    $0x80,%ebp
je     120c <get_command_string+0xec>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
je     1218 <get_command_string+0xf8>
movzbl 0x7(%rsp),%r13d
lea    -0x8(%r13),%eax
cmp    $0x13,%al
ja     11d0 <get_command_string+0xb0>
movzbl %al,%eax
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
nopl   0x0(%rax,%rax,1)
cmp    $0x7f,%r13b
jne    1170 <get_command_string+0x50>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
test   %ebp,%ebp
jle    1208 <get_command_string+0xe8>
add    $0xffffffff,%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
jmp    1198 <get_command_string+0x78>
movslq %ebp,%rax
add    %rax,%rbx
jmp    1215 <get_command_string+0xf5>
xor    %ebp,%ebp
jmp    1218 <get_command_string+0xf8>
mov    $0x80,%ebp
jmp    1218 <get_command_string+0xf8>
xor    %ebp,%ebp
movb   $0x0,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"\08 \08\00"
define i32 @get_command_string(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
store i32 0, i32* %4
br label %6
6: ; preds = %64, %35, %1
%7 = load i32, i32* @STDIN_FILENO
%8 = call i64 @read(i32 %7, i8* %5, i32 1)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %65
10: ; preds = %6
%11 = load i8, i8* %5
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 27
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
store i32 0, i32* %2
br label %67
17: ; preds = %10
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 8
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 127
br i1 %24, label %25, label %38
25: ; preds = %21, %17
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* %4
%31 = add nsw i32 %30, -1
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %25
store i32 0, i32* %2
br label %67
35: ; preds = %25
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %6
38: ; preds = %21
%39 = load i8, i8* %5
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i8, i8* %5
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %46, label %51
46: ; preds = %42, %38
%47 = load i8*, i8** %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %65
51: ; preds = %42
%52 = load i32, i32* @STDOUT_FILENO
%53 = call i32 @write(i32 %52, i8* %5, i32 1)
%54 = load i8, i8* %5
%55 = load i8*, i8** %3
%56 = load i32, i32* %4
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 %54, i8* %58
%59 = load i32, i32* %4
%60 = add nsw i32 %59, 1
store i32 %60, i32* %4
%61 = load i32, i32* %4
%62 = icmp eq i32 %61, 128
br i1 %62, label %63, label %64
63: ; preds = %51
br label %65
64: ; preds = %51
br label %6
65: ; preds = %63, %46, %6
%66 = load i32, i32* %4
store i32 %66, i32* %2
br label %67
67: ; preds = %65, %34, %14
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"\08 \08\00"
define i32 @get_command_string(i8* nocapture writeonly %0) {
%2 = alloca i8
%3 = load i32, i32* @STDIN_FILENO
%4 = call i64 @read(i32 %3, i8* %2, i32 1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %35, label %6
6: ; preds = %1, %17
%7 = phi i32 [ %18, %17 ], [ 0, %1 ]
%8 = load i8, i8* %2
switch i8 %8, label %25 [
i8 27, label %32
i8 127, label %9
i8 8, label %9
i8 13, label %22
i8 10, label %22
]
9: ; preds = %6, %6
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
%12 = icmp slt i32 %7, 1
br i1 %12, label %35, label %13
13: ; preds = %9
%14 = add nsw i32 %7, -1
%15 = load i32, i32* @STDOUT_FILENO
%16 = tail call i32 @write(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %17
17: ; preds = %13, %25
%18 = phi i32 [ %14, %13 ], [ %30, %25 ]
%19 = load i32, i32* @STDIN_FILENO
%20 = call i64 @read(i32 %19, i8* %2, i32 1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %35, label %6
22: ; preds = %6, %6
%23 = sext i32 %7 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
br label %32
25: ; preds = %6
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @write(i32 %26, i8* %2, i32 1)
%28 = sext i32 %7 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 %8, i8* %29
%30 = add nsw i32 %7, 1
%31 = icmp eq i32 %30, 128
br i1 %31, label %35, label %17
32: ; preds = %6, %22
%33 = phi i8* [ %24, %22 ], [ %0, %6 ]
%34 = phi i32 [ %7, %22 ], [ 0, %6 ]
store i8 0, i8* %33
br label %35
35: ; preds = %9, %25, %17, %32, %1
%36 = phi i32 [ 0, %1 ], [ %34, %32 ], [ 0, %9 ], [ 128, %25 ], [ %18, %17 ]
ret i32 %36
}