#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 size_t int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void read_line(FILE *file, char *line_buffer) {
  if (file == NULL) {
    printf("Error: file pointer is null.");
    exit(1);
  }
  if (line_buffer == NULL) {
    printf("Error allocating memory for line buffer.");
    exit(1);
  }
  int ch = getc(file);
  int32_t count = 0;
  while ((ch != '\n') && (ch != EOF)) {
    line_buffer[count] = ch;
    count++;
    ch = getc(file);
  }
  line_buffer[count] = '\0';
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1161 <read_line+0x31>
lea    0xeae(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1182 <read_line+0x52>
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x14(%rbp)
mov    %al,-0x21(%rbp)
je     11b7 <read_line+0x87>
mov    -0x14(%rbp),%eax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11c7 <read_line+0x97>
jmp    11f4 <read_line+0xc4>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,-0x14(%rbp)
jmp    1196 <read_line+0x66>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     119c <read_line+0x6c>
mov    %rsi,%r14
test   %rsi,%rsi
je     11a5 <read_line+0x75>
mov    %rdi,%r15
call   1050 <getc@plt>
xor    %ebx,%ebx
cmp    $0xa,%eax
je     118b <read_line+0x5b>
mov    0x2e8d(%rip),%r12
cmp    (%r12),%eax
je     118b <read_line+0x5b>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1050 <getc@plt>
cmp    $0xa,%eax
je     118b <read_line+0x5b>
cmp    (%r12),%eax
jne    1170 <read_line+0x40>
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe5d(%rip),%rdi
jmp    11ac <read_line+0x7c>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Error: file pointer is null.\00"
@.str.1 = [41 x i8] c"Error allocating memory for line buffer.\00"
@EOF = global i32 0
define void @read_line(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = load i32*, i32** %3
%20 = call i32 @getc(i32* %19)
store i32 %20, i32* %5
store i64 0, i64* %6
br label %21
21: ; preds = %30, %18
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
br label %28
28: ; preds = %24, %21
%29 = phi i1 [ false, %21 ], [ %27, %24 ]
br i1 %29, label %30, label %40
30: ; preds = %28
%31 = load i32, i32* %5
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
%34 = load i64, i64* %6
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 %32, i8* %35
%36 = load i64, i64* %6
%37 = add i64 %36, 1
store i64 %37, i64* %6
%38 = load i32*, i32** %3
%39 = call i32 @getc(i32* %38)
store i32 %39, i32* %5
br label %21
40: ; preds = %28
%41 = load i8*, i8** %4
%42 = load i64, i64* %6
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"Error: file pointer is null.\00"
@.str.1 = [41 x i8] c"Error allocating memory for line buffer.\00"
@EOF = global i32 0
define void @read_line(i32* %0, i8* writeonly %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = icmp eq i8* %1, null
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %7
%13 = tail call i32 @getc(i32* %0)
%14 = icmp ne i32 %13, 10
%15 = load i32, i32* @EOF
%16 = icmp ne i32 %13, %15
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %29
18: ; preds = %12, %18
%19 = phi i64 [ %23, %18 ], [ 0, %12 ]
%20 = phi i32 [ %24, %18 ], [ %13, %12 ]
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 %21, i8* %22
%23 = add i64 %19, 1
%24 = tail call i32 @getc(i32* %0)
%25 = icmp ne i32 %24, 10
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %24, %26
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %18, label %29
29: ; preds = %18, %12
%30 = phi i64 [ 0, %12 ], [ %23, %18 ]
%31 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 0, i8* %31
ret void
}