#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 */

/* Variables and functions */
char EOF;
int READ(int, char *, int);
int /*<<< orphan*/ free(void *);
char *malloc(int);
int /*<<< orphan*/ resize(void **, int, int *, int);
char *read_line_fd(int fd) {
  char *str = malloc(80);
  int n_in = 0, size = 80;
  for (;;) {
    char c;
    if (READ(fd, &c, 1) != 1) {
      if (n_in == 0) {
        free((void *)str);
        ;
        str = NULL;
      }
      return str;
    }
    resize((void **)&str, n_in, &size, sizeof(char));
    if (c == 10 || c == EOF)
      break;
    if (c == 13)
      continue;
    str[n_in++] = c;
  }
  resize((void **)&str, n_in, &size, sizeof(char));
  str[n_in] = 0x00;
  return str;
}


x86 assembly (clang, -O0):
<read_line_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x50,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <READ@plt>
cmp    $0x1,%eax
je     11a9 <read_line_fd+0x69>
cmpl   $0x0,-0x1c(%rbp)
jne    119c <read_line_fd+0x5c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123e <read_line_fd+0xfe>
mov    -0x1c(%rbp),%esi
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
mov    $0x1,%ecx
call   1050 <resize@plt>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     11e1 <read_line_fd+0xa1>
movsbl -0x21(%rbp),%eax
mov    0x2e12(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11e6 <read_line_fd+0xa6>
jmp    1215 <read_line_fd+0xd5>
movsbl -0x21(%rbp),%eax
cmp    $0xd,%eax
jne    11f8 <read_line_fd+0xb8>
jmp    1167 <read_line_fd+0x27>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1167 <read_line_fd+0x27>
mov    -0x1c(%rbp),%esi
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
mov    $0x1,%ecx
call   1050 <resize@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    $0x50,%edi
call   1060 <malloc@plt>
mov    %rax,0x8(%rsp)
movl   $0x50,0x14(%rsp)
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <READ@plt>
cmp    $0x1,%eax
jne    11ed <read_line_fd+0xad>
xor    %ebp,%ebp
lea    0x8(%rsp),%r14
lea    0x14(%rsp),%r15
mov    0x2e58(%rip),%r13
lea    0x7(%rsp),%r12
jmp    11b4 <read_line_fd+0x74>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <READ@plt>
cmp    $0x1,%eax
jne    11e9 <read_line_fd+0xa9>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
call   1050 <resize@plt>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     11fb <read_line_fd+0xbb>
cmp    0x0(%r13),%al
je     11fb <read_line_fd+0xbb>
cmp    $0xd,%al
je     11a0 <read_line_fd+0x60>
mov    0x8(%rsp),%rcx
movslq %ebp,%rdx
add    $0x1,%ebp
mov    %al,(%rcx,%rdx,1)
jmp    11a0 <read_line_fd+0x60>
test   %ebp,%ebp
jne    121d <read_line_fd+0xdd>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1222 <read_line_fd+0xe2>
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rdx
mov    %ebp,%esi
mov    $0x1,%ecx
call   1050 <resize@plt>
mov    0x8(%rsp),%rax
movslq %ebp,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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