#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 int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ copy_chunked(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int,
                                int);
int /*<<< orphan*/ fatal(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void split_file(FILE *in_file, char *filename, int64_t offset) {
  int path_len = strlen(filename);
  char *kernel_name = malloc(path_len + 1 + 7);
  char *image_name = malloc(path_len + 1 + 6);
  strncpy(kernel_name, filename, path_len);
  strncpy(kernel_name + path_len, ".kernel", 7);
  kernel_name[path_len + 7] = 0;
  strncpy(image_name, filename, path_len);
  strncpy(image_name + path_len, ".image", 6);
  image_name[path_len + 6] = 0;
  printf("Kernel path: %s\nImage path: %s\n", kernel_name, image_name);
  FILE *kernel_file = fopen(kernel_name, "wb");
  if (!kernel_file)
    fatal("Could not open kernel file for writing: %s\n", kernel_name);
  copy_chunked(in_file, kernel_file, 0, offset);
  fclose(kernel_file);
  FILE *image_file = fopen(image_name, "wb");
  if (!image_file)
    fatal("Could not open image file for writng: %s\n", image_name);
  copy_chunked(in_file, image_file, offset + 8, -1);
  fclose(image_file);
  free(kernel_name);
  free(image_name);
}


x86 assembly (clang, -O0):
<split_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
add    $0x7,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
add    $0x6,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xe0b(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x7,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
lea    0xdd6(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x6,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdb3(%rip),%rdi
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1290 <split_file+0x100>
mov    -0x20(%rbp),%rsi
lea    0xda7(%rip),%rdi
call   10b0 <fatal@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%ecx
xor    %edx,%edx
call   1050 <copy_chunked@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12da <split_file+0x14a>
mov    -0x28(%rbp),%rsi
lea    0xd89(%rip),%rdi
call   10b0 <fatal@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x8,%edx
mov    $0xffffffff,%ecx
call   1050 <copy_chunked@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
lea    0x8(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,%r12
movslq %ebp,%rbp
lea    0x7(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,%r13
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
lea    (%r12,%rbp,1),%rdi
lea    0xe20(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x7(%r12,%rbp,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
add    %rbp,%rdi
lea    0xdfe(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
movb   $0x0,0x6(%r13,%rbp,1)
lea    0xdee(%rip),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1080 <printf@plt>
lea    0xdfc(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1252 <split_file+0xc2>
lea    0xde8(%rip),%rdi
mov    %r12,%rsi
call   10b0 <fatal@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    %r15d,%ecx
call   1050 <copy_chunked@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xdbe(%rip),%rsi
mov    %r13,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1290 <split_file+0x100>
lea    0xdd6(%rip),%rdi
mov    %r13,%rsi
call   10b0 <fatal@plt>
add    $0x8,%r15d
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    $0xffffffff,%ecx
call   1050 <copy_chunked@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [8 x i8] c".kernel\00"
@.str.1 = [7 x i8] c".image\00"
@.str.2 = [32 x i8] c"Kernel path: %s\0AImage path: %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [44 x i8] c"Could not open kernel file for writing: %s\0A\00"
@.str.5 = [42 x i8] c"Could not open image file for writng: %s\0A\00"
define void @split_file(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
%16 = add nsw i32 %15, 7
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %8
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = add nsw i32 %19, 6
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %8
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = call i32 @strncpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %8
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = call i32 @strncpy(i8* %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%31 = load i8*, i8** %8
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %9
%37 = load i8*, i8** %5
%38 = load i32, i32* %7
%39 = call i32 @strncpy(i8* %36, i8* %37, i32 %38)
%40 = load i8*, i8** %9
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = call i32 @strncpy(i8* %43, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%45 = load i8*, i8** %9
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %8
%51 = load i8*, i8** %9
%52 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %50, i8* %51)
%53 = load i8*, i8** %8
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %54, i32** %10
%55 = load i32*, i32** %10
%56 = icmp ne i32* %55, null
br i1 %56, label %60, label %57
57: ; preds = %3
%58 = load i8*, i8** %8
%59 = call i32 @fatal(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %57, %3
%61 = load i32*, i32** %4
%62 = load i32*, i32** %10
%63 = load i32, i32* %6
%64 = call i32 @copy_chunked(i32* %61, i32* %62, i32 0, i32 %63)
%65 = load i32*, i32** %10
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %9
%68 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %68, i32** %11
%69 = load i32*, i32** %11
%70 = icmp ne i32* %69, null
br i1 %70, label %74, label %71
71: ; preds = %60
%72 = load i8*, i8** %9
%73 = call i32 @fatal(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %71, %60
%75 = load i32*, i32** %4
%76 = load i32*, i32** %11
%77 = load i32, i32* %6
%78 = add nsw i32 %77, 8
%79 = call i32 @copy_chunked(i32* %75, i32* %76, i32 %78, i32 -1)
%80 = load i32*, i32** %11
%81 = call i32 @fclose(i32* %80)
%82 = load i8*, i8** %8
%83 = call i32 @free(i8* %82)
%84 = load i8*, i8** %9
%85 = call i32 @free(i8* %84)
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c".kernel\00"
@.str.1 = [7 x i8] c".image\00"
@.str.2 = [32 x i8] c"Kernel path: %s\0AImage path: %s\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [44 x i8] c"Could not open kernel file for writing: %s\0A\00"
@.str.5 = [42 x i8] c"Could not open image file for writng: %s\0A\00"
define void @split_file(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 8
%6 = tail call i8* @malloc(i32 %5)
%7 = add nsw i32 %4, 7
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @strncpy(i8* %6, i8* %1, i32 %4)
%10 = sext i32 %4 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = tail call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%13 = sext i32 %7 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
store i8 0, i8* %14
%15 = tail call i32 @strncpy(i8* %8, i8* %1, i32 %4)
%16 = getelementptr inbounds i8, i8* %8, i64 %10
%17 = tail call i32 @strncpy(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%18 = add nsw i32 %4, 6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %8, i64 %19
store i8 0, i8* %20
%21 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %8)
%22 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = tail call i32 @fatal(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %26
26: ; preds = %24, %3
%27 = tail call i32 @copy_chunked(i32* %0, i32* %22, i32 0, i32 %2)
%28 = tail call i32 @fclose(i32* %22)
%29 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = tail call i32 @fatal(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %8)
br label %33
33: ; preds = %31, %26
%34 = add nsw i32 %2, 8
%35 = tail call i32 @copy_chunked(i32* %0, i32* %29, i32 %34, i32 -1)
%36 = tail call i32 @fclose(i32* %29)
%37 = tail call i32 @free(i8* %6)
%38 = tail call i32 @free(i8* %8)
ret void
}