#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 EOF;
int /*<<< orphan*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ xrealloc(char *, int);
char *safe_fgets(FILE *f) {
  static char *buffer = 0;
  static int bufsize = 0;
  int bufptr;
  int ch;
  bufptr = 0;
  while (1) {
    ch = fgetc(f);
    if (ch == EOF) {
      if (bufptr == 0)
        return 0;
      buffer[bufptr] = 0;
      return buffer;
    }
    if (ch == '\r')
      continue;
    if (bufsize <= bufptr) {
      bufsize += 10;
      buffer = (char *)xrealloc(buffer, bufsize + 1);
      if (buffer == 0) {
        fprintf(stderr, "Out of memory! bufsize %d\n", bufsize);
        exit(1);
      }
    }
    buffer[bufptr++] = ch;
    if (ch == '\n') {
      buffer[bufptr - 1] = 0;
      return buffer;
    }
  }
}


x86 assembly (clang, -O0):
<safe_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%eax
jne    11a7 <safe_fgets+0x67>
cmpl   $0x0,-0x14(%rbp)
jne    1188 <safe_fgets+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1278 <safe_fgets+0x138>
mov    0x2eb9(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2eaa(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1278 <safe_fgets+0x138>
cmpl   $0xd,-0x18(%rbp)
jne    11b6 <safe_fgets+0x76>
jmp    1153 <safe_fgets+0x13>
mov    0x2e94(%rip),%eax
cmp    -0x14(%rbp),%eax
jg     1228 <safe_fgets+0xe8>
mov    0x2e85(%rip),%eax
add    $0xa,%eax
mov    %eax,0x2e7c(%rip)
mov    0x2e6d(%rip),%rdi
mov    0x2e6f(%rip),%esi
add    $0x1,%esi
call   1030 <xrealloc@plt>
mov    %rax,0x2e58(%rip)
cmpq   $0x0,0x2e50(%rip)
jne    1223 <safe_fgets+0xe3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%edx
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1228 <safe_fgets+0xe8>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    0x2e14(%rip),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x18(%rbp)
jne    1273 <safe_fgets+0x133>
mov    0x2df2(%rip),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2dde(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1278 <safe_fgets+0x138>
jmp    1153 <safe_fgets+0x13>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ecx,%ecx
mov    0x2e95(%rip),%r15
mov    %rcx,%r14
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
je     11c3 <safe_fgets+0x83>
mov    %eax,%ebp
cmp    $0xd,%eax
je     1160 <safe_fgets+0x20>
movslq 0x2ed5(%rip),%rsi
cmp    %rsi,%r14
jge    1190 <safe_fgets+0x50>
mov    0x2ec1(%rip),%rax
jmp    11b4 <safe_fgets+0x74>
nopl   0x0(%rax)
lea    0xa(%rsi),%eax
mov    %eax,0x2eb7(%rip)
mov    0x2ea8(%rip),%rdi
add    $0xb,%esi
call   1030 <xrealloc@plt>
mov    %rax,0x2e99(%rip)
test   %rax,%rax
je     11e8 <safe_fgets+0xa8>
lea    0x1(%r14),%rcx
mov    %bpl,(%rax,%r14,1)
cmp    $0xa,%ebp
jne    1153 <safe_fgets+0x13>
jmp    11d2 <safe_fgets+0x92>
test   %r14d,%r14d
je     11db <safe_fgets+0x9b>
mov    0x2e79(%rip),%rax
mov    %r14d,%r14d
add    %rax,%r14
movb   $0x0,(%r14)
jmp    11dd <safe_fgets+0x9d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%edx
lea    0xe02(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@safe_fgets.buffer = internal global i8* null
@safe_fgets.bufsize = internal global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Out of memory! bufsize %d\0A\00"
define i8* @safe_fgets(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %4
br label %6
6: ; preds = %1, %25, %63
%7 = load i32*, i32** %3
%8 = call i32 @fgetc(i32* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %22
12: ; preds = %6
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i8* null, i8** %2
br label %64
16: ; preds = %12
%17 = load i8*, i8** @safe_fgets.buffer
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 0, i8* %20
%21 = load i8*, i8** @safe_fgets.buffer
store i8* %21, i8** %2
br label %64
22: ; preds = %6
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 13
br i1 %24, label %25, label %26
25: ; preds = %22
br label %6
26: ; preds = %22
%27 = load i32, i32* @safe_fgets.bufsize
%28 = load i32, i32* %4
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %46
30: ; preds = %26
%31 = load i32, i32* @safe_fgets.bufsize
%32 = add nsw i32 %31, 10
store i32 %32, i32* @safe_fgets.bufsize
%33 = load i8*, i8** @safe_fgets.buffer
%34 = load i32, i32* @safe_fgets.bufsize
%35 = add nsw i32 %34, 1
%36 = call i64 @xrealloc(i8* %33, i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** @safe_fgets.buffer
%38 = load i8*, i8** @safe_fgets.buffer
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = load i32, i32* @safe_fgets.bufsize
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
br label %46
46: ; preds = %45, %26
%47 = load i32, i32* %5
%48 = trunc i32 %47 to i8
%49 = load i8*, i8** @safe_fgets.buffer
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 %48, i8* %53
%54 = load i32, i32* %5
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %63
56: ; preds = %46
%57 = load i8*, i8** @safe_fgets.buffer
%58 = load i32, i32* %4
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 0, i8* %61
%62 = load i8*, i8** @safe_fgets.buffer
store i8* %62, i8** %2
br label %64
63: ; preds = %46
br label %6
64: ; preds = %56, %16, %15
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@safe_fgets.buffer = internal unnamed_addr global i8* null
@safe_fgets.bufsize = internal unnamed_addr global i32 0
@EOF = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Out of memory! bufsize %d\0A\00"
define i8* @safe_fgets(i32* nocapture %0) {
br label %2
2: ; preds = %35, %1
%3 = phi i64 [ %38, %35 ], [ 0, %1 ]
br label %4
4: ; preds = %2, %15
%5 = tail call i32 @fgetc(i32* %0)
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = trunc i64 %3 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %46, label %11
11: ; preds = %8
%12 = load i8*, i8** @safe_fgets.buffer
%13 = and i64 %3, 4294967295
%14 = getelementptr inbounds i8, i8* %12, i64 %13
br label %43
15: ; preds = %4
%16 = icmp eq i32 %5, 13
br i1 %16, label %4, label %17
17: ; preds = %15
%18 = load i32, i32* @safe_fgets.bufsize
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %3, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i8*, i8** @safe_fgets.buffer
br label %35
23: ; preds = %17
%24 = add nsw i32 %18, 10
store i32 %24, i32* @safe_fgets.bufsize
%25 = load i8*, i8** @safe_fgets.buffer
%26 = add nsw i32 %18, 11
%27 = tail call i64 @xrealloc(i8* %25, i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** @safe_fgets.buffer
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i32, i32* @safe_fgets.bufsize
%33 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %21, %23
%36 = phi i8* [ %22, %21 ], [ %28, %23 ]
%37 = trunc i32 %5 to i8
%38 = add nuw i64 %3, 1
%39 = getelementptr inbounds i8, i8* %36, i64 %3
store i8 %37, i8* %39
%40 = icmp eq i32 %5, 10
br i1 %40, label %41, label %2
41: ; preds = %35
%42 = getelementptr inbounds i8, i8* %36, i64 %3
br label %43
43: ; preds = %11, %41
%44 = phi i8* [ %42, %41 ], [ %14, %11 ]
%45 = phi i8* [ %36, %41 ], [ %12, %11 ]
store i8 0, i8* %44
br label %46
46: ; preds = %43, %8
%47 = phi i8* [ null, %8 ], [ %45, %43 ]
ret i8* %47
}