#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 */
char interrupt(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int /*<<< orphan*/);
int /*<<< orphan*/ printString(char *);
void readString(char *buffer) {
  char currentChar;
  char bufferToPrint[3];
  unsigned int i = 0;
  bufferToPrint[1] = '\0';
  do {
    currentChar = interrupt(0x16, 0, 0, 0, 0);
    switch (currentChar) {
    case 0xd:
      buffer[i] = 0xd;
      i++;
      buffer[i] = 0xa;
      i++;
      buffer[i] = 0x0;
      i++;
      bufferToPrint[0] = '\r';
      bufferToPrint[1] = '\n';
      bufferToPrint[2] = '\0';
      printString(bufferToPrint);
      return;
    case 0x8:
      if (i > 0) {
        bufferToPrint[0] = currentChar;
        printString(bufferToPrint);
        bufferToPrint[0] = ' ';
        printString(bufferToPrint);
        bufferToPrint[0] = currentChar;
        printString(bufferToPrint);
        i--;
      }
      break;
    default:
      buffer[i] = currentChar;
      bufferToPrint[0] = currentChar;
      printString(bufferToPrint);
      i++;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0xb(%rbp)
mov    $0x16,%edi
xor    %r8d,%r8d
mov    %r8d,%esi
mov    %r8d,%edx
mov    %r8d,%ecx
call   1030 <interrupt@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x8,%eax
je     11cc <readString+0xac>
jmp    1165 <readString+0x45>
mov    -0x14(%rbp),%eax
sub    $0xd,%eax
jne    120f <readString+0xef>
jmp    1176 <readString+0x56>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movb   $0xd,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
movb   $0xd,-0xc(%rbp)
movb   $0xa,-0xb(%rbp)
movb   $0x0,-0xa(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
jmp    1248 <readString+0x128>
cmpl   $0x0,-0x10(%rbp)
jbe    120a <readString+0xea>
mov    -0x9(%rbp),%al
mov    %al,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
movb   $0x20,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
mov    -0x9(%rbp),%al
mov    %al,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1234 <readString+0x114>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x9(%rbp),%al
mov    %al,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1239 <readString+0x119>
mov    $0x1,%al
test   $0x1,%al
jne    1137 <readString+0x17>
jmp    1248 <readString+0x128>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movb   $0x0,0xe(%rsp)
xor    %eax,%eax
lea    0xd(%rsp),%r15
jmp    117a <readString+0x5a>
nopl   (%rax)
mov    $0x0,%eax
test   %r12d,%r12d
je     117a <readString+0x5a>
mov    %bl,0xd(%rsp)
mov    %r15,%rdi
call   1040 <printString@plt>
movb   $0x20,0xd(%rsp)
mov    %r15,%rdi
call   1040 <printString@plt>
mov    $0xffffffff,%ebp
mov    %bl,0xd(%rsp)
mov    %r15,%rdi
call   1040 <printString@plt>
add    %ebp,%r12d
mov    %r12d,%eax
mov    %eax,%r12d
mov    $0x16,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <interrupt@plt>
mov    %eax,%ebx
movsbl %al,%ecx
cmp    $0x8,%ecx
je     1140 <readString+0x20>
mov    %r12d,%eax
cmp    $0xd,%ecx
je     11ad <readString+0x8d>
mov    %bl,(%r14,%rax,1)
mov    $0x1,%ebp
jmp    1168 <readString+0x48>
movb   $0xd,(%r14,%rax,1)
lea    0x1(%r12),%eax
movb   $0xa,(%r14,%rax,1)
add    $0x2,%r12d
movb   $0x0,(%r14,%r12,1)
movw   $0xa0d,0xd(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xd(%rsp),%rdi
call   1040 <printString@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @readString(i8* nocapture writeonly %0) {
%2 = alloca [3 x i8]
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 1
store i8 0, i8* %4
br label %5
5: ; preds = %32, %1
%6 = phi i32 [ 0, %1 ], [ %33, %32 ]
%7 = call signext i8 @interrupt(i32 22, i32 0, i32 0, i32 0, i32 0)
%8 = sext i8 %7 to i32
switch i32 %8, label %25 [
i32 13, label %9
i32 8, label %20
]
9: ; preds = %5
%10 = zext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 13, i8* %11
%12 = add i32 %6, 1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 10, i8* %14
%15 = add i32 %6, 2
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 0, i8* %17
store i8 13, i8* %3
store i8 10, i8* %4
%18 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 2
store i8 0, i8* %18
%19 = call i32 @printString(i8* %3)
ret void
20: ; preds = %5
%21 = icmp eq i32 %6, 0
br i1 %21, label %32, label %22
22: ; preds = %20
store i8 %7, i8* %3
%23 = call i32 @printString(i8* %3)
store i8 32, i8* %3
%24 = call i32 @printString(i8* %3)
br label %28
25: ; preds = %5
%26 = zext i32 %6 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 %7, i8* %27
br label %28
28: ; preds = %22, %25
%29 = phi i32 [ 1, %25 ], [ -1, %22 ]
store i8 %7, i8* %3
%30 = call i32 @printString(i8* %3)
%31 = add i32 %6, %29
br label %32
32: ; preds = %28, %20
%33 = phi i32 [ 0, %20 ], [ %31, %28 ]
br label %5
}