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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int extractFile(FILE *pak, const char *path, const char *filename,
                uint32_t position, uint32_t size) {
  char buffer[1024];
  char filepath[1024];
  FILE *f;
  long pos_backup;
  size_t n;
  snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
  f = fopen(filepath, "wb");
  pos_backup = ftell(pak);
  fseek(pak, position, SEEK_SET);
  while (size > 0) {
    n = (((size) <= (sizeof(buffer))) ? (size) : (sizeof(buffer)));
    fread(buffer, 1, n, pak);
    fwrite(buffer, 1, n, f);
    size -= n;
  }
  fclose(f);
  fseek(pak, pos_backup, SEEK_SET);
  return 0;
}


x86 assembly (clang, -O0):
<extractFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe57(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x828(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x830(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rsi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1288 <extractFile+0x118>
movslq -0x20(%rbp),%rax
cmp    $0x400,%rax
ja     1218 <extractFile+0xa8>
movslq -0x20(%rbp),%rax
mov    %rax,-0x840(%rbp)
jmp    1229 <extractFile+0xb9>
mov    $0x400,%eax
mov    %rax,-0x840(%rbp)
jmp    1229 <extractFile+0xb9>
mov    -0x840(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x838(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
lea    -0x420(%rbp),%rdi
mov    -0x838(%rbp),%rdx
mov    -0x828(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x838(%rbp),%rcx
movslq -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    11ee <extractFile+0x7e>
mov    -0x828(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
mov    -0x830(%rbp),%rsi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %r8d,%ebp
mov    %ecx,%r13d
mov    %rdx,%r8
mov    %rsi,%rcx
mov    %rdi,%r14
lea    0xe69(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <snprintf@plt>
lea    0xe56(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1060 <ftell@plt>
mov    %rax,0x8(%rsp)
movslq %r13d,%rsi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1070 <fseek@plt>
test   %ebp,%ebp
jle    1226 <extractFile+0xb6>
mov    $0x400,%r15d
lea    0x410(%rsp),%r13
xchg   %ax,%ax
mov    %ebp,%ebx
cmp    $0x400,%ebp
cmovae %r15,%rbx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1090 <fwrite@plt>
sub    %ebx,%ebp
jg     11f0 <extractFile+0x80>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
call   1070 <fseek@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
define i32 @extractFile(i32* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca [1024 x i8]
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i64
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%17 = load i8*, i8** %7
%18 = load i8*, i8** %8
%19 = call i32 @snprintf(i8* %16, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %13
%22 = load i32*, i32** %6
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %14
%24 = load i32*, i32** %6
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %24, i64 %26, i32 %27)
br label %29
29: ; preds = %40, %5
%30 = load i32, i32* %10
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %55
32: ; preds = %29
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = icmp ule i64 %34, 1024
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
br label %40
39: ; preds = %32
br label %40
40: ; preds = %39, %36
%41 = phi i64 [ %38, %36 ], [ 1024, %39 ]
store i64 %41, i64* %15
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%43 = load i64, i64* %15
%44 = load i32*, i32** %6
%45 = call i32 @fread(i8* %42, i32 1, i64 %43, i32* %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%47 = load i64, i64* %15
%48 = load i32*, i32** %13
%49 = call i32 @fwrite(i8* %46, i32 1, i64 %47, i32* %48)
%50 = load i64, i64* %15
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = sub i64 %52, %50
%54 = trunc i64 %53 to i32
store i32 %54, i32* %10
br label %29
55: ; preds = %29
%56 = load i32*, i32** %13
%57 = call i32 @fclose(i32* %56)
%58 = load i32*, i32** %6
%59 = load i64, i64* %14
%60 = load i32, i32* @SEEK_SET
%61 = call i32 @fseek(i32* %58, i64 %59, i32 %60)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_SET = global i32 0
define i32 @extractFile(i32* nocapture %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2)
%11 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = call i64 @ftell(i32* %0)
%13 = sext i32 %3 to i64
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @fseek(i32* %0, i64 %13, i32 %14)
%16 = icmp sgt i32 %4, 0
br i1 %16, label %17, label %27
17: ; preds = %5, %17
%18 = phi i32 [ %25, %17 ], [ %4, %5 ]
%19 = zext i32 %18 to i64
%20 = icmp ult i64 %19, 1024
%21 = select i1 %20, i64 %19, i64 1024
%22 = call i32 @fread(i8* %8, i32 1, i64 %21, i32* %0)
%23 = call i32 @fwrite(i8* %8, i32 1, i64 %21, i32* %11)
%24 = trunc i64 %21 to i32
%25 = sub nsw i32 %18, %24
%26 = icmp sgt i32 %25, 0
br i1 %26, label %17, label %27
27: ; preds = %17, %5
%28 = call i32 @fclose(i32* %11)
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %0, i64 %12, i32 %29)
ret i32 0
}