#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 */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ lprintf(int, char *);
char *malloc(int);
char *realloc(char *, int);
scalar_t__ strlen(char *);
char *fgetline(FILE *fp) {
  static int bufsz = 0;
  static char *bufp = NULL;
  int bufidx;
  char *p;
  if (bufp == NULL) {
    if ((bufp = malloc(bufsz = 1024)) == NULL) {
      lprintf(1, "fgetline: cannot allocate memory");
      return NULL;
    }
  }
  bufidx = 0;
  while ((1 == 1)) {
    p = fgets(&bufp[bufidx], bufsz - bufidx, fp);
    if (p == NULL) {
      if (bufidx == 0) {
        free(bufp);
        bufp = NULL;
        bufsz = 0;
        return NULL;
      } else
        break;
    }
    bufidx += strlen(&bufp[bufidx]);
    if (bufidx > 0 && bufp[bufidx - 1] == '\n')
      bufp[--bufidx] = 0;
    if (bufidx == bufsz - 1) {
      lprintf(5, "realloc line");
      if ((p = realloc(bufp, bufsz += 1024)) == NULL) {
        free(bufp);
        bufp = NULL;
        bufsz = 0;
        lprintf(1, "fgetline: cannot realloc");
        return NULL;
      }
      bufp = p;
      continue;
    }
    if (bufidx > 0 && bufp[bufidx - 1] == '\\') {
      bufidx--;
      continue;
    }
    if (bufidx > 0) {
      char *sp = bufp;
      while (isspace(*sp))
        sp++;
      if (*sp == '#') {
        bufidx = 0;
        continue;
      }
    }
    break;
  }
  return bufp;
}


