#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 {
  unsigned long st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, unsigned long, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int get_file_disk(char *uri, unsigned char *file_buf) {
  int read_count = 0;
  int fd = open(uri, O_RDONLY);
  if (fd == -1) {
    perror("open() in get_file_disk http_session.c");
    return -1;
  }
  unsigned long st_size;
  struct stat st;
  if (fstat(fd, &st) == -1) {
    perror("stat() in get_file_disk http_session.c");
    return -1;
  }
  st_size = st.st_size;
  if (st_size > 1048576) {
    fprintf(stderr, "the file %s is too large.\n", uri);
    return -1;
  }
  if ((read_count = read(fd, file_buf, 1048576)) == -1) {
    perror("read() in get_file_disk http_session.c");
    return -1;
  }
  printf("file %s size : %lu , read %d\n", uri, st_size, read_count);
  return read_count;
}


x86 assembly (clang, -O0):
<get_file_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ae <get_file_disk+0x4e>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <get_file_disk+0x108>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11db <get_file_disk+0x7b>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <get_file_disk+0x108>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x100000,-0x28(%rbp)
jbe    1216 <get_file_disk+0xb6>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <get_file_disk+0x108>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x100000,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    124b <get_file_disk+0xeb>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <get_file_disk+0x108>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_disk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e4 <get_file_disk+0x84>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ed <get_file_disk+0x8d>
mov    (%rsp),%rbx
cmp    $0x100001,%rbx
jb     11b8 <get_file_disk+0x58>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
jmp    1202 <get_file_disk+0xa2>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100000,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11f6 <get_file_disk+0x96>
mov    %eax,%ebp
lea    0xebb(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1030 <printf@plt>
jmp    1207 <get_file_disk+0xa7>
lea    0xe15(%rip),%rdi
jmp    11fd <get_file_disk+0x9d>
lea    0xe33(%rip),%rdi
jmp    11fd <get_file_disk+0x9d>
lea    0xe6c(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [39 x i8] c"open() in get_file_disk http_session.c\00"
@.str.1 = [39 x i8] c"stat() in get_file_disk http_session.c\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"the file %s is too large.\0A\00"
@.str.3 = [39 x i8] c"read() in get_file_disk http_session.c\00"
@.str.4 = [30 x i8] c"file %s size : %lu , read %d\0A\00"
define i32 @get_file_disk(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i32 @fstat(i32 %18, %struct.stat* %9)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%25 = load i64, i64* %24
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = icmp ugt i64 %26, 1048576
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %4
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %45
32: ; preds = %23
%33 = load i32, i32* %7
%34 = load i8*, i8** %5
%35 = call i32 @read(i32 %33, i8* %34, i32 1048576)
store i32 %35, i32* %6
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
39: ; preds = %32
%40 = load i8*, i8** %4
%41 = load i64, i64* %8
%42 = load i32, i32* %6
%43 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %40, i64 %41, i32 %42)
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %45
45: ; preds = %39, %37, %28, %21, %15
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [39 x i8] c"open() in get_file_disk http_session.c\00"
@.str.1 = [39 x i8] c"stat() in get_file_disk http_session.c\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"the file %s is too large.\0A\00"
@.str.3 = [39 x i8] c"read() in get_file_disk http_session.c\00"
@.str.4 = [30 x i8] c"file %s size : %lu , read %d\0A\00"
define i32 @get_file_disk(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %2
%10 = bitcast %struct.stat* %3 to i8*
%11 = call i32 @fstat(i32 %5, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %29
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp ugt i64 %17, 1048576
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %29
22: ; preds = %15
%23 = tail call i32 @read(i32 %5, i8* %1, i32 1048576)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %29
27: ; preds = %22
%28 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %17, i32 %23)
br label %29
29: ; preds = %27, %25, %19, %13
%30 = phi i32 [ -1, %13 ], [ -1, %19 ], [ -1, %25 ], [ %23, %27 ]
br label %31
31: ; preds = %29, %7
%32 = phi i32 [ -1, %7 ], [ %30, %29 ]
ret i32 %32
}