#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 read(int, char *, int);
char in_char(int fd) {
  static char buf[1024];
  static int bytes_read = 0;
  static int buf_it = 0;
  if (buf_it == bytes_read) {
    bytes_read = read(fd, buf, 1024);
    buf_it = 0;
  }
  if (bytes_read < 1)
    return (-1);
  return (buf[buf_it++]);
}


x86 assembly (clang, -O0):
<in_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x3323(%rip),%eax
cmp    0x3319(%rip),%eax
jne    1151 <in_char+0x41>
mov    -0x8(%rbp),%edi
lea    0x2f09(%rip),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,0x32f9(%rip)
movl   $0x0,0x32f3(%rip)
cmpl   $0x1,0x32e8(%rip)
jge    1167 <in_char+0x57>
movb   $0xff,-0x1(%rbp)
jmp    1188 <in_char+0x78>
mov    0x32d7(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x32cc(%rip)
movslq %eax,%rcx
lea    0x2ebe(%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):
<in_char>:
push   %rax
mov    0x332d(%rip),%ecx
mov    0x3323(%rip),%eax
cmp    %eax,%ecx
jne    1144 <in_char+0x34>
lea    0x2f18(%rip),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,0x3308(%rip)
movl   $0x0,0x3302(%rip)
xor    %ecx,%ecx
test   %eax,%eax
jle    1160 <in_char+0x50>
lea    0x1(%rcx),%eax
mov    %eax,0x32f3(%rip)
movslq %ecx,%rax
lea    0x2ee5(%rip),%rcx
mov    (%rax,%rcx,1),%al
pop    %rcx
ret
mov    $0xff,%al
pop    %rcx
ret

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

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