#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *FileReadStr(FILE *f) {
  char *res;
  char *tmp;
  int curlen;
  res = (char *)malloc(1);
  res[0] = 0;
  tmp = (char *)malloc(10 + 5);
  while ((strlen(res) == 0) || (res[strlen(res) - 1] != '\n')) {
    if (fgets(tmp, 10, f) == NULL) {
      tmp[0] = '\0';
      break;
    }
    curlen = strlen(res);
    res = realloc(res, strlen(res) + strlen(tmp) + 2);
    strcpy(res + strlen(res), tmp);
  }
  if (tmp[0] == '\0') {
    free(tmp);
    free(res);
    return NULL;
  }
  res[strlen(res) - 1] = '\0';
  free(tmp);
  return res;
}


x86 assembly (clang, -O0):
<FileReadStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    $0xf,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
je     11d2 <FileReadStr+0x72>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11e2 <FileReadStr+0x82>
jmp    1270 <FileReadStr+0x110>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    120a <FileReadStr+0xaa>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1270 <FileReadStr+0x110>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%rdi
add    %eax,%esi
add    $0x2,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    118f <FileReadStr+0x2f>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    129f <FileReadStr+0x13f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <FileReadStr+0x171>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FileReadStr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    $0xf,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     11ad <FileReadStr+0x4d>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     121f <FileReadStr+0xbf>
mov    %r14,%rdi
mov    $0xa,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1207 <FileReadStr+0xa7>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%esi
add    $0x2,%esi
mov    %rbx,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    1190 <FileReadStr+0x30>
movb   $0x0,(%r14)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    123c <FileReadStr+0xdc>
cmpb   $0x0,(%r14)
je     120b <FileReadStr+0xab>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @FileReadStr(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
%7 = call i64 @malloc(i32 1)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 0, i8* %10
%11 = call i64 @malloc(i32 15)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %5
br label %13
13: ; preds = %37, %1
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br label %27
27: ; preds = %17, %13
%28 = phi i1 [ true, %13 ], [ %26, %17 ]
br i1 %28, label %29, label %55
29: ; preds = %27
%30 = load i8*, i8** %5
%31 = load i32*, i32** %3
%32 = call i32* @fgets(i8* %30, i32 10, i32* %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 0
store i8 0, i8* %36
br label %55
37: ; preds = %29
%38 = load i8*, i8** %4
%39 = call i32 @strlen(i8* %38)
store i32 %39, i32* %6
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %42, %44
%46 = add nsw i32 %45, 2
%47 = call i8* @realloc(i8* %40, i32 %46)
store i8* %47, i8** %4
%48 = load i8*, i8** %4
%49 = load i8*, i8** %4
%50 = call i32 @strlen(i8* %49)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8*, i8** %5
%54 = call i32 @strcpy(i8* %52, i8* %53)
br label %13
55: ; preds = %34, %27
%56 = load i8*, i8** %5
%57 = getelementptr inbounds i8, i8* %56, i64 0
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i8*, i8** %5
%63 = call i32 @free(i8* %62)
%64 = load i8*, i8** %4
%65 = call i32 @free(i8* %64)
store i8* null, i8** %2
br label %76
66: ; preds = %55
%67 = load i8*, i8** %4
%68 = load i8*, i8** %4
%69 = call i32 @strlen(i8* %68)
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %67, i64 %71
store i8 0, i8* %72
%73 = load i8*, i8** %5
%74 = call i32 @free(i8* %73)
%75 = load i8*, i8** %4
store i8* %75, i8** %2
br label %76
76: ; preds = %66, %61
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
define i8* @FileReadStr(i32* nocapture %0) {
%2 = tail call i64 @malloc(i32 1)
%3 = inttoptr i64 %2 to i8*
store i8 0, i8* %3
%4 = tail call i64 @malloc(i32 15)
%5 = inttoptr i64 %4 to i8*
br label %6
6: ; preds = %21, %1
%7 = phi i8* [ %3, %1 ], [ %27, %21 ]
%8 = tail call i32 @strlen(i8* %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %6
%11 = tail call i32 @strlen(i8* %7)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %7, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %32, label %17
17: ; preds = %6, %10
%18 = tail call i32* @fgets(i8* %5, i32 10, i32* %0)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
store i8 0, i8* %5
br label %35
21: ; preds = %17
%22 = tail call i32 @strlen(i8* %7)
%23 = tail call i32 @strlen(i8* %7)
%24 = tail call i32 @strlen(i8* %5)
%25 = add i32 %23, 2
%26 = add i32 %25, %24
%27 = tail call i8* @realloc(i8* %7, i32 %26)
%28 = tail call i32 @strlen(i8* %27)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = tail call i32 @strcpy(i8* %30, i8* %5)
br label %6
32: ; preds = %10
%33 = load i8, i8* %5
%34 = icmp eq i8 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %20, %32
%36 = tail call i32 @free(i8* %5)
%37 = tail call i32 @free(i8* %7)
br label %44
38: ; preds = %32
%39 = tail call i32 @strlen(i8* %7)
%40 = add nsw i32 %39, -1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %7, i64 %41
store i8 0, i8* %42
%43 = tail call i32 @free(i8* %5)
br label %44
44: ; preds = %38, %35
%45 = phi i8* [ null, %35 ], [ %7, %38 ]
ret i8* %45
}