#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*/ assert(int);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
char *xmalloc(int);
char *xrealloc(char *, int);
char *cstring_from_stream(FILE *file) {
  char *text = NULL;
  int size = 255;
  int used = 0;
  int rc;
  assert(file != 0);
  text = xmalloc(size + 1);
  for (;;) {
    if (used == size) {
      size *= 2;
      text = xrealloc(text, size);
    }
    if ((rc = fread(text + used, 1, size - used, file)) <= 0) {
      if (ferror(file)) {
        perror("fread");
      }
      break;
    }
    used += rc;
  }
  text[used] = 0;
  return text;
}


x86 assembly (clang, -O0):
<cstring_from_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0xff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11c8 <cstring_from_stream+0x68>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <xrealloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1217 <cstring_from_stream+0xb7>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1212 <cstring_from_stream+0xb2>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
jmp    1225 <cstring_from_stream+0xc5>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a3 <cstring_from_stream+0x43>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstring_from_stream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1070 <assert@plt>
mov    $0x100,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r15
xor    %ebp,%ebp
mov    $0xff,%r12d
jmp    11b1 <cstring_from_stream+0x51>
movslq %ebp,%rbx
add    %r15,%rbx
mov    %r12d,%edx
sub    %ebp,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
add    %eax,%ebp
test   %eax,%eax
jle    11c9 <cstring_from_stream+0x69>
cmp    %r12d,%ebp
jne    1190 <cstring_from_stream+0x30>
add    %r12d,%r12d
mov    %r15,%rdi
mov    %r12d,%esi
call   1050 <xrealloc@plt>
mov    %rax,%r15
jmp    1190 <cstring_from_stream+0x30>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11e5 <cstring_from_stream+0x85>
movb   $0x0,(%rbx)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
jmp    11d6 <cstring_from_stream+0x76>

LLVM IR (-O0):
@.str = [6 x i8] c"fread\00"
define i8* @cstring_from_stream(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
store i8* null, i8** %3
store i32 255, i32* %4
store i32 0, i32* %5
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = call i8* @xmalloc(i32 %12)
store i8* %13, i8** %3
br label %14
14: ; preds = %42, %1
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* %4
%20 = mul nsw i32 %19, 2
store i32 %20, i32* %4
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = call i8* @xrealloc(i8* %21, i32 %22)
store i8* %23, i8** %3
br label %24
24: ; preds = %18, %14
%25 = load i8*, i8** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %4
%30 = load i32, i32* %5
%31 = sub nsw i32 %29, %30
%32 = load i32*, i32** %2
%33 = call i32 @fread(i8* %28, i32 1, i32 %31, i32* %32)
store i32 %33, i32* %6
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %24
%36 = load i32*, i32** %2
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %46
42: ; preds = %24
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = add nsw i32 %44, %43
store i32 %45, i32* %5
br label %14
46: ; preds = %41
%47 = load i8*, i8** %3
%48 = load i32, i32* %5
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@.str = [6 x i8] c"fread\00"
define i8* @cstring_from_stream(i32* %0) {
%2 = icmp ne i32* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i8* @xmalloc(i32 256)
br label %6
6: ; preds = %14, %1
%7 = phi i32 [ 255, %1 ], [ %15, %14 ]
%8 = phi i32 [ 0, %1 ], [ %22, %14 ]
%9 = phi i8* [ %5, %1 ], [ %16, %14 ]
%10 = icmp eq i32 %8, %7
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = shl nsw i32 %7, 1
%13 = tail call i8* @xrealloc(i8* %9, i32 %12)
br label %14
14: ; preds = %11, %6
%15 = phi i32 [ %12, %11 ], [ %7, %6 ]
%16 = phi i8* [ %13, %11 ], [ %9, %6 ]
%17 = sext i32 %8 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = sub nsw i32 %15, %8
%20 = tail call i32 @fread(i8* %18, i32 1, i32 %19, i32* %0)
%21 = icmp slt i32 %20, 1
%22 = add nsw i32 %20, %8
br i1 %21, label %23, label %6
23: ; preds = %14
%24 = tail call i64 @ferror(i32* %0)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %23, %26
store i8 0, i8* %18
ret i8* %16
}