#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ ho_strlen(char *);
char *malloc(scalar_t__);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, scalar_t__);
int stat(char *, struct stat *);
char *strerror(int);
int main(int argc, char **argv) {
  int fd = -1;
  fd = open(argv[1], O_RDONLY, 0644);
  if (fd == -1) {
    printf("open error:%s\n", strerror(errno));
    return errno;
  }
  struct stat sb;
  if (stat(argv[1], &sb) == -1) {
    printf("stat error:%s\n", strerror(errno));
    return errno;
  }
  char *p = malloc(sb.st_size + 1);
  read(fd, p, sb.st_size);
  ho_strlen(p);
  free(p);
  close(fd);
  return 0;
}


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)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1203 <main+0x73>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <main+0xfd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xbb>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdda(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128d <main+0xfd>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <read@plt>
mov    -0x28(%rbp),%rdi
call   1040 <ho_strlen@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1205 <main+0x75>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     121c <main+0x8c>
mov    0x8(%rsp),%r14
lea    0x1(%r14),%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1070 <read@plt>
mov    %rbx,%rdi
call   1040 <ho_strlen@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    123b <main+0xab>
mov    0x2dcc(%rip),%rbx
mov    (%rbx),%edi
call   10b0 <strerror@plt>
lea    0xde6(%rip),%rdi
jmp    1231 <main+0xa1>
mov    0x2db5(%rip),%rbx
mov    (%rbx),%edi
call   10b0 <strerror@plt>
lea    0xdde(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    (%rbx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [15 x i8] c"open error:%s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"stat error:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 420)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @errno
store i32 %20, i32* %3
br label %48
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @stat(i8* %24, %struct.stat* %7)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i32, i32* @errno
store i32 %31, i32* %3
br label %48
32: ; preds = %21
%33 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%34 = load i64, i64* %33
%35 = add nsw i64 %34, 1
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %8
%37 = load i32, i32* %6
%38 = load i8*, i8** %8
%39 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%40 = load i64, i64* %39
%41 = call i32 @read(i32 %37, i8* %38, i64 %40)
%42 = load i8*, i8** %8
%43 = call i32 @ho_strlen(i8* %42)
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %32, %27, %16
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [15 x i8] c"open error:%s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"stat error:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%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, i32 420)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @errno
br label %35
14: ; preds = %2
%15 = bitcast %struct.stat* %3 to i8*
%16 = load i8*, i8** %4
%17 = call i32 @stat(i8* %16, %struct.stat* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @errno
br label %33
24: ; preds = %14
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i64, i64* %25
%27 = add nsw i64 %26, 1
%28 = tail call i8* @malloc(i64 %27)
%29 = tail call i32 @read(i32 %7, i8* %28, i64 %26)
%30 = tail call i32 @ho_strlen(i8* %28)
%31 = tail call i32 @free(i8* %28)
%32 = tail call i32 @close(i32 %7)
br label %33
33: ; preds = %24, %19
%34 = phi i32 [ %23, %19 ], [ 0, %24 ]
br label %35
35: ; preds = %33, %9
%36 = phi i32 [ %13, %9 ], [ %34, %33 ]
ret i32 %36
}