#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*/ exit(int);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
int fgets_whole_line(char **bufptr, size_t *bufsize, FILE *stream) {
  size_t bufcur = 0;
  if (*bufptr == NULL) {
    *bufptr = malloc(512);
    *bufsize = 512;
    if (*bufptr == NULL) {
      fprintf(stderr, "grepcidr: Fatal error: out of memory!\n");
      exit(2);
    }
  }
  while (fgets((*bufptr) + bufcur, (*bufsize) - bufcur, stream)) {
    size_t len = bufcur + strlen(*bufptr + bufcur);
    if (len < 1)
      return 1;
    if ((bufptr[0][len - 1] == '\n') || feof(stream))
      return 1;
    *bufsize *= 2;
    *bufptr = realloc(*bufptr, *bufsize);
    if (*bufptr == NULL) {
      fprintf(stderr, "grepcidr: Fatal error: out of memory!\n");
      exit(2);
    }
    bufcur = len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fgets_whole_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11eb <fgets_whole_line+0x7b>
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x200,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e6 <fgets_whole_line+0x76>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
jmp    11eb <fgets_whole_line+0x7b>
jmp    11f0 <fgets_whole_line+0x80>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
sub    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12f6 <fgets_whole_line+0x186>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jae    1256 <fgets_whole_line+0xe6>
movl   $0x1,-0x4(%rbp)
jmp    12fd <fgets_whole_line+0x18d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1285 <fgets_whole_line+0x115>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1291 <fgets_whole_line+0x121>
movl   $0x1,-0x4(%rbp)
jmp    12fd <fgets_whole_line+0x18d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1080 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e9 <fgets_whole_line+0x179>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xd26(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11f0 <fgets_whole_line+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_whole_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rdi),%rax
test   %rax,%rax
jne    11a6 <fgets_whole_line+0x36>
mov    $0x200,%edi
call   1070 <malloc@plt>
mov    %rax,(%r12)
movq   $0x200,(%r15)
test   %rax,%rax
je     120a <fgets_whole_line+0x9a>
xor    %ebx,%ebx
mov    $0x1,%r13d
xchg   %ax,%ax
add    %rbx,%rax
mov    (%r15),%rsi
sub    %rbx,%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1229 <fgets_whole_line+0xb9>
mov    (%r12),%rbp
lea    (%rbx,%rbp,1),%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
je     122c <fgets_whole_line+0xbc>
cmpb   $0xa,-0x1(%rbx,%rbp,1)
je     122c <fgets_whole_line+0xbc>
mov    %r14,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    122c <fgets_whole_line+0xbc>
mov    (%r15),%rsi
add    %rsi,%rsi
mov    %rsi,(%r15)
mov    (%r12),%rdi
call   1080 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    11b0 <fgets_whole_line+0x40>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"grepcidr: Fatal error: out of memory!\0A\00"
define i32 @fgets_whole_line(i8** %0, i64* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i32* %2, i32** %7
store i64 0, i64* %8
%10 = load i8**, i8*** %5
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %25
13: ; preds = %3
%14 = call i8* @malloc(i32 512)
%15 = load i8**, i8*** %5
store i8* %14, i8** %15
%16 = load i64*, i64** %6
store i64 512, i64* %16
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %13
br label %25
25: ; preds = %24, %3
br label %26
26: ; preds = %81, %25
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
%29 = load i64, i64* %8
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i64*, i64** %6
%32 = load i64, i64* %31
%33 = load i64, i64* %8
%34 = sub i64 %32, %33
%35 = load i32*, i32** %7
%36 = call i64 @fgets(i8* %30, i64 %34, i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %83
38: ; preds = %26
%39 = load i64, i64* %8
%40 = load i8**, i8*** %5
%41 = load i8*, i8** %40
%42 = load i64, i64* %8
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = call i64 @strlen(i8* %43)
%45 = add i64 %39, %44
store i64 %45, i64* %9
%46 = load i64, i64* %9
%47 = icmp ult i64 %46, 1
br i1 %47, label %48, label %49
48: ; preds = %38
store i32 1, i32* %4
br label %84
49: ; preds = %38
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 0
%52 = load i8*, i8** %51
%53 = load i64, i64* %9
%54 = sub i64 %53, 1
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 10
br i1 %58, label %63, label %59
59: ; preds = %49
%60 = load i32*, i32** %7
%61 = call i64 @feof(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %59, %49
store i32 1, i32* %4
br label %84
64: ; preds = %59
%65 = load i64*, i64** %6
%66 = load i64, i64* %65
%67 = mul i64 %66, 2
store i64 %67, i64* %65
%68 = load i8**, i8*** %5
%69 = load i8*, i8** %68
%70 = load i64*, i64** %6
%71 = load i64, i64* %70
%72 = call i8* @realloc(i8* %69, i64 %71)
%73 = load i8**, i8*** %5
store i8* %72, i8** %73
%74 = load i8**, i8*** %5
%75 = load i8*, i8** %74
%76 = icmp eq i8* %75, null
br i1 %76, label %77, label %81
77: ; preds = %64
%78 = load i32, i32* @stderr
%79 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%80 = call i32 @exit(i32 2)
unreachable
81: ; preds = %64
%82 = load i64, i64* %9
store i64 %82, i64* %8
br label %26
83: ; preds = %26
store i32 0, i32* %4
br label %84
84: ; preds = %83, %63, %48
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"grepcidr: Fatal error: out of memory!\0A\00"
define i32 @fgets_whole_line(i8** nocapture %0, i64* nocapture %1, i32* nocapture %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %8, %3
%7 = phi i8* [ %4, %3 ], [ %9, %8 ]
br label %15
8: ; preds = %3
%9 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
store i8* %9, i8** %0
store i64 512, i64* %1
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %6
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 2)
unreachable
15: ; preds = %6, %37
%16 = phi i8* [ %41, %37 ], [ %7, %6 ]
%17 = phi i64 [ %27, %37 ], [ 0, %6 ]
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %1
%20 = sub i64 %19, %17
%21 = tail call i64 @fgets(i8* %18, i64 %20, i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %47, label %23
23: ; preds = %15
%24 = load i8*, i8** %0
%25 = getelementptr inbounds i8, i8* %24, i64 %17
%26 = tail call i64 @strlen(i8* dereferenceable(1) %25)
%27 = add i64 %26, %17
%28 = icmp eq i64 %27, 0
br i1 %28, label %47, label %29
29: ; preds = %23
%30 = add i64 %27, -1
%31 = getelementptr inbounds i8, i8* %24, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
br i1 %33, label %47, label %34
34: ; preds = %29
%35 = tail call i64 @feof(i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = load i64, i64* %1
%39 = shl i64 %38, 1
store i64 %39, i64* %1
%40 = load i8*, i8** %0
%41 = tail call i8* @realloc(i8* %40, i64 %39)
store i8* %41, i8** %0
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %15
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = tail call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%46 = tail call i32 @exit(i32 2)
unreachable
47: ; preds = %29, %34, %23, %15
%48 = phi i32 [ 0, %15 ], [ 1, %23 ], [ 1, %34 ], [ 1, %29 ]
ret i32 %48
}