#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*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ strlen(char *);
char *xrealloc(char *, int);
char *strreadline(FILE *stream) {
  int i;
  char c, *ptr_s;
  for (i = 0, ptr_s = NULL; !feof(stream);) {
    c = fgetc(stream);
    if (c == '\n' || c == '\r')
      break;
    ptr_s = xrealloc(ptr_s, i + 1);
    *(ptr_s + i++) = c;
  }
  ptr_s = xrealloc(ptr_s, i + 1);
  *(ptr_s + i) = '\0';
  if (strlen(ptr_s) == 0) {
    free(ptr_s);
    return NULL;
  }
  return ptr_s;
}


x86 assembly (clang, -O0):
<strreadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1189 <strreadline+0x39>
jmp    11e4 <strreadline+0x94>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
je     11af <strreadline+0x5f>
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    11b4 <strreadline+0x64>
jmp    11e4 <strreadline+0x94>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <xrealloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x15(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    116b <strreadline+0x1b>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1050 <xrealloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    122c <strreadline+0xdc>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1234 <strreadline+0xe4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strreadline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
call   1060 <feof@plt>
test   %eax,%eax
je     115d <strreadline+0x1d>
xor    %r12d,%r12d
xor    %r15d,%r15d
jmp    11ac <strreadline+0x6c>
xor    %r13d,%r13d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
je     11a9 <strreadline+0x69>
mov    %eax,%ebx
cmp    $0xd,%al
je     11a9 <strreadline+0x69>
lea    0x1(%r13),%r12
mov    %r15,%rdi
mov    %r12d,%esi
call   1040 <xrealloc@plt>
mov    %rax,%r15
mov    %bl,(%rax,%r13,1)
mov    %r14,%rdi
call   1060 <feof@plt>
mov    %r12,%r13
test   %eax,%eax
je     1170 <strreadline+0x30>
jmp    11ac <strreadline+0x6c>
mov    %r13,%r12
lea    0x1(%r12),%esi
mov    %r15,%rdi
call   1040 <xrealloc@plt>
mov    %r12d,%ecx
movb   $0x0,(%rax,%rcx,1)
cmpb   $0x0,(%rax)
jne    11cf <strreadline+0x8f>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @strreadline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i32* %0, i32** %3
store i32 0, i32* %4
store i8* null, i8** %6
br label %7
7: ; preds = %23, %1
%8 = load i32*, i32** %3
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %34
12: ; preds = %7
%13 = load i32*, i32** %3
%14 = call signext i8 @fgetc(i32* %13)
store i8 %14, i8* %5
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 10
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 13
br i1 %21, label %22, label %23
22: ; preds = %18, %12
br label %34
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = call i8* @xrealloc(i8* %24, i32 %26)
store i8* %27, i8** %6
%28 = load i8, i8* %5
%29 = load i8*, i8** %6
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
br label %7
34: ; preds = %22, %7
%35 = load i8*, i8** %6
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
%38 = call i8* @xrealloc(i8* %35, i32 %37)
store i8* %38, i8** %6
%39 = load i8*, i8** %6
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %6
%44 = call i64 @strlen(i8* %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
store i8* null, i8** %2
br label %51
49: ; preds = %34
%50 = load i8*, i8** %6
store i8* %50, i8** %2
br label %51
51: ; preds = %49, %46
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
define i8* @strreadline(i32* nocapture %0) {
%2 = tail call i32 @feof(i32* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %19
4: ; preds = %1, %8
%5 = phi i64 [ %9, %8 ], [ 0, %1 ]
%6 = phi i8* [ %11, %8 ], [ null, %1 ]
%7 = tail call signext i8 @fgetc(i32* %0)
switch i8 %7, label %8 [
i8 13, label %15
i8 10, label %15
]
8: ; preds = %4
%9 = add nuw i64 %5, 1
%10 = trunc i64 %9 to i32
%11 = tail call i8* @xrealloc(i8* %6, i32 %10)
%12 = getelementptr inbounds i8, i8* %11, i64 %5
store i8 %7, i8* %12
%13 = tail call i32 @feof(i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %4, label %15
15: ; preds = %8, %4, %4
%16 = phi i64 [ %5, %4 ], [ %5, %4 ], [ %9, %8 ]
%17 = phi i8* [ %6, %4 ], [ %6, %4 ], [ %11, %8 ]
%18 = trunc i64 %16 to i32
br label %19
19: ; preds = %15, %1
%20 = phi i32 [ 0, %1 ], [ %18, %15 ]
%21 = phi i8* [ null, %1 ], [ %17, %15 ]
%22 = add nuw nsw i32 %20, 1
%23 = tail call i8* @xrealloc(i8* %21, i32 %22)
%24 = zext i32 %20 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
%26 = load i8, i8* %23
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = tail call i32 @free(i8* %23)
br label %30
30: ; preds = %19, %28
%31 = phi i8* [ null, %28 ], [ %23, %19 ]
ret i8* %31
}