#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ xmalloc(int);
scalar_t__ xrealloc(void *, int);
char *fsafegets(FILE *fileInput, unsigned int chunkSize) {
  char *inputBuffer, *wholeBuffer = NULL, *newline = NULL;
  int currentBufferLength;
  if (chunkSize > 0) {
    inputBuffer = (char *)xmalloc(chunkSize);
    currentBufferLength = 0;
    do {
      if (fgets(inputBuffer, chunkSize, fileInput) == NULL) {
        if (wholeBuffer) {
          free(wholeBuffer);
          wholeBuffer = NULL;
        }
        break;
      }
      currentBufferLength += chunkSize;
      if (wholeBuffer) {
        wholeBuffer =
            (char *)xrealloc((void *)wholeBuffer, currentBufferLength);
        strcat(wholeBuffer, inputBuffer);
      } else {
        wholeBuffer = (char *)xmalloc(currentBufferLength);
        strcpy(wholeBuffer, inputBuffer);
      }
      newline = strchr(wholeBuffer, '\n');
    } while ((newline == NULL) && (!feof(fileInput)));
    if ((wholeBuffer) && (newline != NULL))
      *newline = '\0';
    free(inputBuffer);
  } else
    wholeBuffer = NULL;
  return wholeBuffer;
}


x86 assembly (clang, -O0):
<fsafegets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
jbe    12b2 <fsafegets+0x132>
mov    -0xc(%rbp),%edi
call   1070 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11f7 <fsafegets+0x77>
cmpq   $0x0,-0x20(%rbp)
je     11f2 <fsafegets+0x72>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    1287 <fsafegets+0x107>
mov    -0xc(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     122d <fsafegets+0xad>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <xrealloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <strcat@plt>
jmp    1246 <fsafegets+0xc6>
mov    -0x2c(%rbp),%edi
call   1070 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x2d(%rbp)
jne    127c <fsafegets+0xfc>
mov    -0x8(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11bc <fsafegets+0x3c>
cmpq   $0x0,-0x20(%rbp)
je     12a4 <fsafegets+0x124>
cmpq   $0x0,-0x28(%rbp)
je     12a4 <fsafegets+0x124>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    12ba <fsafegets+0x13a>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsafegets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
je     1228 <fsafegets+0xa8>
mov    %esi,%r12d
mov    %rdi,%r15
mov    %esi,%edi
call   1070 <xmalloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %r12d,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     122c <fsafegets+0xac>
test   %rbx,%rbx
je     11f0 <fsafegets+0x70>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <xrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
jmp    1205 <fsafegets+0x85>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   1070 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    123b <fsafegets+0xbb>
mov    %r15,%rdi
call   1090 <feof@plt>
add    %r12d,%ebp
test   %eax,%eax
je     11b0 <fsafegets+0x30>
jmp    1247 <fsafegets+0xc7>
xor    %ebx,%ebx
jmp    124f <fsafegets+0xcf>
test   %rbx,%rbx
je     1245 <fsafegets+0xc5>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1245 <fsafegets+0xc5>
test   %rbx,%rbx
je     1245 <fsafegets+0xc5>
movb   $0x0,(%rax)
jmp    1247 <fsafegets+0xc7>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @fsafegets(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i8* null, i8** %6
store i8* null, i8** %7
%9 = load i32, i32* %4
%10 = icmp ugt i32 %9, 0
br i1 %10, label %11, label %73
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call i64 @xmalloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
store i32 0, i32* %8
br label %15
15: ; preds = %60, %11
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = load i32*, i32** %3
%19 = call i32* @fgets(i8* %16, i32 %17, i32* %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
store i8* null, i8** %6
br label %27
27: ; preds = %24, %21
br label %62
28: ; preds = %15
%29 = load i32, i32* %4
%30 = load i32, i32* %8
%31 = add i32 %30, %29
store i32 %31, i32* %8
%32 = load i8*, i8** %6
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %42
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = call i64 @xrealloc(i8* %35, i32 %36)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %6
%39 = load i8*, i8** %6
%40 = load i8*, i8** %5
%41 = call i32 @strcat(i8* %39, i8* %40)
br label %49
42: ; preds = %28
%43 = load i32, i32* %8
%44 = call i64 @xmalloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = load i8*, i8** %5
%48 = call i32 @strcpy(i8* %46, i8* %47)
br label %49
49: ; preds = %42, %34
%50 = load i8*, i8** %6
%51 = call i8* @strchr(i8* %50, i8 signext 10)
store i8* %51, i8** %7
br label %52
52: ; preds = %49
%53 = load i8*, i8** %7
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i32*, i32** %3
%57 = call i32 @feof(i32* %56)
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
br label %60
60: ; preds = %55, %52
%61 = phi i1 [ false, %52 ], [ %59, %55 ]
br i1 %61, label %15, label %62
62: ; preds = %60, %27
%63 = load i8*, i8** %6
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %70
65: ; preds = %62
%66 = load i8*, i8** %7
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = load i8*, i8** %7
store i8 0, i8* %69
br label %70
70: ; preds = %68, %65, %62
%71 = load i8*, i8** %5
%72 = call i32 @free(i8* %71)
br label %74
73: ; preds = %2
store i8* null, i8** %6
br label %74
74: ; preds = %73, %70
%75 = load i8*, i8** %6
ret i8* %75
}

LLVM IR (-O2):
define i8* @fsafegets(i32* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %40, label %4
4: ; preds = %2
%5 = tail call i64 @xmalloc(i32 %1)
%6 = inttoptr i64 %5 to i8*
br label %7
7: ; preds = %31, %4
%8 = phi i8* [ null, %4 ], [ %28, %31 ]
%9 = phi i32 [ 0, %4 ], [ %17, %31 ]
%10 = tail call i32* @fgets(i8* %6, i32 %1, i32* %0)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = icmp eq i8* %8, null
br i1 %13, label %37, label %14
14: ; preds = %12
%15 = tail call i32 @free(i8* %8)
br label %37
16: ; preds = %7
%17 = add i32 %9, %1
%18 = icmp eq i8* %8, null
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i64 @xrealloc(i8* %8, i32 %17)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @strcat(i8* %21, i8* %6)
br label %27
23: ; preds = %16
%24 = tail call i64 @xmalloc(i32 %17)
%25 = inttoptr i64 %24 to i8*
%26 = tail call i32 @strcpy(i8* %25, i8* %6)
br label %27
27: ; preds = %23, %19
%28 = phi i8* [ %21, %19 ], [ %25, %23 ]
%29 = tail call i8* @strchr(i8* dereferenceable(1) %28, i8 signext 10)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = tail call i32 @feof(i32* %0)
%33 = icmp eq i32 %32, 0
br i1 %33, label %7, label %37
34: ; preds = %27
%35 = icmp eq i8* %28, null
br i1 %35, label %37, label %36
36: ; preds = %34
store i8 0, i8* %29
br label %37
37: ; preds = %31, %12, %14, %36, %34
%38 = phi i8* [ %28, %36 ], [ null, %34 ], [ null, %12 ], [ null, %14 ], [ %28, %31 ]
%39 = tail call i32 @free(i8* %6)
br label %40
40: ; preds = %2, %37
%41 = phi i8* [ %38, %37 ], [ null, %2 ]
ret i8* %41
}