#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 */
unsigned int sys_call(int, unsigned int, int, int /*<<< orphan*/,
                      int /*<<< orphan*/);
unsigned int tty_gets_irq(char *buf, unsigned int bufsize) {
  unsigned int ret;
  unsigned char byte;
  unsigned int index = 0;
  while (index < (bufsize - 1)) {
    do {
      ret = sys_call(0x0A, (unsigned int)(&byte), 1, 0, 0);
    } while (ret != 1);
    if (byte == 0x0A)
      break;
    else if ((byte == 0x7F) && (index > 0))
      index--;
    else {
      buf[index] = byte;
      index++;
    }
  }
  buf[index] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<tty_gets_irq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    11bb <tty_gets_irq+0xab>
jmp    113c <tty_gets_irq+0x2c>
lea    -0x11(%rbp),%rax
mov    %eax,%esi
mov    $0xa,%edi
mov    $0x1,%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1030 <sys_call@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    113c <tty_gets_irq+0x2c>
movzbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1176 <tty_gets_irq+0x66>
jmp    11bb <tty_gets_irq+0xab>
movzbl -0x11(%rbp),%eax
cmp    $0x7f,%eax
jne    119b <tty_gets_irq+0x8b>
cmpl   $0x0,-0x18(%rbp)
jbe    119b <tty_gets_irq+0x8b>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11b1 <tty_gets_irq+0xa1>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b6 <tty_gets_irq+0xa6>
jmp    1126 <tty_gets_irq+0x16>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_gets_irq>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
add    $0xffffffff,%r15d
je     1173 <tty_gets_irq+0x63>
lea    0x7(%rsp),%rbx
xor    %ebp,%ebp
jmp    1140 <tty_gets_irq+0x30>
mov    %ebp,%ecx
mov    %al,(%r14,%rcx,1)
mov    $0x1,%ecx
add    %ecx,%ebp
cmp    %r15d,%ebp
jae    1175 <tty_gets_irq+0x65>
xchg   %ax,%ax
mov    $0xa,%edi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <sys_call@plt>
cmp    $0x1,%eax
jne    1140 <tty_gets_irq+0x30>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1175 <tty_gets_irq+0x65>
cmp    $0x7f,%al
jne    112c <tty_gets_irq+0x1c>
mov    $0xffffffff,%ecx
test   %ebp,%ebp
jne    1137 <tty_gets_irq+0x27>
jmp    112c <tty_gets_irq+0x1c>
xor    %ebp,%ebp
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @tty_gets_irq(i8* nocapture writeonly %0, i32 %1) {
%3 = alloca i8
%4 = add i32 %1, -1
%5 = icmp eq i32 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = ptrtoint i8* %3 to i64
%8 = trunc i64 %7 to i32
br label %9
9: ; preds = %6, %24
%10 = phi i32 [ 0, %6 ], [ %26, %24 ]
br label %11
11: ; preds = %9, %11
%12 = call i32 @sys_call(i32 10, i32 %8, i32 1, i32 0, i32 0)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %11
14: ; preds = %11
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, 10
br i1 %16, label %28, label %17
17: ; preds = %14
%18 = icmp eq i8 %15, 127
%19 = icmp ne i32 %10, 0
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = zext i32 %10 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 %15, i8* %23
br label %24
24: ; preds = %17, %21
%25 = phi i32 [ 1, %21 ], [ -1, %17 ]
%26 = add i32 %10, %25
%27 = icmp ult i32 %26, %4
br i1 %27, label %9, label %28
28: ; preds = %24, %14, %2
%29 = phi i32 [ 0, %2 ], [ %10, %14 ], [ %26, %24 ]
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
store i8 0, i8* %31
ret i32 0
}