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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ kill(char const *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, size_t);
int main() {
  const size_t parts = 5;
  const char *srcFilePath = "babe.jpg";
  FILE *srcFile = fopen(srcFilePath, "r");
  if (!srcFile)
    kill(srcFilePath);
  fseek(srcFile, 0, SEEK_END);
  long long position = ftell(srcFile);
  fseek(srcFile, 0, SEEK_SET);
  long long partSize = (position / parts) + 1;
  char buffer[4096];
  size_t i;
  for (i = 0; i < parts; i++) {
    char name[11];
    sprintf(name, "Part-%lu.jpg", i + 1);
    FILE *currentDestFile = fopen(name, "w");
    if (!currentDestFile)
      kill("Could not create a part");
    size_t writtenBytes = 0;
    while (writtenBytes <= partSize && !feof(srcFile)) {
      size_t readBytes = fread(buffer, 1, 4096, srcFile);
      fwrite(buffer, 1, readBytes, currentDestFile);
      writtenBytes += readBytes;
    }
    fclose(currentDestFile);
  }
  fclose(srcFile);
  printf("Done!");
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x5,-0x10(%rbp)
lea    0xe3f(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ed <main+0x4d>
mov    -0x18(%rbp),%rdi
call   1080 <kill@plt>
mov    -0x20(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rax
mov    $0x5,%ecx
xor    %edx,%edx
div    %rcx
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x1038(%rbp)
cmpq   $0x5,-0x1038(%rbp)
05
jae    136e <main+0x1ce>
lea    -0x1043(%rbp),%rdi
mov    -0x1038(%rbp),%rdx
add    $0x1,%rdx
lea    0xda1(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x1043(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x1050(%rbp)
cmpq   $0x0,-0x1050(%rbp)
jne    12a3 <main+0x103>
lea    0xd7c(%rip),%rdi
call   1080 <kill@plt>
movq   $0x0,-0x1058(%rbp)
mov    -0x1058(%rbp),%rcx
xor    %eax,%eax
cmp    -0x30(%rbp),%rcx
mov    %al,-0x1061(%rbp)
ja     12de <main+0x13e>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1061(%rbp)
mov    -0x1061(%rbp),%al
test   $0x1,%al
jne    12f1 <main+0x151>
jmp    134b <main+0x1ab>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1060(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x1060(%rbp),%rdx
mov    -0x1050(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x1060(%rbp),%rax
add    -0x1058(%rbp),%rax
mov    %rax,-0x1058(%rbp)
jmp    12ae <main+0x10e>
mov    -0x1050(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1038(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1038(%rbp)
jmp    1243 <main+0xa3>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xcb4(%rip),%rdi
call   1050 <printf@plt>
mov    0x2c46(%rip),%rax
mov    (%rax),%eax
add    $0x1070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
lea    0xe48(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d8 <main+0x38>
lea    0xe2d(%rip),%rdi
call   1080 <kill@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r14
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
movabs $0xcccccccccccccccd,%rcx
cc cc cc
mov    %r14,%rax
mul    %rcx
mov    %rdx,%r15
shr    $0x2,%r15
add    $0x1,%r15
lea    0x20(%rsp),%r12
jmp    1247 <main+0xa7>
nopl   (%rax)
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rbp
cmp    $0x5,%rbp
je     12d4 <main+0x134>
add    $0x1,%rbp
lea    0x15(%rsp),%r14
mov    %r14,%rdi
lea    0xdb1(%rip),%rsi
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdx
call   10b0 <sprintf@plt>
mov    %r14,%rdi
lea    0xda7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    128a <main+0xea>
lea    0xd95(%rip),%rdi
call   1080 <kill@plt>
xor    %r14d,%r14d
nopl   (%rax)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1230 <main+0x90>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbp,%rcx
call   10c0 <fwrite@plt>
add    %r13,%r14
cmp    %r15,%r14
jbe    1290 <main+0xf0>
jmp    1230 <main+0x90>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd4f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"babe.jpg\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Part-%lu.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [24 x i8] c"Could not create a part\00"
@.str.5 = [6 x i8] c"Done!\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca [11 x i8]
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %1
store i64 5, i64* %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp ne i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %0
%18 = load i8*, i8** %3
%19 = call i32 @kill(i8* %18)
br label %20
20: ; preds = %17, %0
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %4
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %5
%26 = load i32*, i32** %4
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i64, i64* %5
%30 = udiv i64 %29, 5
%31 = add i64 %30, 1
store i64 %31, i64* %6
store i64 0, i64* %8
br label %32
32: ; preds = %72, %20
%33 = load i64, i64* %8
%34 = icmp ult i64 %33, 5
br i1 %34, label %35, label %75
35: ; preds = %32
%36 = getelementptr inbounds [11 x i8], [11 x i8]* %9, i64 0, i64 0
%37 = load i64, i64* %8
%38 = add i64 %37, 1
%39 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %38)
%40 = getelementptr inbounds [11 x i8], [11 x i8]* %9, i64 0, i64 0
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %41, i32** %10
%42 = load i32*, i32** %10
%43 = icmp ne i32* %42, null
br i1 %43, label %46, label %44
44: ; preds = %35
%45 = call i32 @kill(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %35
store i64 0, i64* %11
br label %47
47: ; preds = %58, %46
%48 = load i64, i64* %11
%49 = load i64, i64* %6
%50 = icmp ule i64 %48, %49
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32*, i32** %4
%53 = call i32 @feof(i32* %52)
%54 = icmp ne i32 %53, 0
%55 = xor i1 %54, true
br label %56
56: ; preds = %51, %47
%57 = phi i1 [ false, %47 ], [ %55, %51 ]
br i1 %57, label %58, label %69
58: ; preds = %56
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%60 = load i32*, i32** %4
%61 = call i64 @fread(i8* %59, i32 1, i32 4096, i32* %60)
store i64 %61, i64* %12
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%63 = load i64, i64* %12
%64 = load i32*, i32** %10
%65 = call i32 @fwrite(i8* %62, i32 1, i64 %63, i32* %64)
%66 = load i64, i64* %12
%67 = load i64, i64* %11
%68 = add i64 %67, %66
store i64 %68, i64* %11
br label %47
69: ; preds = %56
%70 = load i32*, i32** %10
%71 = call i32 @fclose(i32* %70)
br label %72
72: ; preds = %69
%73 = load i64, i64* %8
%74 = add i64 %73, 1
store i64 %74, i64* %8
br label %32
75: ; preds = %32
%76 = load i32*, i32** %4
%77 = call i32 @fclose(i32* %76)
%78 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* @EXIT_SUCCESS
ret i32 %79
}

LLVM IR (-O2):
@.str = [9 x i8] c"babe.jpg\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [13 x i8] c"Part-%lu.jpg\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [24 x i8] c"Could not create a part\00"
@.str.5 = [6 x i8] c"Done!\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = alloca [11 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @kill(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = udiv i64 %10, 5
%14 = add nuw nsw i64 %13, 1
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%16 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
br label %17
17: ; preds = %7, %35
%18 = phi i64 [ 0, %7 ], [ %19, %35 ]
%19 = add nuw nsw i64 %18, 1
%20 = call i32 @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %19)
%21 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @kill(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
br label %26
26: ; preds = %25, %30
%27 = phi i64 [ %33, %30 ], [ 0, %25 ]
%28 = tail call i32 @feof(i32* %3)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = call i64 @fread(i8* %15, i32 1, i32 4096, i32* %3)
%32 = call i32 @fwrite(i8* %15, i32 1, i64 %31, i32* %21)
%33 = add i64 %31, %27
%34 = icmp ugt i64 %33, %14
br i1 %34, label %35, label %26
35: ; preds = %30, %26
%36 = tail call i32 @fclose(i32* %21)
%37 = icmp eq i64 %19, 5
br i1 %37, label %38, label %17
38: ; preds = %35
%39 = tail call i32 @fclose(i32* %3)
%40 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* @EXIT_SUCCESS
ret i32 %41
}