x86 assembly (clang, -O0):
<fgetline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2ee4(%rip)
jne    11d2 <fgetline+0x62>
movl   $0x400,0x2ecc(%rip)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,0x2ec3(%rip)
cmp    $0x0,%rax
jne    11cd <fgetline+0x5d>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
call   1090 <lprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e5 <fgetline+0x275>
jmp    11d2 <fgetline+0x62>
movl   $0x0,-0x14(%rbp)
mov    0x2e88(%rip),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    0x2e73(%rip),%esi
sub    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1245 <fgetline+0xd5>
cmpl   $0x0,-0x14(%rbp)
jne    1240 <fgetline+0xd0>
mov    0x2e4f(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2e3f(%rip)
movl   $0x0,0x2e2d(%rip)
movq   $0x0,-0x8(%rbp)
jmp    13e5 <fgetline+0x275>
jmp    13da <fgetline+0x26a>
mov    0x2e1c(%rip),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12a3 <fgetline+0x133>
mov    0x2df2(%rip),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12a3 <fgetline+0x133>
mov    0x2dd5(%rip),%rax
mov    -0x14(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
mov    0x2db4(%rip),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1343 <fgetline+0x1d3>
mov    $0x5,%edi
lea    0xd5e(%rip),%rsi
call   1090 <lprintf@plt>
mov    0x2d99(%rip),%rdi
mov    0x2d8b(%rip),%esi
add    $0x400,%esi
mov    %esi,0x2d7f(%rip)
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1333 <fgetline+0x1c3>
mov    0x2d6d(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2d5d(%rip)
movl   $0x0,0x2d4b(%rip)
mov    $0x1,%edi
lea    0xd0d(%rip),%rsi
call   1090 <lprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13e5 <fgetline+0x275>
mov    -0x20(%rbp),%rax
mov    %rax,0x2d2a(%rip)
jmp    11d9 <fgetline+0x69>
cmpl   $0x0,-0x14(%rbp)
jle    1378 <fgetline+0x208>
mov    0x2d14(%rip),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
jne    1378 <fgetline+0x208>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11d9 <fgetline+0x69>
cmpl   $0x0,-0x14(%rbp)
jle    13d5 <fgetline+0x265>
mov    0x2cdf(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     13b4 <fgetline+0x244>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    138d <fgetline+0x21d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
jne    13d0 <fgetline+0x260>
movl   $0x0,-0x14(%rbp)
jmp    11d9 <fgetline+0x69>
jmp    13d5 <fgetline+0x265>
jmp    13da <fgetline+0x26a>
mov    0x2c87(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2ee3(%rip),%rdi
test   %rdi,%rdi
je     1192 <fgetline+0x22>
mov    0x2ed0(%rip),%ebx
jmp    11be <fgetline+0x4e>
movl   $0x400,0x2ec4(%rip)
mov    $0x400,%ebx
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2eb3(%rip)
test   %rax,%rax
je     12fe <fgetline+0x18e>
mov    %ebx,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12db <fgetline+0x16b>
xor    %r13d,%r13d
mov    $0x400,%ebp
xor    %r12d,%r12d
jmp    120d <fgetline+0x9d>
xchg   %ax,%ax
xor    %r12d,%r12d
mov    0x2e7e(%rip),%rbx
movslq %r12d,%r13
add    %r13,%rbx
mov    0x2e6a(%rip),%esi
sub    %r13d,%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12cd <fgetline+0x15d>
mov    %r12d,%r14d
mov    0x2e51(%rip),%rbx
add    %rbx,%r13
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
add    %r14d,%r12d
test   %r12d,%r12d
jle    123f <fgetline+0xcf>
lea    -0x1(%r12),%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    123f <fgetline+0xcf>
movb   $0x0,(%rbx,%rax,1)
mov    %eax,%r12d
mov    0x2e1b(%rip),%eax
add    $0xffffffff,%eax
cmp    %eax,%r12d
jne    1290 <fgetline+0x120>
mov    $0x5,%edi
lea    0xdc8(%rip),%rsi
call   1090 <lprintf@plt>
mov    0x2e03(%rip),%rdi
mov    0x2df5(%rip),%esi
add    %ebp,%esi
mov    %esi,0x2ded(%rip)
call   1080 <realloc@plt>
test   %rax,%rax
je     1307 <fgetline+0x197>
mov    %rax,%rbx
mov    %rax,0x2ddd(%rip)
jmp    11ea <fgetline+0x7a>
test   %r12d,%r12d
jle    12d2 <fgetline+0x162>
add    $0xffffffff,%r12d
cmpb   $0x5c,(%rbx,%r12,1)
je     11ea <fgetline+0x7a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%rbx),%edi
call   1050 <isspace@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    12b0 <fgetline+0x140>
cmpb   $0x23,-0x1(%rbx)
je     11e0 <fgetline+0x70>
jmp    12d2 <fgetline+0x162>
test   %r12d,%r12d
je     12db <fgetline+0x16b>
mov    0x2d8f(%rip),%rax
jmp    133b <fgetline+0x1cb>
mov    0x2d86(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2d76(%rip)
movl   $0x0,0x2d64(%rip)
jmp    1339 <fgetline+0x1c9>
lea    0xcfb(%rip),%rsi
jmp    132f <fgetline+0x1bf>
mov    0x2d5a(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2d4a(%rip)
movl   $0x0,0x2d38(%rip)
lea    0xcff(%rip),%rsi
mov    $0x1,%edi
call   1090 <lprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@fgetline.bufsz = internal global i32 0
@fgetline.bufp = internal global i8* null
@.str = [33 x i8] c"fgetline: cannot allocate memory\00"
@.str.1 = [13 x i8] c"realloc line\00"
@.str.2 = [25 x i8] c"fgetline: cannot realloc\00"
define i8* @fgetline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32* %0, i32** %3
%7 = load i8*, i8** @fgetline.bufp
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
store i32 1024, i32* @fgetline.bufsz
%10 = call i8* @malloc(i32 1024)
store i8* %10, i8** @fgetline.bufp
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @lprintf(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %118
14: ; preds = %9
br label %15
15: ; preds = %14, %1
store i32 0, i32* %4
br label %16
16: ; preds = %15, %78, %92, %113
%17 = load i8*, i8** @fgetline.bufp
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* @fgetline.bufsz
%22 = load i32, i32* %4
%23 = sub nsw i32 %21, %22
%24 = load i32*, i32** %3
%25 = call i8* @fgets(i8* %20, i32 %23, i32* %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %16
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i8*, i8** @fgetline.bufp
%33 = call i32 @free(i8* %32)
store i8* null, i8** @fgetline.bufp
store i32 0, i32* @fgetline.bufsz
store i8* null, i8** %2
br label %118
34: ; preds = %28
br label %116
35: ; preds = %16
%36 = load i8*, i8** @fgetline.bufp
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = call i64 @strlen(i8* %39)
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %62
47: ; preds = %35
%48 = load i8*, i8** @fgetline.bufp
%49 = load i32, i32* %4
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %62
56: ; preds = %47
%57 = load i8*, i8** @fgetline.bufp
%58 = load i32, i32* %4
%59 = add nsw i32 %58, -1
store i32 %59, i32* %4
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
br label %62
62: ; preds = %56, %47, %35
%63 = load i32, i32* %4
%64 = load i32, i32* @fgetline.bufsz
%65 = sub nsw i32 %64, 1
%66 = icmp eq i32 %63, %65
br i1 %66, label %67, label %80
67: ; preds = %62
%68 = call i32 @lprintf(i32 5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%69 = load i8*, i8** @fgetline.bufp
%70 = load i32, i32* @fgetline.bufsz
%71 = add nsw i32 %70, 1024
store i32 %71, i32* @fgetline.bufsz
%72 = call i8* @realloc(i8* %69, i32 %71)
store i8* %72, i8** %5
%73 = icmp eq i8* %72, null
br i1 %73, label %74, label %78
74: ; preds = %67
%75 = load i8*, i8** @fgetline.bufp
%76 = call i32 @free(i8* %75)
store i8* null, i8** @fgetline.bufp
store i32 0, i32* @fgetline.bufsz
%77 = call i32 @lprintf(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %118
78: ; preds = %67
%79 = load i8*, i8** %5
store i8* %79, i8** @fgetline.bufp
br label %16
80: ; preds = %62
%81 = load i32, i32* %4
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %95
83: ; preds = %80
%84 = load i8*, i8** @fgetline.bufp
%85 = load i32, i32* %4
%86 = sub nsw i32 %85, 1
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %84, i64 %87
%89 = load i8, i8* %88
%90 = sext i8 %89 to i32
%91 = icmp eq i32 %90, 92
br i1 %91, label %92, label %95
92: ; preds = %83
%93 = load i32, i32* %4
%94 = add nsw i32 %93, -1
store i32 %94, i32* %4
br label %16
95: ; preds = %83, %80
%96 = load i32, i32* %4
%97 = icmp sgt i32 %96, 0
br i1 %97, label %98, label %115
98: ; preds = %95
%99 = load i8*, i8** @fgetline.bufp
store i8* %99, i8** %6
br label %100
100: ; preds = %105, %98
%101 = load i8*, i8** %6
%102 = load i8, i8* %101
%103 = call i64 @isspace(i8 signext %102)
%104 = icmp ne i64 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %100
%106 = load i8*, i8** %6
%107 = getelementptr inbounds i8, i8* %106, i32 1
store i8* %107, i8** %6
br label %100
108: ; preds = %100
%109 = load i8*, i8** %6
%110 = load i8, i8* %109
%111 = sext i8 %110 to i32
%112 = icmp eq i32 %111, 35
br i1 %112, label %113, label %114
113: ; preds = %108
store i32 0, i32* %4
br label %16
114: ; preds = %108
br label %115
115: ; preds = %114, %95
br label %116
116: ; preds = %115, %34
%117 = load i8*, i8** @fgetline.bufp
store i8* %117, i8** %2
br label %118
118: ; preds = %116, %74, %31, %12
%119 = load i8*, i8** %2
ret i8* %119
}

LLVM IR (-O2):
@fgetline.bufsz = internal unnamed_addr global i32 0
@fgetline.bufp = internal unnamed_addr global i8* null
@.str = [33 x i8] c"fgetline: cannot allocate memory\00"
@.str.1 = [13 x i8] c"realloc line\00"
@.str.2 = [25 x i8] c"fgetline: cannot realloc\00"
define i8* @fgetline(i32* nocapture %0) {
%2 = load i8*, i8** @fgetline.bufp
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = load i32, i32* @fgetline.bufsz
br label %11
6: ; preds = %1
store i32 1024, i32* @fgetline.bufsz
%7 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
store i8* %7, i8** @fgetline.bufp
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @lprintf(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %84
11: ; preds = %4, %6
%12 = phi i32 [ 1024, %6 ], [ %5, %4 ]
%13 = phi i8* [ %7, %6 ], [ %2, %4 ]
%14 = tail call i8* @fgets(i8* %13, i32 %12, i32* %0)
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %21
16: ; preds = %54
%17 = icmp eq i32 %56, 0
br i1 %17, label %18, label %82
18: ; preds = %11, %16
%19 = load i8*, i8** @fgetline.bufp
%20 = tail call i32 @free(i8* %19)
store i8* null, i8** @fgetline.bufp
store i32 0, i32* @fgetline.bufsz
br label %84
21: ; preds = %11, %54
%22 = phi i64 [ %57, %54 ], [ 0, %11 ]
%23 = phi i32 [ %56, %54 ], [ 0, %11 ]
%24 = load i8*, i8** @fgetline.bufp
%25 = getelementptr inbounds i8, i8* %24, i64 %22
%26 = tail call i64 @strlen(i8* dereferenceable(1) %25)
%27 = trunc i64 %26 to i32
%28 = add i32 %23, %27
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %21
%31 = add nsw i32 %28, -1
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %24, i64 %32
%34 = load i8, i8* %33
%35 = icmp eq i8 %34, 10
br i1 %35, label %36, label %37
36: ; preds = %30
store i8 0, i8* %33
br label %37
37: ; preds = %36, %30, %21
%38 = phi i32 [ %31, %36 ], [ %28, %30 ], [ %28, %21 ]
%39 = load i32, i32* @fgetline.bufsz
%40 = add nsw i32 %39, -1
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %63
42: ; preds = %37
%43 = tail call i32 @lprintf(i32 5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%44 = load i8*, i8** @fgetline.bufp
%45 = load i32, i32* @fgetline.bufsz
%46 = add nsw i32 %45, 1024
store i32 %46, i32* @fgetline.bufsz
%47 = tail call i8* @realloc(i8* %44, i32 %46)
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = load i8*, i8** @fgetline.bufp
%51 = tail call i32 @free(i8* %50)
store i8* null, i8** @fgetline.bufp
store i32 0, i32* @fgetline.bufsz
%52 = tail call i32 @lprintf(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %84
53: ; preds = %42
store i8* %47, i8** @fgetline.bufp
br label %54
54: ; preds = %80, %53, %65
%55 = phi i8* [ %47, %53 ], [ %81, %80 ], [ %24, %65 ]
%56 = phi i32 [ %38, %53 ], [ 0, %80 ], [ %66, %65 ]
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i32, i32* @fgetline.bufsz
%60 = sub nsw i32 %59, %56
%61 = tail call i8* @fgets(i8* %58, i32 %60, i32* %0)
%62 = icmp eq i8* %61, null
br i1 %62, label %16, label %21
63: ; preds = %37
%64 = icmp sgt i32 %38, 0
br i1 %64, label %65, label %82
65: ; preds = %63
%66 = add nsw i32 %38, -1
%67 = zext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %24, i64 %67
%69 = load i8, i8* %68
%70 = icmp eq i8 %69, 92
br i1 %70, label %54, label %71
71: ; preds = %65, %71
%72 = phi i8* [ %76, %71 ], [ %24, %65 ]
%73 = load i8, i8* %72
%74 = tail call i64 @isspace(i8 signext %73)
%75 = icmp eq i64 %74, 0
%76 = getelementptr inbounds i8, i8* %72, i64 1
br i1 %75, label %77, label %71
77: ; preds = %71
%78 = load i8, i8* %72
%79 = icmp eq i8 %78, 35
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = load i8*, i8** @fgetline.bufp
br label %54
82: ; preds = %77, %63, %16
%83 = load i8*, i8** @fgetline.bufp
br label %84
84: ; preds = %82, %49, %18, %9
%85 = phi i8* [ null, %9 ], [ null, %18 ], [ %83, %82 ], [ null, %49 ]
ret i8* %85
}