#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ asprintf(char **, char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("can't open");
    return 1;
  }
  int len = 1024, n = 0;
  char buff[len];
  do {
    n = read(fd, buff, len - 1);
    if (n > 0) {
      buff[n] = '\0';
      printf("%s", buff);
    }
  } while (n < 0 && EINTR == errno);
  if (n < 0) {
    char *err = NULL;
    asprintf(&err, "cant read %s", argv[1]);
    perror(err);
    free(err);
    return 1;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b1 <main+0x51>
lea    0xe60(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x169>
movl   $0x400,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    121e <main+0xbe>
mov    -0x48(%rbp),%rsi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1223 <main+0xc3>
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x49(%rbp)
jge    124c <main+0xec>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11e5 <main+0x85>
cmpl   $0x0,-0x1c(%rbp)
jge    12a6 <main+0x146>
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    -0x38(%rbp),%rdi
lea    0xd8f(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12c2 <main+0x162>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     124f <main+0xdf>
mov    %eax,%r15d
mov    %rsp,-0x38(%rbp)
mov    %rsp,%rbx
add    $0xfffffffffffffc00,%rbx
mov    %rbx,%rsp
mov    0x2e17(%rip),%r13
mov    0x2e18(%rip),%r12
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1219 <main+0xa9>
je     11e0 <main+0x70>
mov    (%r12),%rcx
cmp    %rcx,0x0(%r13)
je     11c0 <main+0x50>
test   %eax,%eax
je     1230 <main+0xc0>
movq   $0x0,-0x30(%rbp)
mov    0x8(%r14),%rdx
lea    0xe14(%rip),%rsi
lea    -0x30(%rbp),%rdi
call   1070 <asprintf@plt>
mov    -0x30(%rbp),%rdi
call   1090 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    123c <main+0xcc>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
lea    0xdef(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1240 <main+0xd0>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [11 x i8] c"can't open\00"
@.str.1 = [3 x i8] c"%s\00"
@EINTR = global i64 0
@errno = global i64 0
@.str.2 = [13 x i8] c"cant read %s\00"
@.str.3 = [2 x i8] c"\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 i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %65
22: ; preds = %2
store i32 1024, i32* %7
store i32 0, i32* %8
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %9
%26 = alloca i8, i64 %24
store i64 %24, i64* %10
br label %27
27: ; preds = %47, %22
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 1
%31 = call i32 @read(i32 %28, i8* %26, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %26, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %39
39: ; preds = %34, %27
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i64, i64* @EINTR
%45 = load i64, i64* @errno
%46 = icmp eq i64 %44, %45
br label %47
47: ; preds = %43, %40
%48 = phi i1 [ false, %40 ], [ %46, %43 ]
br i1 %48, label %27, label %49
49: ; preds = %47
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %61
52: ; preds = %49
store i8* null, i8** %11
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @asprintf(i8** %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %11
%58 = call i32 @perror(i8* %57)
%59 = load i8*, i8** %11
%60 = call i32 @free(i8* %59)
store i32 1, i32* %3
store i32 1, i32* %12
br label %63
61: ; preds = %49
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %12
br label %63
63: ; preds = %61, %52
%64 = load i8*, i8** %9
br label %65
65: ; preds = %63, %20
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [11 x i8] c"can't open\00"
@.str.1 = [3 x i8] c"%s\00"
@EINTR = global i64 0
@errno = global i64 0
@.str.2 = [13 x i8] c"cant read %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %41
11: ; preds = %2
%12 = tail call i8* @llvm.stacksave()
%13 = alloca [1024 x i8]
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
br label %15
15: ; preds = %22, %11
%16 = call i32 @read(i32 %7, i8* %14, i32 1023)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = zext i32 %16 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %37
22: ; preds = %15
%23 = icmp ne i32 %16, 0
%24 = load i64, i64* @EINTR
%25 = load i64, i64* @errno
%26 = icmp eq i64 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %15, label %28
28: ; preds = %22
br i1 %23, label %29, label %37
29: ; preds = %28
%30 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%31 = load i8*, i8** %4
%32 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %3
%34 = call i32 @perror(i8* %33)
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
br label %39
37: ; preds = %18, %28
%38 = call i32 @putchar(i32 10)
br label %39
39: ; preds = %37, %29
%40 = phi i32 [ 1, %29 ], [ 0, %37 ]
br label %41
41: ; preds = %39, %9
%42 = phi i32 [ 1, %9 ], [ %40, %39 ]
ret i32 %42
}