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

/* Variables and functions */
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
char *find_lf(char *, char *);
int /*<<< orphan*/ memmove(char *, char *, int);
int read(int /*<<< orphan*/, char *, int);
const char *fgets2(FILE *stream) {
  static char buffer[(256 * 1024) + 68];
  static char *end = buffer;
  static char *line = buffer;
  char *next;
  int ret;
  next = line;
  while (1) {
    next = find_lf(next, end);
    if (next < end) {
      const char *start = line;
      *next = '\0';
      line = next + 1;
      return start;
    }
    if (line > buffer) {
      if (end != line)
        memmove(buffer, line, end - line);
      end = buffer + (end - line);
      next = end;
      line = buffer;
    } else {
      if (end == buffer + (256 * 1024))
        return NULL;
    }
    ret = read(fileno(stream), end, buffer + (256 * 1024) - end);
    if (ret <= 0) {
      if (end == line)
        return NULL;
      *end = '\0';
      end = line;
      return line;
    }
    end += ret;
    *end = '\n';
  }
}


x86 assembly (clang, -O0):
<fgets2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ef5(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ede(%rip),%rsi
call   1060 <find_lf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    0x2eca(%rip),%rax
jae    11aa <fgets2+0x6a>
mov    0x2ec5(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,0x2eab(%rip)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <fgets2+0x1d0>
lea    0x2eaf(%rip),%rax
cmp    %rax,0x2e90(%rip)
jbe    1238 <fgets2+0xf8>
mov    0x2e7b(%rip),%rax
cmp    0x2e7c(%rip),%rax
je     11f8 <fgets2+0xb8>
mov    0x2e6f(%rip),%rsi
mov    0x2e60(%rip),%rax
mov    0x2e61(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2e6d(%rip),%rdi
call   1050 <memmove@plt>
mov    0x2e41(%rip),%rcx
mov    0x2e42(%rip),%rax
sub    %rax,%rcx
lea    0x2e50(%rip),%rax
add    %rcx,%rax
mov    %rax,0x2e26(%rip)
mov    0x2e1f(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0x2e34(%rip),%rax
mov    %rax,0x2e15(%rip)
jmp    1264 <fgets2+0x124>
lea    0x2e21(%rip),%rax
add    $0x40000,%rax
cmp    %rax,0x2df4(%rip)
jne    125f <fgets2+0x11f>
movq   $0x0,-0x8(%rbp)
jmp    1310 <fgets2+0x1d0>
jmp    1264 <fgets2+0x124>
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2dca(%rip),%rsi
mov    0x2dc3(%rip),%rcx
lea    0x2ddc(%rip),%rax
add    $0x40000,%rax
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     12ea <fgets2+0x1aa>
mov    0x2d98(%rip),%rax
cmp    0x2d99(%rip),%rax
jne    12c2 <fgets2+0x182>
movq   $0x0,-0x8(%rbp)
jmp    1310 <fgets2+0x1d0>
mov    0x2d77(%rip),%rax
movb   $0x0,(%rax)
mov    0x2d75(%rip),%rax
mov    %rax,0x2d66(%rip)
mov    0x2d67(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1310 <fgets2+0x1d0>
mov    -0x1c(%rbp),%ecx
mov    0x2d4c(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2d3f(%rip)
mov    0x2d38(%rip),%rax
movb   $0xa,(%rax)
jmp    1157 <fgets2+0x17>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2ef5(%rip),%rdi
mov    0x2ee6(%rip),%rsi
call   1060 <find_lf@plt>
mov    %rax,%rbx
mov    0x2ed7(%rip),%rdx
cmp    %rdx,%rax
jb     1239 <fgets2+0xf9>
lea    0x2ee7(%rip),%r12
xor    %r14d,%r14d
lea    0x42edd(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2eb1(%rip),%rsi
cmp    %r12,%rsi
jbe    11c0 <fgets2+0x80>
cmp    %rsi,%rdx
je     11ca <fgets2+0x8a>
sub    %esi,%edx
mov    %r12,%rdi
call   1050 <memmove@plt>
mov    0x2e8e(%rip),%rbx
mov    0x2e8f(%rip),%rdx
jmp    11cd <fgets2+0x8d>
nopl   0x0(%rax,%rax,1)
cmp    %r13,%rdx
jne    11e1 <fgets2+0xa1>
jmp    124e <fgets2+0x10e>
mov    %rdx,%rbx
sub    %rdx,%rbx
add    %r12,%rbx
mov    %rbx,0x2e66(%rip)
mov    %r12,0x2e67(%rip)
mov    %r15,%rdi
call   1040 <fileno@plt>
mov    0x2e50(%rip),%rsi
mov    %r12d,%edx
sub    %esi,%edx
add    $0x40000,%edx
mov    %eax,%edi
call   1030 <read@plt>
mov    0x2e37(%rip),%rcx
test   %eax,%eax
jle    125b <fgets2+0x11b>
mov    %eax,%eax
lea    (%rcx,%rax,1),%rsi
mov    %rsi,0x2e26(%rip)
movb   $0xa,(%rcx,%rax,1)
mov    %rbx,%rdi
call   1060 <find_lf@plt>
mov    %rax,%rbx
mov    0x2e10(%rip),%rdx
cmp    %rdx,%rax
jae    1190 <fgets2+0x50>
mov    0x2e08(%rip),%r14
movb   $0x0,(%rbx)
add    $0x1,%rbx
mov    %rbx,0x2dfa(%rip)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2de6(%rip),%r14
cmp    %r14,%rcx
je     1273 <fgets2+0x133>
movb   $0x0,(%rcx)
mov    %r14,0x2dcf(%rip)
jmp    124e <fgets2+0x10e>
xor    %r14d,%r14d
jmp    124e <fgets2+0x10e>

LLVM IR (-O0):
@fgets2.buffer = internal global [262212 x i8] zeroinitializer
@fgets2.end = internal global i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i32 0, i32 0)
@fgets2.line = internal global i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i32 0, i32 0)
define i8* @fgets2(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i32* %0, i32** %3
%7 = load i8*, i8** @fgets2.line
store i8* %7, i8** %4
br label %8
8: ; preds = %1, %70
%9 = load i8*, i8** %4
%10 = load i8*, i8** @fgets2.end
%11 = call i8* @find_lf(i8* %9, i8* %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = load i8*, i8** @fgets2.end
%14 = icmp ult i8* %12, %13
br i1 %14, label %15, label %21
15: ; preds = %8
%16 = load i8*, i8** @fgets2.line
store i8* %16, i8** %6
%17 = load i8*, i8** %4
store i8 0, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %19, i8** @fgets2.line
%20 = load i8*, i8** %6
store i8* %20, i8** %2
br label %76
21: ; preds = %8
%22 = load i8*, i8** @fgets2.line
%23 = icmp ugt i8* %22, getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0)
br i1 %23, label %24, label %45
24: ; preds = %21
%25 = load i8*, i8** @fgets2.end
%26 = load i8*, i8** @fgets2.line
%27 = icmp ne i8* %25, %26
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i8*, i8** @fgets2.line
%30 = load i8*, i8** @fgets2.end
%31 = load i8*, i8** @fgets2.line
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = trunc i64 %34 to i32
%36 = call i32 @memmove(i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0), i8* %29, i32 %35)
br label %37
37: ; preds = %28, %24
%38 = load i8*, i8** @fgets2.end
%39 = load i8*, i8** @fgets2.line
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = getelementptr inbounds i8, i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0), i64 %42
store i8* %43, i8** @fgets2.end
%44 = load i8*, i8** @fgets2.end
store i8* %44, i8** %4
store i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0), i8** @fgets2.line
br label %50
45: ; preds = %21
%46 = load i8*, i8** @fgets2.end
%47 = icmp eq i8* %46, getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 262144)
br i1 %47, label %48, label %49
48: ; preds = %45
store i8* null, i8** %2
br label %76
49: ; preds = %45
br label %50
50: ; preds = %49, %37
%51 = load i32*, i32** %3
%52 = call i32 @fileno(i32* %51)
%53 = load i8*, i8** @fgets2.end
%54 = load i8*, i8** @fgets2.end
%55 = ptrtoint i8* %54 to i64
%56 = sub i64 ptrtoint (i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 262144) to i64), %55
%57 = trunc i64 %56 to i32
%58 = call i32 @read(i32 %52, i8* %53, i32 %57)
store i32 %58, i32* %5
%59 = load i32, i32* %5
%60 = icmp sle i32 %59, 0
br i1 %60, label %61, label %70
61: ; preds = %50
%62 = load i8*, i8** @fgets2.end
%63 = load i8*, i8** @fgets2.line
%64 = icmp eq i8* %62, %63
br i1 %64, label %65, label %66
65: ; preds = %61
store i8* null, i8** %2
br label %76
66: ; preds = %61
%67 = load i8*, i8** @fgets2.end
store i8 0, i8* %67
%68 = load i8*, i8** @fgets2.line
store i8* %68, i8** @fgets2.end
%69 = load i8*, i8** @fgets2.line
store i8* %69, i8** %2
br label %76
70: ; preds = %50
%71 = load i32, i32* %5
%72 = load i8*, i8** @fgets2.end
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8, i8* %72, i64 %73
store i8* %74, i8** @fgets2.end
%75 = load i8*, i8** @fgets2.end
store i8 10, i8* %75
br label %8
76: ; preds = %66, %65, %48, %15
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@fgets2.buffer = internal global [262212 x i8] zeroinitializer
@fgets2.end = internal unnamed_addr global i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0)
@fgets2.line = internal unnamed_addr global i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0)
define i8* @fgets2(i32* nocapture %0) {
%2 = load i8*, i8** @fgets2.line
%3 = load i8*, i8** @fgets2.end
%4 = tail call i8* @find_lf(i8* %2, i8* %3)
%5 = load i8*, i8** @fgets2.end
%6 = icmp ult i8* %4, %5
br i1 %6, label %7, label %11
7: ; preds = %49, %1
%8 = phi i8* [ %4, %1 ], [ %52, %49 ]
%9 = load i8*, i8** @fgets2.line
store i8 0, i8* %8
%10 = getelementptr inbounds i8, i8* %8, i64 1
store i8* %10, i8** @fgets2.line
br label %55
11: ; preds = %1, %49
%12 = phi i8* [ %53, %49 ], [ %5, %1 ]
%13 = phi i8* [ %52, %49 ], [ %4, %1 ]
%14 = load i8*, i8** @fgets2.line
%15 = icmp ugt i8* %14, getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0)
br i1 %15, label %16, label %33
16: ; preds = %11
%17 = icmp eq i8* %12, %14
br i1 %17, label %26, label %18
18: ; preds = %16
%19 = ptrtoint i8* %12 to i64
%20 = ptrtoint i8* %14 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = tail call i32 @memmove(i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0), i8* %14, i32 %22)
%24 = load i8*, i8** @fgets2.end
%25 = load i8*, i8** @fgets2.line
br label %26
26: ; preds = %18, %16
%27 = phi i8* [ %25, %18 ], [ %12, %16 ]
%28 = phi i8* [ %24, %18 ], [ %12, %16 ]
%29 = ptrtoint i8* %28 to i64
%30 = ptrtoint i8* %27 to i64
%31 = sub i64 %29, %30
%32 = getelementptr inbounds [262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 %31
store i8* %32, i8** @fgets2.end
store i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 0), i8** @fgets2.line
br label %35
33: ; preds = %11
%34 = icmp eq i8* %12, getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 262144)
br i1 %34, label %55, label %35
35: ; preds = %33, %26
%36 = phi i8* [ %32, %26 ], [ %13, %33 ]
%37 = tail call i32 @fileno(i32* %0)
%38 = load i8*, i8** @fgets2.end
%39 = ptrtoint i8* %38 to i64
%40 = trunc i64 %39 to i32
%41 = sub i32 ptrtoint (i8* getelementptr inbounds ([262212 x i8], [262212 x i8]* @fgets2.buffer, i64 0, i64 262144) to i32), %40
%42 = tail call i32 @read(i32 %37, i8* %38, i32 %41)
%43 = icmp slt i32 %42, 1
%44 = load i8*, i8** @fgets2.end
br i1 %43, label %45, label %49
45: ; preds = %35
%46 = load i8*, i8** @fgets2.line
%47 = icmp eq i8* %44, %46
br i1 %47, label %55, label %48
48: ; preds = %45
store i8 0, i8* %44
store i8* %46, i8** @fgets2.end
br label %55
49: ; preds = %35
%50 = zext i32 %42 to i64
%51 = getelementptr inbounds i8, i8* %44, i64 %50
store i8* %51, i8** @fgets2.end
store i8 10, i8* %51
%52 = tail call i8* @find_lf(i8* %36, i8* %51)
%53 = load i8*, i8** @fgets2.end
%54 = icmp ult i8* %52, %53
br i1 %54, label %7, label %11
55: ; preds = %33, %45, %48, %7
%56 = phi i8* [ %9, %7 ], [ %46, %48 ], [ null, %45 ], [ null, %33 ]
ret i8* %56
}