#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 */
struct string_t {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free_string(struct string_t *);
struct string_t *new_string();
int /*<<< orphan*/ string_cat_char(struct string_t *, char);
struct string_t *fread_string(FILE *file) {
  struct string_t *string = new_string();
  char cc;
  while (fread(&cc, 1, 1, file) == 1) {
    if (!cc)
      return string;
    string_cat_char(string, cc);
  }
  free_string(string);
  return NULL;
}


x86 assembly (clang, -O0):
<fread_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <new_string@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x19(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    119d <fread_string+0x5d>
cmpb   $0x0,-0x19(%rbp)
jne    118b <fread_string+0x4b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ae <fread_string+0x6e>
mov    -0x18(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   1050 <string_cat_char@plt>
jmp    1157 <fread_string+0x17>
mov    -0x18(%rbp),%rdi
call   1060 <free_string@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_string>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <new_string@plt>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11ae <fread_string+0x6e>
lea    0xf(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movzbl 0xf(%rsp),%eax
test   %al,%al
je     11b8 <fread_string+0x78>
movsbl %al,%esi
mov    %rbx,%rdi
call   1050 <string_cat_char@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1180 <fread_string+0x40>
mov    %rbx,%rdi
call   1060 <free_string@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.string_t = type { i32 }
define %struct.string_t* @fread_string(i32* %0) {
%2 = alloca %struct.string_t*
%3 = alloca i32*
%4 = alloca %struct.string_t*
%5 = alloca i8
store i32* %0, i32** %3
%6 = call %struct.string_t* (...) @new_string()
store %struct.string_t* %6, %struct.string_t** %4
br label %7
7: ; preds = %16, %1
%8 = load i32*, i32** %3
%9 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %20
11: ; preds = %7
%12 = load i8, i8* %5
%13 = icmp ne i8 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = load %struct.string_t*, %struct.string_t** %4
store %struct.string_t* %15, %struct.string_t** %2
br label %23
16: ; preds = %11
%17 = load %struct.string_t*, %struct.string_t** %4
%18 = load i8, i8* %5
%19 = call i32 @string_cat_char(%struct.string_t* %17, i8 signext %18)
br label %7
20: ; preds = %7
%21 = load %struct.string_t*, %struct.string_t** %4
%22 = call i32 @free_string(%struct.string_t* %21)
store %struct.string_t* null, %struct.string_t** %2
br label %23
23: ; preds = %20, %14
%24 = load %struct.string_t*, %struct.string_t** %2
ret %struct.string_t* %24
}

LLVM IR (-O2):
%struct.string_t = type { i32 }
define %struct.string_t* @fread_string(i32* nocapture %0) {
%2 = alloca i8
%3 = tail call %struct.string_t* (...) @new_string()
%4 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %0)
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %13
6: ; preds = %1, %9
%7 = load i8, i8* %2
%8 = icmp eq i8 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i32 @string_cat_char(%struct.string_t* %3, i8 signext %7)
%11 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %0)
%12 = icmp eq i32 %11, 1
br i1 %12, label %6, label %13
13: ; preds = %9, %1
%14 = tail call i32 @free_string(%struct.string_t* %3)
br label %15
15: ; preds = %6, %13
%16 = phi %struct.string_t* [ null, %13 ], [ %3, %6 ]
ret %struct.string_t* %16
}