#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*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int extract_lines(unsigned char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *mmap(int /*<<< orphan*/, unsigned int, int /*<<< orphan*/,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd;
  unsigned size;
  unsigned char *image;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    return -1;
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    fprintf(stderr, "Cannot open %s\n", argv[1]);
    return -2;
  }
  size = lseek(fd, 0, SEEK_END);
  if ((image = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)) ==
      (unsigned char *)-1) {
    fprintf(stderr, "Cannot map ELF file\n");
    return -3;
  }
  return extract_lines(image);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119a <main+0x4a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <main+0x114>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11e7 <main+0x97>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1264 <main+0x114>
mov    -0x14(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1258 <main+0x108>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1264 <main+0x114>
mov    -0x20(%rbp),%rdi
call   1070 <extract_lines@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ba <main+0x6a>
mov    0x8(%rbx),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11db <main+0x8b>
mov    %eax,%ebp
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e53(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11fd <main+0xad>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <extract_lines@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe33(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1219 <main+0xc9>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe27(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xfffffffe,%eax
jmp    1219 <main+0xc9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xfffffffd,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [21 x i8] c"Cannot map ELF file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %46
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -2, i32* %3
br label %46
30: ; preds = %17
%31 = load i32, i32* %6
%32 = load i32, i32* @SEEK_END
%33 = call i32 @lseek(i32 %31, i32 0, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %6
%38 = call i8* @mmap(i32 0, i32 %34, i32 %35, i32 %36, i32 %37, i32 0)
store i8* %38, i8** %8
%39 = icmp eq i8* %38, inttoptr (i64 -1 to i8*)
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %3
br label %46
43: ; preds = %30
%44 = load i8*, i8** %8
%45 = call i32 @extract_lines(i8* %44)
store i32 %45, i32* %3
br label %46
46: ; preds = %43, %40, %24, %11
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"Cannot open %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [21 x i8] c"Cannot map ELF file\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %30
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %9
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %30
18: ; preds = %8
%19 = load i32, i32* @SEEK_END
%20 = tail call i32 @lseek(i32 %12, i32 0, i32 %19)
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = tail call i8* @mmap(i32 0, i32 %20, i32 %21, i32 %22, i32 %12, i32 0)
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %30
28: ; preds = %18
%29 = tail call i32 @extract_lines(i8* %23)
br label %30
30: ; preds = %28, %25, %14, %4
%31 = phi i32 [ -1, %4 ], [ -2, %14 ], [ -3, %25 ], [ %29, %28 ]
ret i32 %31
}