#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 fgetc(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
char *read_string(FILE *f) {
  char *string = malloc(sizeof(char) * 256);
  int c;
  int length = 0;
  if (!string)
    return string;
  while ((c = fgetc(f)) != '\00') {
    string[length++] = c;
  }
  string[length++] = '\0';
  return realloc(string, sizeof(char) * length);
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x100,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1169 <read_string+0x39>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cf <read_string+0x9f>
jmp    116e <read_string+0x3e>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     11a2 <read_string+0x72>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    116e <read_string+0x3e>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    $0x100,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     118d <read_string+0x5d>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <fgetc@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     1174 <read_string+0x44>
nopl   0x0(%rax,%rax,1)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <fgetc@plt>
test   %eax,%eax
jne    1160 <read_string+0x30>
mov    %ebx,%eax
add    $0x1,%ebx
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <realloc@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @read_string(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
%7 = call i8* @malloc(i32 256)
store i8* %7, i8** %4
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = load i8*, i8** %4
store i8* %11, i8** %2
br label %37
12: ; preds = %1
br label %13
13: ; preds = %17, %12
%14 = load i32*, i32** %3
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = load i32, i32* %5
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %4
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 %19, i8* %24
br label %13
25: ; preds = %13
%26 = load i8*, i8** %4
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = mul i64 1, %33
%35 = trunc i64 %34 to i32
%36 = call i8* @realloc(i8* %31, i32 %35)
store i8* %36, i8** %2
br label %37
37: ; preds = %25, %10
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
define i8* @read_string(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%3 = icmp eq i8* %2, null
br i1 %3, label %23, label %4
4: ; preds = %1
%5 = tail call i32 @fgetc(i32* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %4, %7
%8 = phi i64 [ %11, %7 ], [ 0, %4 ]
%9 = phi i32 [ %13, %7 ], [ %5, %4 ]
%10 = trunc i32 %9 to i8
%11 = add nuw i64 %8, 1
%12 = getelementptr inbounds i8, i8* %2, i64 %8
store i8 %10, i8* %12
%13 = tail call i32 @fgetc(i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %7
%16 = trunc i64 %11 to i32
br label %17
17: ; preds = %15, %4
%18 = phi i32 [ 0, %4 ], [ %16, %15 ]
%19 = add nuw nsw i32 %18, 1
%20 = zext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %2, i64 %20
store i8 0, i8* %21
%22 = tail call i8* @realloc(i8* %2, i32 %19)
br label %23
23: ; preds = %1, %17
%24 = phi i8* [ %22, %17 ], [ null, %1 ]
ret i8* %24
}