#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 */
typedef int n;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ free(char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(unsigned int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int read_usercode_file(char *p_usercodename, char **p_usercode_buf,
                       unsigned int *p_len) {
  int exitcode = 0;
  int h_usercode;
  int n;
  unsigned int len;
  char *usercode_buf;
  h_usercode = open(p_usercodename, O_RDONLY);
  if (h_usercode >= 0) {
    n = lseek(h_usercode, 0, SEEK_END);
    lseek(h_usercode, 0, SEEK_SET);
    len = (((n) + 512 - 1) & ~(512 - 1));
    usercode_buf = malloc(len);
    if (usercode_buf) {
      memset(usercode_buf + n, 0, len - n);
      if (read(h_usercode, usercode_buf, n) == n) {
        *p_usercode_buf = usercode_buf;
        *p_len = len;
      } else {
        exitcode = errno;
        printf("Failed to read file \"%s\". Please check whether it "
               "exists and verify your read permission.\n",
               p_usercodename);
        free(usercode_buf);
      }
    } else {
      exitcode = errno;
      printf("Failed to allocate memory for \"%s\"\n", p_usercodename);
    }
    close(h_usercode);
  } else {
    exitcode = errno;
    printf("Failed to open file \"%s\". Please check whether it "
           "exists and verify your permission.\n",
           p_usercodename);
  }
  return exitcode;
}


x86 assembly (clang, -O0):
<read_usercode_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     12a9 <read_usercode_file+0x129>
mov    -0x20(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x24(%rbp),%eax
add    $0x200,%eax
sub    $0x1,%eax
and    $0xfffffe00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1280 <read_usercode_file+0x100>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%edx
sub    -0x24(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1080 <read@plt>
cmp    -0x24(%rbp),%eax
jne    1256 <read_usercode_file+0xd6>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    127b <read_usercode_file+0xfb>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xd93(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    129c <read_usercode_file+0x11c>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
jmp    12c5 <read_usercode_file+0x145>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xdbf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_usercode_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     1234 <read_usercode_file+0xb4>
mov    %eax,%ebx
mov    %rbp,0x10(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebp
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0x1ff(%rbp),%r12d
and    $0xfffffe00,%r12d
mov    %r12d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     124f <read_usercode_file+0xcf>
mov    %rax,%r15
mov    %r13,0x8(%rsp)
movslq %ebp,%rdi
add    %rax,%rdi
mov    %r12d,%edx
sub    %ebp,%edx
xor    %r13d,%r13d
xor    %esi,%esi
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <read@plt>
cmp    %ebp,%eax
jne    126a <read_usercode_file+0xea>
mov    0x10(%rsp),%rax
mov    %r15,(%rax)
mov    0x8(%rsp),%rax
mov    %r12d,(%rax)
jmp    128b <read_usercode_file+0x10b>
mov    0x2d95(%rip),%rax
mov    (%rax),%r13d
lea    0xe3a(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    1292 <read_usercode_file+0x112>
mov    0x2d7a(%rip),%rax
mov    (%rax),%r13d
lea    0xdfb(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    128b <read_usercode_file+0x10b>
mov    0x2d5f(%rip),%rax
mov    (%rax),%r13d
lea    0xd85(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
@.str = [91 x i8] c"Failed to read file \22%s\22. Please check whether it exists and verify your read permission.\0A\00"
@.str.1 = [36 x i8] c"Failed to allocate memory for \22%s\22\0A\00"
@.str.2 = [86 x i8] c"Failed to open file \22%s\22. Please check whether it exists and verify your permission.\0A\00"
define i32 @read_usercode_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
store i32 0, i32* %7
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %66
17: ; preds = %3
%18 = load i32, i32* %8
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %8
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %9
%25 = add nsw i32 %24, 512
%26 = sub nsw i32 %25, 1
%27 = and i32 %26, -512
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %11
%30 = load i8*, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %59
32: ; preds = %17
%33 = load i8*, i8** %11
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = sub i32 %37, %38
%40 = call i32 @memset(i8* %36, i32 0, i32 %39)
%41 = load i32, i32* %8
%42 = load i8*, i8** %11
%43 = load i32, i32* %9
%44 = call i32 @read(i32 %41, i8* %42, i32 %43)
%45 = load i32, i32* %9
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %32
%48 = load i8*, i8** %11
%49 = load i8**, i8*** %5
store i8* %48, i8** %49
%50 = load i32, i32* %10
%51 = load i32*, i32** %6
store i32 %50, i32* %51
br label %58
52: ; preds = %32
%53 = load i32, i32* @errno
store i32 %53, i32* %7
%54 = load i8*, i8** %4
%55 = call i32 @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i8* %54)
%56 = load i8*, i8** %11
%57 = call i32 @free(i8* %56)
br label %58
58: ; preds = %52, %47
br label %63
59: ; preds = %17
%60 = load i32, i32* @errno
store i32 %60, i32* %7
%61 = load i8*, i8** %4
%62 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %59, %58
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
br label %70
66: ; preds = %3
%67 = load i32, i32* @errno
store i32 %67, i32* %7
%68 = load i8*, i8** %4
%69 = call i32 @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.2, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %66, %63
%71 = load i32, i32* %7
ret i32 %71
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
@.str = [91 x i8] c"Failed to read file \22%s\22. Please check whether it exists and verify your read permission.\0A\00"
@.str.1 = [36 x i8] c"Failed to allocate memory for \22%s\22\0A\00"
@.str.2 = [86 x i8] c"Failed to open file \22%s\22. Please check whether it exists and verify your permission.\0A\00"
define i32 @read_usercode_file(i8* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %34
7: ; preds = %3
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %5, i32 0, i32 %8)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %5, i32 0, i32 %10)
%12 = add nsw i32 %9, 511
%13 = and i32 %12, -512
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %28, label %16
16: ; preds = %7
%17 = sext i32 %9 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = sub i32 %13, %9
%20 = tail call i32 @memset(i8* %18, i32 0, i32 %19)
%21 = tail call i32 @read(i32 %5, i8* %14, i32 %9)
%22 = icmp eq i32 %21, %9
br i1 %22, label %23, label %24
23: ; preds = %16
store i8* %14, i8** %1
store i32 %13, i32* %2
br label %31
24: ; preds = %16
%25 = load i32, i32* @errno
%26 = tail call i32 @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str, i64 0, i64 0), i8* %0)
%27 = tail call i32 @free(i8* %14)
br label %31
28: ; preds = %7
%29 = load i32, i32* @errno
%30 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %23, %24, %28
%32 = phi i32 [ 0, %23 ], [ %25, %24 ], [ %29, %28 ]
%33 = tail call i32 @close(i32 %5)
br label %37
34: ; preds = %3
%35 = load i32, i32* @errno
%36 = tail call i32 @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %37
37: ; preds = %34, %31
%38 = phi i32 [ %32, %31 ], [ %35, %34 ]
ret i32 %38
}