#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*/ SEEK_CUR;
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int getc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *loadstr(FILE *file) {
  int n, size = 0;
  char *buf;
  do {
    n = getc(file);
    size++;
  } while ((n != (int)'\0') && (n != EOF));
  if (n == EOF)
    return NULL;
  if ((buf = (char *)malloc(size)) == NULL)
    return NULL;
  fseek(file, -size, SEEK_CUR);
  fread(buf, 1, size, file);
  return buf;
}


x86 assembly (clang, -O0):
<loadstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
je     1189 <loadstr+0x49>
mov    -0x14(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1153 <loadstr+0x13>
mov    -0x14(%rbp),%eax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
jne    11b3 <loadstr+0x73>
movq   $0x0,-0x8(%rbp)
jmp    120a <loadstr+0xca>
mov    -0x18(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d6 <loadstr+0x96>
movq   $0x0,-0x8(%rbp)
jmp    120a <loadstr+0xca>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
sub    -0x18(%rbp),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
mov    0x2e8d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <getc@plt>
add    $0xffffffff,%ebp
mov    (%r14),%ecx
test   %eax,%eax
je     1176 <loadstr+0x36>
cmp    %ecx,%eax
jne    1160 <loadstr+0x20>
cmp    %ecx,%eax
je     11b8 <loadstr+0x78>
mov    %ebp,%r15d
neg    %r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11b8 <loadstr+0x78>
mov    %rax,%r14
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
jmp    11bb <loadstr+0x7b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@SEEK_CUR = global i32 0
define i8* @loadstr(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32* %0, i32** %3
store i32 0, i32* %5
br label %7
7: ; preds = %19, %1
%8 = load i32*, i32** %3
%9 = call i32 @getc(i32* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %5
%11 = add nsw i32 %10, 1
store i32 %11, i32* %5
br label %12
12: ; preds = %7
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %7, label %21
21: ; preds = %19
%22 = load i32, i32* %4
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i8* null, i8** %2
br label %43
26: ; preds = %21
%27 = load i32, i32* %5
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %26
store i8* null, i8** %2
br label %43
32: ; preds = %26
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = sub nsw i32 0, %34
%36 = load i32, i32* @SEEK_CUR
%37 = call i32 @fseek(i32* %33, i32 %35, i32 %36)
%38 = load i8*, i8** %6
%39 = load i32, i32* %5
%40 = load i32*, i32** %3
%41 = call i32 @fread(i8* %38, i32 1, i32 %39, i32* %40)
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %43
43: ; preds = %32, %31, %25
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
@EOF = global i32 0
@SEEK_CUR = global i32 0
define i8* @loadstr(i32* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i32 [ 0, %1 ], [ %5, %2 ]
%4 = tail call i32 @getc(i32* %0)
%5 = add nuw nsw i32 %3, 1
%6 = icmp ne i32 %4, 0
%7 = load i32, i32* @EOF
%8 = icmp ne i32 %4, %7
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %2, label %10
10: ; preds = %2
%11 = icmp eq i32 %4, %7
br i1 %11, label %21, label %12
12: ; preds = %10
%13 = tail call i64 @malloc(i32 %5)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = xor i32 %3, -1
%18 = load i32, i32* @SEEK_CUR
%19 = tail call i32 @fseek(i32* %0, i32 %17, i32 %18)
%20 = tail call i32 @fread(i8* %14, i32 1, i32 %5, i32* %0)
br label %21
21: ; preds = %12, %10, %16
%22 = phi i8* [ %14, %16 ], [ null, %10 ], [ null, %12 ]
ret i8* %22
}