#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t readline(int, char *, size_t);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int fd;
  size_t ret = 0;
  size_t len = 32;
  char *buff = malloc(len);
  if (argc < 2) {
    usage(argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open");
    return 2;
  }
  while ((ret = readline(fd, buff, len)) > 0) {
    if (ret <= len) {
      buff[ret + 1] = 0;
      printf("%s", buff);
    } else {
      free(buff);
      len *= 2;
      buff = malloc(len);
      if (buff == NULL) {
        break;
      }
    }
  }
  free(buff);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x20,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d5 <main+0x55>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b7 <main+0x137>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1210 <main+0x90>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12b7 <main+0x137>
jmp    1215 <main+0x95>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <readline@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    129f <main+0x11f>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
ja     1263 <main+0xe3>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x30(%rbp),%rsi
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
jmp    129a <main+0x11a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1295 <main+0x115>
jmp    129f <main+0x11f>
jmp    129a <main+0x11a>
jmp    1215 <main+0x95>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x20,%edi
call   1070 <malloc@plt>
cmp    $0x1,%ebp
jg     11ae <main+0x2e>
mov    (%rbx),%rdi
call   1080 <usage@plt>
mov    $0x1,%ebp
jmp    125e <main+0xde>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1269 <main+0xe9>
mov    %eax,%r14d
mov    $0x20,%ebx
mov    $0x20,%edx
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <readline@plt>
test   %rax,%rax
je     124c <main+0xcc>
mov    %rax,%rbp
lea    0xe14(%rip),%r12
jmp    1227 <main+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,0x1(%rbp,%r15,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <readline@plt>
mov    %rax,%rbp
test   %rax,%rax
je     124c <main+0xcc>
cmp    %rbx,%rbp
jbe    1200 <main+0x80>
mov    %r15,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1211 <main+0x91>
xor    %r15d,%r15d
jmp    124e <main+0xce>
xor    %ebp,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%ebp
jmp    125e <main+0xde>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(32) i8* @malloc(i64 32)
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @usage(i8* %6)
br label %44
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 %17, label %14
14: ; preds = %8
%15 = tail call i64 @readline(i32 %12, i8* %3, i64 32)
%16 = icmp eq i64 %15, 0
br i1 %16, label %38, label %19
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %44
19: ; preds = %14, %33
%20 = phi i64 [ %36, %33 ], [ %15, %14 ]
%21 = phi i8* [ %35, %33 ], [ %3, %14 ]
%22 = phi i64 [ %34, %33 ], [ 32, %14 ]
%23 = icmp ugt i64 %20, %22
br i1 %23, label %28, label %24
24: ; preds = %19
%25 = add i64 %20, 1
%26 = getelementptr inbounds i8, i8* %21, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %33
28: ; preds = %19
%29 = tail call i32 @free(i8* %21)
%30 = shl i64 %22, 1
%31 = tail call i8* @malloc(i64 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %28, %24
%34 = phi i64 [ %22, %24 ], [ %30, %28 ]
%35 = phi i8* [ %21, %24 ], [ %31, %28 ]
%36 = tail call i64 @readline(i32 %12, i8* %35, i64 %34)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %19
38: ; preds = %33, %28, %14
%39 = phi i64 [ 0, %14 ], [ %20, %28 ], [ 0, %33 ]
%40 = phi i8* [ %3, %14 ], [ null, %28 ], [ %35, %33 ]
%41 = tail call i32 @free(i8* %40)
%42 = tail call i32 @close(i32 %12)
%43 = trunc i64 %39 to i32
br label %44
44: ; preds = %38, %17, %5
%45 = phi i32 [ 1, %5 ], [ 2, %17 ], [ %43, %38 ]
ret i32 %45
}