#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 */
typedef int uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
uint64_t read_segment(char *buf, FILE *fd) {
  uint64_t read_sz = (64 * 1024 * 1024) - (8 * 1024);
  uint64_t real_sz = (uint64_t)fread(buf, 1, read_sz, fd);
  if (real_sz == 0)
    return (0);
  if (real_sz != read_sz) {
    if (buf[real_sz - 1] != '\n') {
      real_sz++;
      buf[real_sz - 1] = '\n';
    }
    return (real_sz);
  }
  for (uint64_t extra = 0; extra < (8 * 1024); extra++) {
    if (fread(buf + real_sz, 1, 1, fd) == 0)
      break;
    real_sz++;
    if (buf[real_sz - 1] == '\n')
      break;
  }
  return (real_sz);
}


x86 assembly (clang, -O0):
<read_segment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x3ffe000,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1155 <read_segment+0x45>
movl   $0x0,-0x4(%rbp)
jmp    121f <read_segment+0x10f>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11a0 <read_segment+0x90>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1195 <read_segment+0x85>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121f <read_segment+0x10f>
movl   $0x0,-0x24(%rbp)
cmpl   $0x2000,-0x24(%rbp)
jge    1219 <read_segment+0x109>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x0,%rax
jne    11de <read_segment+0xce>
jmp    1219 <read_segment+0x109>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1206 <read_segment+0xf6>
jmp    1219 <read_segment+0x109>
jmp    120b <read_segment+0xfb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a7 <read_segment+0x97>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_segment>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1,%esi
mov    $0x3ffe000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%r12d
test   %eax,%eax
je     11ca <read_segment+0xba>
cmp    $0x3ffe000,%r12d
jne    119c <read_segment+0x8c>
mov    $0x3ffe000,%r13d
mov    $0x3ffe001,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r14,%r13,1),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11c7 <read_segment+0xb7>
cmpb   $0xa,(%rbx)
je     11ca <read_segment+0xba>
add    $0x1,%r13
add    $0x1,%r12d
cmp    $0x4000000,%r13
jne    1160 <read_segment+0x50>
mov    $0x4000000,%r12d
jmp    11ca <read_segment+0xba>
shl    $0x20,%rax
movabs $0xffffffff00000000,%rcx
ff ff ff
add    %rax,%rcx
sar    $0x20,%rcx
cmpb   $0xa,(%r14,%rcx,1)
je     11ca <read_segment+0xba>
add    $0x1,%r12d
sar    $0x20,%rax
movb   $0xa,(%r14,%rax,1)
jmp    11ca <read_segment+0xba>
mov    %r13d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @read_segment(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 67100672, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = load i32*, i32** %5
%12 = call i64 @fread(i8* %9, i32 1, i32 %10, i32* %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %71
17: ; preds = %2
%18 = load i32, i32* %7
%19 = load i32, i32* %6
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %40
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 10
br i1 %29, label %30, label %38
30: ; preds = %21
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
%33 = load i8*, i8** %4
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 10, i8* %37
br label %38
38: ; preds = %30, %21
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %71
40: ; preds = %17
store i32 0, i32* %8
br label %41
41: ; preds = %66, %40
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 8192
br i1 %43, label %44, label %69
44: ; preds = %41
%45 = load i8*, i8** %4
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32*, i32** %5
%50 = call i64 @fread(i8* %48, i32 1, i32 1, i32* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %44
br label %69
53: ; preds = %44
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
%56 = load i8*, i8** %4
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 10
br i1 %63, label %64, label %65
64: ; preds = %53
br label %69
65: ; preds = %53
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
br label %41
69: ; preds = %64, %52, %41
%70 = load i32, i32* %7
store i32 %70, i32* %3
br label %71
71: ; preds = %69, %38, %16
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
define i32 @read_segment(i8* nocapture %0, i32* nocapture %1) {
%3 = tail call i64 @fread(i8* %0, i32 1, i32 67100672, i32* %1)
%4 = trunc i64 %3 to i32
switch i32 %4, label %5 [
i32 0, label %31
i32 67100672, label %18
]
5: ; preds = %2
%6 = shl i64 %3, 32
%7 = add i64 %6, -4294967296
%8 = ashr exact i64 %7, 32
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
br i1 %11, label %31, label %12
12: ; preds = %5
%13 = add nsw i32 %4, 1
%14 = ashr exact i64 %6, 32
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 10, i8* %15
br label %31
16: ; preds = %23
%17 = icmp eq i64 %24, 67108864
br i1 %17, label %31, label %18
18: ; preds = %2, %16
%19 = phi i64 [ %24, %16 ], [ 67100672, %2 ]
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = tail call i64 @fread(i8* %20, i32 1, i32 1, i32* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = add nuw nsw i64 %19, 1
%25 = load i8, i8* %20
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %16
27: ; preds = %23
%28 = trunc i64 %24 to i32
br label %31
29: ; preds = %18
%30 = trunc i64 %19 to i32
br label %31
31: ; preds = %16, %27, %29, %5, %12, %2
%32 = phi i32 [ %4, %2 ], [ %13, %12 ], [ %4, %5 ], [ %28, %27 ], [ %30, %29 ], [ 67108864, %16 ]
ret i32 %32
}