#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 errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int iterate_over_input(int fh_in, void (*callback)(char *line)) {
  char buf[0xfff];
  int r_size, err;
  int line_size;
  int buf_end;
  int line_nr;
  char *p;
  memset(buf, 0, 0xfff);
  r_size = 0;
  buf_end = 0;
  line_nr = 0;
  while (1 == 1) {
    r_size = read(fh_in, &buf[buf_end], 0xfff - buf_end);
    if (r_size == -1) {
      err = errno;
      fprintf(stderr, "failed reading input:%s\n", strerror(err));
      return 1;
    }
    buf_end = buf_end + r_size;
    if (buf_end == 0) {
      return 0;
    }
    p = buf;
    while (p < buf + buf_end) {
      if (*p == '\n' || *p == '\0') {
        line_nr++;
        *p = '\0';
        break;
      }
      p++;
    }
    if (p == buf + buf_end) {
      fprintf(stderr, "Line(%d) does not fit the buffer %d\n", line_nr, 0xfff);
      return 1;
    }
    line_size = p - buf;
    callback(buf);
    memmove(buf, p + 1, 0xfff - line_size);
    buf_end -= (line_size + 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<iterate_over_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0xfff,%edx
call   1030 <memset@plt>
movl   $0x0,-0x1014(%rbp)
movl   $0x0,-0x1020(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x8(%rbp),%edi
movslq -0x1020(%rbp),%rax
lea    -0x1010(%rbp),%rsi
add    %rax,%rsi
mov    $0xfff,%edx
sub    -0x1020(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    1216 <iterate_over_input+0xc6>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1018(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    -0x1018(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x1034(%rbp),%edi
mov    %rax,%rdx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137c <iterate_over_input+0x22c>
mov    -0x1020(%rbp),%eax
add    -0x1014(%rbp),%eax
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x1020(%rbp)
jne    1241 <iterate_over_input+0xf1>
movl   $0x0,-0x4(%rbp)
jmp    137c <iterate_over_input+0x22c>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
lea    -0x1010(%rbp),%rcx
movslq -0x1020(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    12cb <iterate_over_input+0x17b>
mov    -0x1030(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1296 <iterate_over_input+0x146>
mov    -0x1030(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12b4 <iterate_over_input+0x164>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
mov    -0x1030(%rbp),%rax
movb   $0x0,(%rax)
jmp    12cb <iterate_over_input+0x17b>
mov    -0x1030(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1030(%rbp)
jmp    124f <iterate_over_input+0xff>
mov    -0x1030(%rbp),%rax
lea    -0x1010(%rbp),%rcx
movslq -0x1020(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jne    131a <iterate_over_input+0x1ca>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    -0x1024(%rbp),%edx
lea    0xd17(%rip),%rsi
mov    $0xfff,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137c <iterate_over_input+0x22c>
mov    -0x1030(%rbp),%rax
lea    -0x1010(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x101c(%rbp)
mov    -0x10(%rbp),%rax
lea    -0x1010(%rbp),%rdi
call   *%rax
lea    -0x1010(%rbp),%rdi
mov    -0x1030(%rbp),%rsi
add    $0x1,%rsi
mov    $0xfff,%edx
sub    -0x101c(%rbp),%edx
call   1060 <memmove@plt>
mov    -0x101c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x1020(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1020(%rbp)
jmp    1193 <iterate_over_input+0x43>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iterate_over_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %edi,%r12d
lea    0x10(%rsp),%r13
movl   $0x0,0xc(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xfff,%edx
call   1030 <memset@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0xfff,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1239 <iterate_over_input+0xe9>
xor    %ebp,%ebp
xchg   %ax,%ax
add    %eax,%ebp
je     1265 <iterate_over_input+0x115>
movslq %ebp,%rcx
lea    (%rsp,%rcx,1),%rax
add    $0x10,%rax
mov    %r13,%rbx
test   %ecx,%ecx
jle    11e8 <iterate_over_input+0x98>
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%ecx
cmp    $0xa,%cl
je     11e0 <iterate_over_input+0x90>
test   %cl,%cl
je     11e0 <iterate_over_input+0x90>
add    $0x1,%rbx
cmp    %rax,%rbx
jb     11c0 <iterate_over_input+0x70>
jmp    11e8 <iterate_over_input+0x98>
nopw   0x0(%rax,%rax,1)
addl   $0x1,0xc(%rsp)
movb   $0x0,(%rbx)
cmp    %rax,%rbx
je     1269 <iterate_over_input+0x119>
mov    %ebx,%r14d
sub    %r13d,%r14d
mov    %r13,%rdi
call   *%r15
add    $0x1,%rbx
mov    $0xfff,%edx
sub    %r14d,%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1060 <memmove@plt>
not    %r14d
add    %r14d,%ebp
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    $0xfff,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11a0 <iterate_over_input+0x50>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xda9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1289 <iterate_over_input+0x139>
xor    %eax,%eax
jmp    128e <iterate_over_input+0x13e>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    0xc(%rsp),%edx
mov    $0xfff,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"failed reading input:%s\0A\00"
@.str.1 = [37 x i8] c"Line(%d) does not fit the buffer %d\0A\00"
define i32 @iterate_over_input(i32 %0, void (i8*)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca void (i8*)*
%6 = alloca [4095 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %4
store void (i8*)* %1, void (i8*)** %5
%13 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 4095)
store i32 0, i32* %7
store i32 0, i32* %10
store i32 0, i32* %11
br label %15
15: ; preds = %2, %75
%16 = load i32, i32* %4
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 %18
%20 = load i32, i32* %10
%21 = sub nsw i32 4095, %20
%22 = call i32 @read(i32 %16, i8* %19, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = load i32, i32* @errno
store i32 %26, i32* %8
%27 = load i32, i32* @stderr
%28 = load i32, i32* %8
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %94
31: ; preds = %15
%32 = load i32, i32* %10
%33 = load i32, i32* %7
%34 = add nsw i32 %32, %33
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 0, i32* %3
br label %94
38: ; preds = %31
%39 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
store i8* %39, i8** %12
br label %40
40: ; preds = %61, %38
%41 = load i8*, i8** %12
%42 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = icmp ult i8* %41, %45
br i1 %46, label %47, label %64
47: ; preds = %40
%48 = load i8*, i8** %12
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %57, label %52
52: ; preds = %47
%53 = load i8*, i8** %12
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %52, %47
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
%60 = load i8*, i8** %12
store i8 0, i8* %60
br label %64
61: ; preds = %52
%62 = load i8*, i8** %12
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %12
br label %40
64: ; preds = %57, %40
%65 = load i8*, i8** %12
%66 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%67 = load i32, i32* %10
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = icmp eq i8* %65, %69
br i1 %70, label %71, label %75
71: ; preds = %64
%72 = load i32, i32* @stderr
%73 = load i32, i32* %11
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %73, i32 4095)
store i32 1, i32* %3
br label %94
75: ; preds = %64
%76 = load i8*, i8** %12
%77 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%78 = ptrtoint i8* %76 to i64
%79 = ptrtoint i8* %77 to i64
%80 = sub i64 %78, %79
%81 = trunc i64 %80 to i32
store i32 %81, i32* %9
%82 = load void (i8*)*, void (i8*)** %5
%83 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%84 = getelementptr inbounds [4095 x i8], [4095 x i8]* %6, i64 0, i64 0
%85 = load i8*, i8** %12
%86 = getelementptr inbounds i8, i8* %85, i64 1
%87 = load i32, i32* %9
%88 = sub nsw i32 4095, %87
%89 = call i32 @memmove(i8* %84, i8* %86, i32 %88)
%90 = load i32, i32* %9
%91 = add nsw i32 %90, 1
%92 = load i32, i32* %10
%93 = sub nsw i32 %92, %91
store i32 %93, i32* %10
br label %15
94: ; preds = %71, %37, %25
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@errno = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"failed reading input:%s\0A\00"
@.str.1 = [37 x i8] c"Line(%d) does not fit the buffer %d\0A\00"
define i32 @iterate_over_input(i32 %0, void (i8*)* nocapture %1) {
%3 = alloca [4095 x i8]
%4 = getelementptr inbounds [4095 x i8], [4095 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 4095)
%6 = call i32 @read(i32 %0, i8* %4, i32 4095)
%7 = icmp eq i32 %6, -1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = ptrtoint [4095 x i8]* %3 to i64
br label %15
10: ; preds = %40, %2
%11 = load i32, i32* @errno
%12 = load i32, i32* @stderr
%13 = call i8* @strerror(i32 %11)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %54
15: ; preds = %8, %40
%16 = phi i32 [ %6, %8 ], [ %52, %40 ]
%17 = phi i32 [ 0, %8 ], [ %35, %40 ]
%18 = phi i32 [ 0, %8 ], [ %48, %40 ]
%19 = add nsw i32 %16, %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %54, label %21
21: ; preds = %15
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds [4095 x i8], [4095 x i8]* %3, i64 0, i64 %22
%24 = icmp sgt i32 %19, 0
br i1 %24, label %25, label %33
25: ; preds = %21, %30
%26 = phi i8* [ %31, %30 ], [ %4, %21 ]
%27 = load i8, i8* %26
switch i8 %27, label %30 [
i8 10, label %28
i8 0, label %28
]
28: ; preds = %25, %25
%29 = add nsw i32 %17, 1
store i8 0, i8* %26
br label %33
30: ; preds = %25
%31 = getelementptr inbounds i8, i8* %26, i64 1
%32 = icmp ult i8* %31, %23
br i1 %32, label %25, label %33
33: ; preds = %30, %21, %28
%34 = phi i8* [ %26, %28 ], [ %4, %21 ], [ %31, %30 ]
%35 = phi i32 [ %29, %28 ], [ %17, %21 ], [ %17, %30 ]
%36 = icmp eq i8* %34, %23
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 4095)
br label %54
40: ; preds = %33
%41 = ptrtoint i8* %34 to i64
%42 = sub i64 %41, %9
%43 = trunc i64 %42 to i32
%44 = getelementptr inbounds i8, i8* %34, i64 1
%45 = sub nsw i32 4095, %43
%46 = call i32 @memmove(i8* %4, i8* %44, i32 %45)
%47 = xor i32 %43, -1
%48 = add i32 %19, %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [4095 x i8], [4095 x i8]* %3, i64 0, i64 %49
%51 = sub nsw i32 4095, %48
%52 = call i32 @read(i32 %0, i8* %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %10, label %15
54: ; preds = %15, %37, %10
%55 = phi i32 [ 1, %10 ], [ 1, %37 ], [ 0, %15 ]
ret i32 %55
}