#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 getDebugChar();
int gdb_read(char *buf, int nbytes) {
  int i = 0;
  (*(volatile unsigned *)0x7f08) &= ~0x1000;
  for (i = 0; i < nbytes; i++) {
    *(buf + i) = getDebugChar();
    if ((*(buf + i) == '\n') || (*(buf + i) == '\r')) {
      (*(buf + i + 1)) = 0;
      break;
    }
  }
  (*(volatile unsigned *)0x7f08) |= 0x1000;
  return (i);
}


x86 assembly (clang, -O0):
<gdb_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x7f08,%eax
mov    (%rax),%ecx
and    $0xffffefff,%ecx
mov    $0x7f08,%eax
mov    %ecx,(%rax)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b0 <gdb_read+0xa0>
mov    $0x0,%al
call   1030 <getDebugChar@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     118b <gdb_read+0x7b>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    119d <gdb_read+0x8d>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
jmp    11b0 <gdb_read+0xa0>
jmp    11a2 <gdb_read+0x92>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1141 <gdb_read+0x31>
mov    $0x7f08,%eax
mov    (%rax),%ecx
or     $0x1000,%ecx
mov    $0x7f08,%eax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_read>:
push   %r15
push   %r14
push   %rbx
andl   $0xffffefff,0x7f08
ff ef ff ff
test   %esi,%esi
jle    1159 <gdb_read+0x49>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <getDebugChar@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xd,%al
je     1151 <gdb_read+0x41>
cmp    $0xa,%al
je     1151 <gdb_read+0x41>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <gdb_read+0x20>
mov    %r15d,%ebx
jmp    115b <gdb_read+0x4b>
movb   $0x0,0x1(%r14,%rbx,1)
jmp    115b <gdb_read+0x4b>
xor    %ebx,%ebx
orl    $0x1000,0x7f08
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @gdb_read(i8* nocapture writeonly %0, i32 %1) {
%3 = load volatile i32, i32* inttoptr (i64 32520 to i32*)
%4 = and i32 %3, -4097
store volatile i32 %4, i32* inttoptr (i64 32520 to i32*)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %20
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %17
%9 = phi i64 [ 0, %6 ], [ %18, %17 ]
%10 = tail call signext i8 (...) @getDebugChar()
%11 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 %10, i8* %11
switch i8 %10, label %17 [
i8 10, label %12
i8 13, label %12
]
12: ; preds = %8, %8
%13 = trunc i64 %9 to i32
%14 = and i64 %9, 4294967295
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 0, i8* %16
br label %20
17: ; preds = %8
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %20, label %8
20: ; preds = %17, %2, %12
%21 = phi i32 [ %13, %12 ], [ 0, %2 ], [ %1, %17 ]
%22 = load volatile i32, i32* inttoptr (i64 32520 to i32*)
%23 = or i32 %22, 4096
store volatile i32 %23, i32* inttoptr (i64 32520 to i32*)
ret i32 %21
}