#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 getchar();
int /*<<< orphan*/ putchar(char);
void getcmd(char *buf, size_t len) {
  size_t pos = 0;
  int done = 0, ch;
  while (!done) {
    ch = getchar();
    if ((ch == '\b' || ch == 127) && pos > 0) {
      putchar('\b');
      putchar(' ');
      putchar('\b');
      pos--;
    } else if (ch == '\r' || ch == '\n') {
      putchar('\r');
      putchar('\n');
      done = 1;
    } else if (ch >= 32 && ch < 127 && pos < len - 1) {
      buf[pos++] = ch;
      putchar(ch);
    } else {
      putchar('\a');
    }
  }
  buf[pos] = 0;
}


x86 assembly (clang, -O0):
<getcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1155 <getcmd+0x35>
jmp    124e <getcmd+0x12e>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x8,-0x20(%rbp)
je     1173 <getcmd+0x53>
cmpl   $0x7f,-0x20(%rbp)
jne    11ad <getcmd+0x8d>
cmpq   $0x0,-0x18(%rbp)
jbe    11ad <getcmd+0x8d>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1249 <getcmd+0x129>
cmpl   $0xd,-0x20(%rbp)
je     11c1 <getcmd+0xa1>
cmpl   $0xa,-0x20(%rbp)
jne    11e1 <getcmd+0xc1>
mov    $0xd,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x1,-0x1c(%rbp)
jmp    1244 <getcmd+0x124>
cmpl   $0x20,-0x20(%rbp)
jl     1235 <getcmd+0x115>
cmpl   $0x7f,-0x20(%rbp)
jge    1235 <getcmd+0x115>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    1235 <getcmd+0x115>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    123f <getcmd+0x11f>
mov    $0x7,%edi
call   1030 <putchar@plt>
jmp    1244 <getcmd+0x124>
jmp    1249 <getcmd+0x129>
jmp    113f <getcmd+0x1f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcmd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
add    $0xffffffffffffffff,%r15
xor    %ebx,%ebx
jmp    114a <getcmd+0x2a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x7,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0x8,%eax
setne  %cl
cmp    $0x7f,%eax
setne  %dl
test   %dl,%cl
jne    1166 <getcmd+0x46>
test   %rbx,%rbx
jne    118f <getcmd+0x6f>
cmp    $0xa,%eax
je     11b3 <getcmd+0x93>
cmp    $0xd,%eax
je     11b3 <getcmd+0x93>
lea    -0x20(%rax),%ecx
cmp    $0x5e,%ecx
ja     1140 <getcmd+0x20>
cmp    %r15,%rbx
jae    1140 <getcmd+0x20>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    114a <getcmd+0x2a>
mov    $0x8,%edi
call   1030 <putchar@plt>
mov    $0x20,%edi
call   1030 <putchar@plt>
mov    $0x8,%edi
call   1030 <putchar@plt>
add    $0xffffffffffffffff,%rbx
jmp    114a <getcmd+0x2a>
mov    $0xd,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movb   $0x0,(%r14,%rbx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @getcmd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %5
store i32 0, i32* %6
br label %8
8: ; preds = %62, %2
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %63
12: ; preds = %8
%13 = call i32 (...) @getchar()
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 8
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 127
br i1 %18, label %19, label %28
19: ; preds = %16, %12
%20 = load i64, i64* %5
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i32 @putchar(i8 signext 8)
%24 = call i32 @putchar(i8 signext 32)
%25 = call i32 @putchar(i8 signext 8)
%26 = load i64, i64* %5
%27 = add i64 %26, -1
store i64 %27, i64* %5
br label %62
28: ; preds = %19, %16
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 13
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %37
34: ; preds = %31, %28
%35 = call i32 @putchar(i8 signext 13)
%36 = call i32 @putchar(i8 signext 10)
store i32 1, i32* %6
br label %61
37: ; preds = %31
%38 = load i32, i32* %7
%39 = icmp sge i32 %38, 32
br i1 %39, label %40, label %58
40: ; preds = %37
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 127
br i1 %42, label %43, label %58
43: ; preds = %40
%44 = load i64, i64* %5
%45 = load i64, i64* %4
%46 = sub i64 %45, 1
%47 = icmp ult i64 %44, %46
br i1 %47, label %48, label %58
48: ; preds = %43
%49 = load i32, i32* %7
%50 = trunc i32 %49 to i8
%51 = load i8*, i8** %3
%52 = load i64, i64* %5
%53 = add i64 %52, 1
store i64 %53, i64* %5
%54 = getelementptr inbounds i8, i8* %51, i64 %52
store i8 %50, i8* %54
%55 = load i32, i32* %7
%56 = trunc i32 %55 to i8
%57 = call i32 @putchar(i8 signext %56)
br label %60
58: ; preds = %43, %40, %37
%59 = call i32 @putchar(i8 signext 7)
br label %60
60: ; preds = %58, %48
br label %61
61: ; preds = %60, %34
br label %62
62: ; preds = %61, %22
br label %8
63: ; preds = %8
%64 = load i8*, i8** %3
%65 = load i64, i64* %5
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8 0, i8* %66
ret void
}

LLVM IR (-O2):
define void @getcmd(i8* nocapture writeonly %0, i64 %1) {
%3 = add i64 %1, -1
br label %4
4: ; preds = %29, %2
%5 = phi i64 [ 0, %2 ], [ %30, %29 ]
%6 = icmp ne i64 %5, 0
%7 = icmp ult i64 %5, %3
br label %8
8: ; preds = %4, %31
%9 = tail call i32 (...) @getchar()
%10 = icmp eq i32 %9, 8
%11 = icmp eq i32 %9, 127
%12 = or i1 %10, %11
%13 = select i1 %12, i1 %6, i1 false
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = tail call i32 @putchar(i8 signext 8)
%16 = tail call i32 @putchar(i8 signext 32)
%17 = tail call i32 @putchar(i8 signext 8)
%18 = add i64 %5, -1
br label %29
19: ; preds = %8
switch i32 %9, label %20 [
i32 13, label %33
i32 10, label %33
]
20: ; preds = %19
%21 = add i32 %9, -32
%22 = icmp ult i32 %21, 95
%23 = select i1 %22, i1 %7, i1 false
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = trunc i32 %9 to i8
%26 = add nuw i64 %5, 1
%27 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 %25, i8* %27
%28 = tail call i32 @putchar(i8 signext %25)
br label %29
29: ; preds = %24, %14
%30 = phi i64 [ %18, %14 ], [ %26, %24 ]
br label %4
31: ; preds = %20
%32 = tail call i32 @putchar(i8 signext 7)
br label %8
33: ; preds = %19, %19
%34 = tail call i32 @putchar(i8 signext 13)
%35 = tail call i32 @putchar(i8 signext 10)
%36 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 0, i8* %36
ret void
}