#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:
      bufferToPrint[0] = currentChar;
      if (i > 0) {
        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    11f3 <readString+0xd3>
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    122c <readString+0x10c>
mov    -0x9(%rbp),%al
mov    %al,-0xc(%rbp)
cmpl   $0x0,-0x10(%rbp)
jbe    11ee <readString+0xce>
lea    -0xc(%rbp),%rdi
call   1040 <printString@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1218 <readString+0xf8>
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    121d <readString+0xfd>
mov    $0x1,%al
test   $0x1,%al
jne    1137 <readString+0x17>
jmp    122c <readString+0x10c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
movb   $0x0,0x6(%rsp)
xor    %eax,%eax
lea    0x5(%rsp),%r14
jmp    115e <readString+0x3e>
nopl   0x0(%rax,%rax,1)
mov    %al,0x5(%rsp)
mov    $0xffffffff,%ebp
mov    $0x0,%eax
test   %ebx,%ebx
je     115e <readString+0x3e>
mov    %r14,%rdi
call   1040 <printString@plt>
add    %ebp,%ebx
mov    %ebx,%eax
mov    %eax,%ebx
mov    $0x16,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <interrupt@plt>
movsbl %al,%edx
cmp    $0x8,%edx
je     1140 <readString+0x20>
mov    %ebx,%ecx
cmp    $0xd,%edx
je     1191 <readString+0x71>
mov    %al,(%r15,%rcx,1)
mov    %al,0x5(%rsp)
mov    $0x1,%ebp
jmp    1152 <readString+0x32>
movb   $0xd,(%r15,%rcx,1)
lea    0x1(%rbx),%eax
movb   $0xa,(%r15,%rax,1)
add    $0x2,%ebx
movb   $0x0,(%r15,%rbx,1)
movw   $0xa0d,0x5(%rsp)
movb   $0x0,0x7(%rsp)
lea    0x5(%rsp),%rdi
call   1040 <printString@plt>
add    $0x8,%rsp
pop    %rbx
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 = %59, %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 %46 [
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 %60
35: ; preds = %7
%36 = load i8, i8* %3
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
store i8 %36, i8* %37
%38 = load i32, i32* %5
%39 = icmp ugt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%42 = call i32 @printString(i8* %41)
%43 = load i32, i32* %5
%44 = add i32 %43, -1
store i32 %44, i32* %5
br label %45
45: ; preds = %40, %35
br label %58
46: ; preds = %7
%47 = load i8, i8* %3
%48 = load i8*, i8** %2
%49 = load i32, i32* %5
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
%52 = load i8, i8* %3
%53 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
store i8 %52, i8* %53
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%55 = call i32 @printString(i8* %54)
%56 = load i32, i32* %5
%57 = add i32 %56, 1
store i32 %57, i32* %5
br label %58
58: ; preds = %46, %45
br label %59
59: ; preds = %58
br i1 true, label %7, label %60
60: ; preds = %11, %59
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 = %29, %1
%6 = phi i32 [ 0, %1 ], [ %30, %29 ]
%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 %22 [
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
store i8 %7, i8* %3
%21 = icmp eq i32 %6, 0
br i1 %21, label %29, label %25
22: ; preds = %5
%23 = zext i32 %6 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 %7, i8* %24
store i8 %7, i8* %3
br label %25
25: ; preds = %20, %22
%26 = phi i32 [ 1, %22 ], [ -1, %20 ]
%27 = call i32 @printString(i8* %3)
%28 = add i32 %6, %26
br label %29
29: ; preds = %25, %20
%30 = phi i32 [ 0, %20 ], [ %28, %25 ]
br label %5
}