#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, size_t);
char *strchr(char *, char);
int strlen(char *);
bool read_line(char *buffer, size_t *buf_size) {
  char *eol = strchr(buffer, '\r');
  if (eol != NULL) {
    if (*(eol + 1) != '\n') {
      return false;
    }
    return true;
  }
  char *buf_end = buffer + strlen(buffer);
  int size = read(0, buf_end, *buf_size - (buf_end - buffer) - 1);
  if (size == -1) {
    perror("read");
    exit(2);
  }
  *buf_size += size;
  buffer[*buf_size] = 0;
  eol = strchr(buffer, '\r');
  if (eol != NULL) {
    if (*(eol + 1) != '\n') {
      return false;
    }
    return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a6 <read_line+0x56>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0xa,%eax
je     119a <read_line+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    1278 <read_line+0x128>
movl   $0x1,-0x4(%rbp)
jmp    1278 <read_line+0x128>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sub    %rax,%rdx
sub    $0x1,%rdx
xor    %edi,%edi
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    120e <read_line+0xbe>
lea    0xe01(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
movslq -0x2c(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1271 <read_line+0x121>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0xa,%eax
je     1265 <read_line+0x115>
movl   $0x0,-0x4(%rbp)
jmp    1278 <read_line+0x128>
movl   $0x1,-0x4(%rbp)
jmp    1278 <read_line+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11aa <read_line+0x5a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdx
lea    (%rbx,%rdx,1),%rsi
not    %rdx
add    (%r14),%rdx
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11bb <read_line+0x6b>
cltq
add    (%r14),%rax
mov    %rax,(%r14)
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11b4 <read_line+0x64>
xor    %ebp,%ebp
cmpb   $0xa,0x1(%rax)
sete   %bpl
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i32 @read_line(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = call i8* @strchr(i8* %9, i8 signext 13)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 10
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 0, i32* %3
br label %66
20: ; preds = %13
store i32 1, i32* %3
br label %66
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i64*, i64** %5
%29 = load i64, i64* %28
%30 = load i8*, i8** %7
%31 = load i8*, i8** %4
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = sub i64 %29, %34
%36 = sub i64 %35, 1
%37 = call i32 @read(i32 0, i8* %27, i64 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %21
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @exit(i32 2)
unreachable
43: ; preds = %21
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = load i64*, i64** %5
%47 = load i64, i64* %46
%48 = add i64 %47, %45
store i64 %48, i64* %46
%49 = load i8*, i8** %4
%50 = load i64*, i64** %5
%51 = load i64, i64* %50
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %4
%54 = call i8* @strchr(i8* %53, i8 signext 13)
store i8* %54, i8** %6
%55 = load i8*, i8** %6
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %65
57: ; preds = %43
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i64 1
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 10
br i1 %62, label %63, label %64
63: ; preds = %57
store i32 0, i32* %3
br label %66
64: ; preds = %57
store i32 1, i32* %3
br label %66
65: ; preds = %43
store i32 0, i32* %3
br label %66
66: ; preds = %65, %64, %63, %20, %19
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @read_line(i8* %0, i64* nocapture %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 13)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %24
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i64, i64* %1
%10 = xor i64 %7, -1
%11 = add i64 %9, %10
%12 = tail call i32 @read(i32 0, i8* %8, i64 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %5
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %5
%18 = sext i32 %12 to i64
%19 = load i64, i64* %1
%20 = add i64 %19, %18
store i64 %20, i64* %1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 0, i8* %21
%22 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 13)
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %17, %2
%25 = phi i8* [ %3, %2 ], [ %22, %17 ]
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %24, %17
%31 = phi i32 [ 0, %17 ], [ %29, %24 ]
ret i32 %31
}