#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *readline(FILE *fp) {
  char *str = (char *)NULL;
  int ch = 0, len = 256, step = 256, i = 0;
  str = (char *)malloc(len);
  if (str == NULL)
    return str;
  while (1) {
    ch = fgetc(fp);
    if (feof(fp))
      break;
    if (ch == '\n' || ch == '\r') {
      str[i++] = 0;
      break;
    }
    str[i++] = ch;
    if (i == len - 2) {
      len += step;
      str = (char *)realloc(str, len);
      if (str == NULL) {
        fclose(fp);
        return str;
      }
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x100,-0x20(%rbp)
movl   $0x100,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <readline+0x54>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1273 <readline+0x123>
jmp    11a9 <readline+0x59>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11cd <readline+0x7d>
jmp    126b <readline+0x11b>
cmpl   $0xa,-0x1c(%rbp)
je     11e1 <readline+0x91>
cmpl   $0xd,-0x1c(%rbp)
jne    11fc <readline+0xac>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    126b <readline+0x11b>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jne    1266 <readline+0x116>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1261 <readline+0x111>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1273 <readline+0x123>
jmp    1266 <readline+0x116>
jmp    11a9 <readline+0x59>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x100,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e1 <readline+0x91>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    11ee <readline+0x9e>
mov    $0x100,%ebx
xor    %r12d,%r12d
jmp    11a7 <readline+0x57>
xchg   %ax,%ax
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    11ee <readline+0x9e>
cmp    $0xd,%ebp
je     11e6 <readline+0x96>
cmp    $0xa,%ebp
je     11e6 <readline+0x96>
mov    %bpl,(%r15,%r12,1)
add    $0x1,%r12
lea    -0x2(%rbx),%eax
cmp    %rax,%r12
jne    1190 <readline+0x40>
add    $0x100,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1190 <readline+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %r15d,%r15d
jmp    11ee <readline+0x9e>
mov    %r12d,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @readline(i32* nocapture %0) {
%2 = tail call i64 @malloc(i32 256)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %37, label %5
5: ; preds = %1
%6 = tail call i32 @fgetc(i32* %0)
%7 = tail call i64 @feof(i32* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %37
9: ; preds = %5, %31
%10 = phi i64 [ %19, %31 ], [ 0, %5 ]
%11 = phi i32 [ %34, %31 ], [ %6, %5 ]
%12 = phi i32 [ %33, %31 ], [ 256, %5 ]
%13 = phi i8* [ %32, %31 ], [ %3, %5 ]
switch i32 %11, label %17 [
i32 13, label %14
i32 10, label %14
]
14: ; preds = %9, %9
%15 = and i64 %10, 4294967295
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
br label %37
17: ; preds = %9
%18 = trunc i32 %11 to i8
%19 = add nuw i64 %10, 1
%20 = getelementptr inbounds i8, i8* %13, i64 %10
store i8 %18, i8* %20
%21 = add nsw i32 %12, -2
%22 = zext i32 %21 to i64
%23 = icmp eq i64 %19, %22
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = add nsw i32 %12, 256
%26 = tail call i64 @realloc(i8* %13, i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = icmp eq i64 %26, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @fclose(i32* %0)
br label %37
31: ; preds = %24, %17
%32 = phi i8* [ %27, %24 ], [ %13, %17 ]
%33 = phi i32 [ %25, %24 ], [ %12, %17 ]
%34 = tail call i32 @fgetc(i32* %0)
%35 = tail call i64 @feof(i32* %0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %9, label %37
37: ; preds = %31, %5, %14, %1, %29
%38 = phi i8* [ null, %29 ], [ null, %1 ], [ %13, %14 ], [ %3, %5 ], [ %32, %31 ]
ret i8* %38
}