#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*/ _read(char volatile *, int);
int /*<<< orphan*/ _remove_char();
int /*<<< orphan*/ putchar(char volatile);
void read(char *buffer, int bytes) {
  int index = 0;
  volatile char key_holder = '\0';
  _read(&key_holder, bytes);
  while (index < bytes) {
    if (key_holder == '\0')
      continue;
    else if (key_holder == 8) {
      if (index > 0) {
        buffer[--index] = '\0';
        _remove_char();
      }
    } else {
      buffer[index++] = key_holder;
      putchar(key_holder);
    }
    if (key_holder == '\n')
      break;
    key_holder = '\0';
  }
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0xc(%rbp),%esi
lea    -0x11(%rbp),%rdi
call   1050 <_read@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f4 <read+0xc4>
mov    -0x11(%rbp),%al
movsbl %al,%eax
cmp    $0x0,%eax
jne    1176 <read+0x46>
jmp    1156 <read+0x26>
mov    -0x11(%rbp),%al
movsbl %al,%eax
cmp    $0x8,%eax
jne    11af <read+0x7f>
cmpl   $0x0,-0x10(%rbp)
jle    11aa <read+0x7a>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    $0x0,%al
call   1040 <_remove_char@plt>
jmp    11d2 <read+0xa2>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x11(%rbp),%al
movsbl %al,%edi
call   1030 <putchar@plt>
jmp    11d7 <read+0xa7>
mov    -0x11(%rbp),%al
movsbl %al,%eax
cmp    $0xa,%eax
jne    11eb <read+0xbb>
jmp    11f4 <read+0xc4>
movb   $0x0,-0x11(%rbp)
jmp    1156 <read+0x26>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%rdi
call   1050 <_read@plt>
test   %ebp,%ebp
jle    11b0 <read+0x80>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x0,0xf(%rsp)
je     1160 <read+0x30>
movzbl 0xf(%rsp),%eax
cmp    $0x8,%al
jne    1185 <read+0x55>
test   %ebx,%ebx
jle    119e <read+0x6e>
add    $0xffffffff,%ebx
movb   $0x0,(%r14,%rbx,1)
xor    %eax,%eax
call   1040 <_remove_char@plt>
jmp    119e <read+0x6e>
movzbl 0xf(%rsp),%eax
movslq %ebx,%rcx
add    $0x1,%ebx
mov    %al,(%r14,%rcx,1)
movsbl 0xf(%rsp),%edi
call   1030 <putchar@plt>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     11b0 <read+0x80>
movb   $0x0,0xf(%rsp)
cmp    %ebp,%ebx
jl     1160 <read+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @read(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store volatile i8 0, i8* %6
%7 = load i32, i32* %4
%8 = call i32 @_read(i8* %6, i32 %7)
br label %9
9: ; preds = %48, %17, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %49
13: ; preds = %9
%14 = load volatile i8, i8* %6
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
br label %9
18: ; preds = %13
%19 = load volatile i8, i8* %6
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 8
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i8*, i8** %3
%27 = load i32, i32* %5
%28 = add nsw i32 %27, -1
store i32 %28, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 0, i8* %30
%31 = call i32 (...) @_remove_char()
br label %32
32: ; preds = %25, %22
br label %42
33: ; preds = %18
%34 = load volatile i8, i8* %6
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 %34, i8* %39
%40 = load volatile i8, i8* %6
%41 = call i32 @putchar(i8 signext %40)
br label %42
42: ; preds = %33, %32
br label %43
43: ; preds = %42
%44 = load volatile i8, i8* %6
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %48
47: ; preds = %43
br label %49
48: ; preds = %43
store volatile i8 0, i8* %6
br label %9
49: ; preds = %47, %9
ret void
}

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