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

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ editorAppendRow(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
void editorOpen(char *filename) {
  FILE *fp = fopen(filename, "r");
  if (!fp)
    die("fopen");
  char *line = NULL;
  size_t linecap = 0;
  ssize_t linelen;
  while ((linelen = getline(&line, &linecap, fp)) != -1) {
    while (linelen > 0 &&
           (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) {
      linelen--;
    }
    editorAppendRow(line, linelen);
  }
  free(line);
  fclose(fp);
}


x86 assembly (clang, -O0):
<editorOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1197 <editorOpen+0x37>
lea    0xe70(%rip),%rdi
call   1070 <die@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1246 <editorOpen+0xe6>
jmp    11c9 <editorOpen+0x69>
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jle    1217 <editorOpen+0xb7>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x26(%rbp)
je     1211 <editorOpen+0xb1>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0x26(%rbp)
mov    -0x26(%rbp),%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1227 <editorOpen+0xc7>
jmp    1235 <editorOpen+0xd5>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11c9 <editorOpen+0x69>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <editorAppendRow@plt>
jmp    11a7 <editorOpen+0x47>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<editorOpen>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1189 <editorOpen+0x29>
lea    0xe7e(%rip),%rdi
call   1070 <die@plt>
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1213 <editorOpen+0xb3>
mov    %rsp,%r14
lea    0x8(%rsp),%r15
jmp    11dc <editorOpen+0x7c>
nopl   0x0(%rax)
xor    %eax,%eax
mov    %eax,%esi
call   1030 <editorAppendRow@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1213 <editorOpen+0xb3>
mov    (%rsp),%rdi
test   %eax,%eax
jle    11c2 <editorOpen+0x62>
mov    %eax,%eax
jmp    11fd <editorOpen+0x9d>
nopl   0x0(%rax,%rax,1)
lea    0x1(%rcx),%rdx
mov    %rcx,%rax
cmp    $0x1,%rdx
jbe    11c0 <editorOpen+0x60>
lea    -0x1(%rax),%rcx
mov    %ecx,%edx
movzbl (%rdi,%rdx,1),%edx
cmp    $0xd,%dl
je     11f0 <editorOpen+0x90>
cmp    $0xa,%dl
je     11f0 <editorOpen+0x90>
jmp    11c2 <editorOpen+0x62>
mov    (%rsp),%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
define void @editorOpen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i8* null, i8** %4
store i64 0, i64* %5
br label %14
14: ; preds = %47, %13
%15 = load i32*, i32** %3
%16 = call i32 @getline(i8** %4, i64* %5, i32* %15)
store i32 %16, i32* %6
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %51
18: ; preds = %14
br label %19
19: ; preds = %44, %18
%20 = load i32, i32* %6
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %42
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %40, label %31
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 13
br label %40
40: ; preds = %31, %22
%41 = phi i1 [ true, %22 ], [ %39, %31 ]
br label %42
42: ; preds = %40, %19
%43 = phi i1 [ false, %19 ], [ %41, %40 ]
br i1 %43, label %44, label %47
44: ; preds = %42
%45 = load i32, i32* %6
%46 = add nsw i32 %45, -1
store i32 %46, i32* %6
br label %19
47: ; preds = %42
%48 = load i8*, i8** %4
%49 = load i32, i32* %6
%50 = call i32 @editorAppendRow(i8* %48, i32 %49)
br label %14
51: ; preds = %14
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
%54 = load i32*, i32** %3
%55 = call i32 @fclose(i32* %54)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
define void @editorOpen(i8* nocapture %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%10 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%11 = call i32 @getline(i8** %2, i64* %3, i32* %4)
%12 = icmp eq i32 %11, -1
br i1 %12, label %34, label %13
13: ; preds = %8, %29
%14 = phi i32 [ %32, %29 ], [ %11, %8 ]
%15 = icmp sgt i32 %14, 0
%16 = load i8*, i8** %2
br i1 %15, label %17, label %29
17: ; preds = %13
%18 = zext i32 %14 to i64
br label %19
19: ; preds = %17, %25
%20 = phi i64 [ %18, %17 ], [ %21, %25 ]
%21 = add nsw i64 %20, -1
%22 = and i64 %21, 4294967295
%23 = getelementptr inbounds i8, i8* %16, i64 %22
%24 = load i8, i8* %23
switch i8 %24, label %27 [
i8 10, label %25
i8 13, label %25
]
25: ; preds = %19, %19
%26 = icmp ugt i64 %20, 1
br i1 %26, label %19, label %29
27: ; preds = %19
%28 = trunc i64 %20 to i32
br label %29
29: ; preds = %25, %27, %13
%30 = phi i32 [ %14, %13 ], [ %28, %27 ], [ 0, %25 ]
%31 = call i32 @editorAppendRow(i8* %16, i32 %30)
%32 = call i32 @getline(i8** %2, i64* %3, i32* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %13
34: ; preds = %29, %8
%35 = load i8*, i8** %2
%36 = call i32 @free(i8* %35)
%37 = call i32 @fclose(i32* %4)
ret void
}