#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*/ buffer_clean(char *, int, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
char get_char(int fd) {
  static char buffer[4096 + 1] = {0};
  static int i = 0;
  if (buffer[i] == '\0') {
    i = 0;
    buffer_clean(buffer, 4096 + 1, 0);
    if (read(fd, buffer, 4096) <= 0)
      return (-1);
  }
  return (buffer[i++]);
}


x86 assembly (clang, -O0):
<get_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movslq 0x3f12(%rip),%rcx
lea    0x2f07(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    118f <get_char+0x6f>
movl   $0x0,0x3ef4(%rip)
lea    0x2ee9(%rip),%rdi
mov    $0x1001,%esi
xor    %edx,%edx
call   1040 <buffer_clean@plt>
mov    -0x8(%rbp),%edi
lea    0x2ed3(%rip),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jg     118a <get_char+0x6a>
movb   $0xff,-0x1(%rbp)
jmp    11b0 <get_char+0x90>
jmp    118f <get_char+0x6f>
mov    0x3eaf(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x3ea4(%rip)
movslq %eax,%rcx
lea    0x2e96(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_char>:
push   %r14
push   %rbx
push   %rax
movslq 0x3f19(%rip),%rcx
lea    0x2f0e(%rip),%rax
mov    (%rcx,%rax,1),%al
test   %al,%al
jne    117a <get_char+0x5a>
mov    %edi,%ebx
movl   $0x0,0x3eff(%rip)
lea    0x2ef4(%rip),%r14
mov    %r14,%rdi
mov    $0x1001,%esi
xor    %edx,%edx
call   1040 <buffer_clean@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    118b <get_char+0x6b>
movslq 0x3ece(%rip),%rcx
mov    (%rcx,%r14,1),%al
add    $0x1,%ecx
mov    %ecx,0x3ec1(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    $0xff,%al
jmp    1183 <get_char+0x63>

LLVM IR (-O0):
@get_char.buffer = internal global [4097 x i8] zeroinitializer
@get_char.i = internal global i32 0
define signext i8 @get_char(i32 %0) {
%2 = alloca i8
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* @get_char.i
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds [4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 %5
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %1
store i32 0, i32* @get_char.i
%11 = call i32 @buffer_clean(i8* getelementptr inbounds ([4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 0), i32 4097, i32 0)
%12 = load i32, i32* %3
%13 = call i64 @read(i32 %12, i8* getelementptr inbounds ([4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 0), i32 4096)
%14 = icmp sle i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
store i8 -1, i8* %2
br label %23
16: ; preds = %10
br label %17
17: ; preds = %16, %1
%18 = load i32, i32* @get_char.i
%19 = add nsw i32 %18, 1
store i32 %19, i32* @get_char.i
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds [4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %2
br label %23
23: ; preds = %17, %15
%24 = load i8, i8* %2
ret i8 %24
}

LLVM IR (-O2):
@get_char.buffer = internal global [4097 x i8] zeroinitializer
@get_char.i = internal unnamed_addr global i32 0
define signext i8 @get_char(i32 %0) {
%2 = load i32, i32* @get_char.i
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds [4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 %3
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %1
store i32 0, i32* @get_char.i
%8 = tail call i32 @buffer_clean(i8* getelementptr inbounds ([4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 0), i32 4097, i32 0)
%9 = tail call i64 @read(i32 %0, i8* getelementptr inbounds ([4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 0), i32 4096)
%10 = icmp slt i64 %9, 1
br i1 %10, label %20, label %11
11: ; preds = %7
%12 = load i32, i32* @get_char.i
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [4097 x i8], [4097 x i8]* @get_char.buffer, i64 0, i64 %13
%15 = load i8, i8* %14
br label %16
16: ; preds = %11, %1
%17 = phi i8 [ %15, %11 ], [ %5, %1 ]
%18 = phi i32 [ %12, %11 ], [ %2, %1 ]
%19 = add nsw i32 %18, 1
store i32 %19, i32* @get_char.i
br label %20
20: ; preds = %7, %16
%21 = phi i8 [ %17, %16 ], [ -1, %7 ]
ret i8 %21
}