#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*/ backspace_and_erase();
long crb_getc(int /*<<< orphan*/);
int /*<<< orphan*/ crb_puts(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ output_bell();
int /*<<< orphan*/ output_crnl();
unsigned long getline(char *buf, unsigned long bufsize) {
  unsigned long len = 0;
  long c;
  while (1) {
    c = crb_getc(0);
    if (c < 0)
      continue;
    switch ((int)c) {
    case '\r':
    case '\n':
      output_crnl();
      buf[len] = 0;
      return len;
    case '\b':
    case 0x7f:
      if (len > 0) {
        backspace_and_erase();
        len--;
      } else
        output_bell();
      break;
    default:
      if (len + 1 < bufsize) {
        buf[len] = c;
        crb_puts(0, buf + len, 1);
        len++;
      } else
        output_bell();
      break;
    }
  }
}


x86 assembly (clang, -O0):
<getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
xor    %edi,%edi
call   1070 <crb_getc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1183 <getline+0x33>
jmp    1168 <getline+0x18>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x8,%eax
je     11e7 <getline+0x97>
jmp    1197 <getline+0x47>
mov    -0x24(%rbp),%eax
sub    $0xa,%eax
je     11ca <getline+0x7a>
jmp    11a8 <getline+0x58>
mov    -0x24(%rbp),%eax
sub    $0xd,%eax
je     11ca <getline+0x7a>
jmp    11b9 <getline+0x69>
mov    -0x24(%rbp),%eax
sub    $0x7f,%eax
je     11e7 <getline+0x97>
jmp    1216 <getline+0xc6>
mov    $0x0,%al
call   1040 <output_crnl@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x18(%rbp)
jbe    120a <getline+0xba>
mov    $0x0,%al
call   1050 <backspace_and_erase@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1211 <getline+0xc1>
mov    $0x0,%al
call   1030 <output_bell@plt>
jmp    126a <getline+0x11a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
cmp    -0x10(%rbp),%rax
jae    125e <getline+0x10e>
mov    -0x20(%rbp),%rax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rsi
add    -0x18(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1060 <crb_puts@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1265 <getline+0x115>
mov    $0x0,%al
call   1030 <output_bell@plt>
jmp    126a <getline+0x11a>
jmp    1168 <getline+0x18>

x86 assembly (clang, -O2):
<getline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
xor    %r15d,%r15d
lea    0x1(%r15),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %edi,%edi
call   1070 <crb_getc@plt>
test   %rax,%rax
js     1170 <getline+0x20>
cmp    $0xc,%eax
jg     118d <getline+0x3d>
cmp    $0x8,%eax
je     11b0 <getline+0x60>
cmp    $0xa,%eax
jne    1197 <getline+0x47>
jmp    11fa <getline+0xaa>
cmp    $0x7f,%eax
je     11b0 <getline+0x60>
cmp    $0xd,%eax
je     11fa <getline+0xaa>
cmp    %r12,%rbx
jb     11d0 <getline+0x80>
xor    %eax,%eax
call   1030 <output_bell@plt>
jmp    1170 <getline+0x20>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
test   %r15,%r15
je     11f0 <getline+0xa0>
call   1050 <backspace_and_erase@plt>
add    $0xffffffffffffffff,%r15
jmp    1161 <getline+0x11>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%r15,1),%rsi
mov    %al,(%r14,%r15,1)
xor    %edi,%edi
mov    $0x1,%edx
call   1060 <crb_puts@plt>
mov    %rbx,%r15
jmp    1161 <getline+0x11>
nopl   0x0(%rax)
call   1030 <output_bell@plt>
jmp    115e <getline+0xe>
xor    %eax,%eax
call   1040 <output_crnl@plt>
movb   $0x0,(%r14,%r15,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @getline(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %5
br label %7
7: ; preds = %2, %11, %51
%8 = call i64 @crb_getc(i32 0)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
br label %7
12: ; preds = %7
%13 = load i64, i64* %6
%14 = trunc i64 %13 to i32
switch i32 %14, label %31 [
i32 13, label %15
i32 10, label %15
i32 8, label %21
i32 127, label %21
]
15: ; preds = %12, %12
%16 = call i32 (...) @output_crnl()
%17 = load i8*, i8** %3
%18 = load i64, i64* %5
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 0, i8* %19
%20 = load i64, i64* %5
ret i64 %20
21: ; preds = %12, %12
%22 = load i64, i64* %5
%23 = icmp ugt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 (...) @backspace_and_erase()
%26 = load i64, i64* %5
%27 = add i64 %26, -1
store i64 %27, i64* %5
br label %30
28: ; preds = %21
%29 = call i32 (...) @output_bell()
br label %30
30: ; preds = %28, %24
br label %51
31: ; preds = %12
%32 = load i64, i64* %5
%33 = add i64 %32, 1
%34 = load i64, i64* %4
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %31
%37 = load i64, i64* %6
%38 = trunc i64 %37 to i8
%39 = load i8*, i8** %3
%40 = load i64, i64* %5
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 %38, i8* %41
%42 = load i8*, i8** %3
%43 = load i64, i64* %5
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = call i32 @crb_puts(i32 0, i8* %44, i32 1)
%46 = load i64, i64* %5
%47 = add i64 %46, 1
store i64 %47, i64* %5
br label %50
48: ; preds = %31
%49 = call i32 (...) @output_bell()
br label %50
50: ; preds = %48, %36
br label %51
51: ; preds = %50, %30
br label %7
}

LLVM IR (-O2):
define i64 @getline(i8* %0, i64 %1) {
br label %3
3: ; preds = %28, %2
%4 = phi i64 [ 0, %2 ], [ %29, %28 ]
%5 = add i64 %4, 1
%6 = icmp ult i64 %5, %1
br label %7
7: ; preds = %3, %30
br label %8
8: ; preds = %7, %8
%9 = tail call i64 @crb_getc(i32 0)
%10 = icmp slt i64 %9, 0
br i1 %10, label %8, label %11
11: ; preds = %8
%12 = trunc i64 %9 to i32
switch i32 %12, label %23 [
i32 13, label %13
i32 10, label %13
i32 8, label %16
i32 127, label %16
]
13: ; preds = %11, %11
%14 = tail call i32 (...) @output_crnl()
%15 = getelementptr inbounds i8, i8* %0, i64 %4
store i8 0, i8* %15
ret i64 %4
16: ; preds = %11, %11
%17 = icmp eq i64 %4, 0
br i1 %17, label %21, label %18
18: ; preds = %16
%19 = tail call i32 (...) @backspace_and_erase()
%20 = add i64 %4, -1
br label %28
21: ; preds = %16
%22 = tail call i32 (...) @output_bell()
br label %28
23: ; preds = %11
br i1 %6, label %24, label %30
24: ; preds = %23
%25 = trunc i64 %9 to i8
%26 = getelementptr inbounds i8, i8* %0, i64 %4
store i8 %25, i8* %26
%27 = tail call i32 @crb_puts(i32 0, i8* %26, i32 1)
br label %28
28: ; preds = %24, %18, %21
%29 = phi i64 [ 0, %21 ], [ %20, %18 ], [ %5, %24 ]
br label %3
30: ; preds = %23
%31 = tail call i32 (...) @output_bell()
br label %7
}