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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
int strlen(char const *);
const char *load_kernel_source(const char *filename) {
  size_t len = strlen(".") + 1 + strlen(filename) + 1;
  char *fullpath = (char *)malloc(len);
  sprintf(fullpath, "%s/%s", ".", filename);
  fullpath[len] = '\0';
  int fd = open(fullpath, O_RDONLY);
  struct stat st;
  fstat(fd, &st);
  char *buf = (char *)malloc(st.st_size + 1);
  assert(st.st_size == read(fd, buf, st.st_size));
  buf[st.st_size] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<load_kernel_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7d(%rip),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rcx
lea    0xe3d(%rip),%rsi
lea    0xe34(%rip),%rdx
call   1080 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_kernel_source>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
lea    0xe7a(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r12
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe50(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   1080 <sprintf@plt>
movb   $0x0,(%rbx,%r12,1)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
movslq 0x8(%rsp),%rbx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
xor    %edi,%edi
cmp    %eax,%ebx
sete   %dil
call   1050 <assert@plt>
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define i8* @load_kernel_source(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
store i8* %0, i8** %2
%8 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = add nsw i32 %8, 1
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = trunc i64 %15 to i32
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = load i8*, i8** %2
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %4
%23 = load i64, i64* %3
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = call i32 @fstat(i32 %28, %struct.stat* %6)
%30 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %7
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* %5
%38 = load i8*, i8** %7
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @read(i32 %37, i8* %38, i32 %40)
%42 = icmp eq i32 %36, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i8*, i8** %7
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %7
ret i8* %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
define i8* @load_kernel_source(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @strlen(i8* %0)
%5 = add i32 %3, 2
%6 = add i32 %5, %4
%7 = sext i32 %6 to i64
%8 = tail call i64 @malloc(i32 %6)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = getelementptr inbounds i8, i8* %9, i64 %7
store i8 0, i8* %11
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = bitcast %struct.stat* %2 to i8*
%15 = call i32 @fstat(i32 %13, %struct.stat* %2)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @read(i32 %13, i8* %20, i32 %17)
%22 = icmp eq i32 %17, %21
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
%25 = sext i32 %17 to i64
%26 = getelementptr inbounds i8, i8* %20, i64 %25
store i8 0, i8* %26
ret i8* %20
}