#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, unsigned int, int, int /*<<< orphan*/ *);
char *malloc(unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int, unsigned int, char *);
void extractfile(FILE *src, char *filename, unsigned int offset,
                 unsigned int size) {
  printf("Copying %i bytes from 0x%06x to %s\n", size, offset, filename);
  FILE *des = fopen(filename, "wb");
  if (!des) {
    perror("create file");
    exit(1);
  }
  char *buf = malloc(size);
  if (buf == NULL) {
    perror("malloc");
    exit(1);
  }
  fseek(src, offset, SEEK_SET);
  fread(buf, size, 1, src);
  fwrite(buf, size, 1, des);
  free(buf);
  fclose(des);
}


x86 assembly (clang, -O0):
<extractfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1201 <extractfile+0x61>
lea    0xe35(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122e <extractfile+0x8e>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe46(%rip),%rdi
mov    %ecx,%esi
mov    %rbp,%rcx
call   1060 <printf@plt>
lea    0xe59(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123a <extractfile+0x9a>
mov    %rax,%r15
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1243 <extractfile+0xa3>
mov    %rax,%rbp
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %r12d,%esi
call   1080 <fseek@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10c0 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xde6(%rip),%rdi
jmp    124a <extractfile+0xaa>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Copying %i bytes from 0x%06x to %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [12 x i8] c"create file\00"
@.str.3 = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
define void @extractfile(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = load i8*, i8** %6
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12, i8* %13)
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %4
%20 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %4
%23 = load i32, i32* %8
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %10
%25 = load i8*, i8** %10
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load i32*, i32** %5
%32 = load i32, i32* %7
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %31, i32 %32, i32 %33)
%35 = load i8*, i8** %10
%36 = load i32, i32* %8
%37 = load i32*, i32** %5
%38 = call i32 @fread(i8* %35, i32 %36, i32 1, i32* %37)
%39 = load i8*, i8** %10
%40 = load i32, i32* %8
%41 = load i32*, i32** %9
%42 = call i32 @fwrite(i8* %39, i32 %40, i32 1, i32* %41)
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"Copying %i bytes from 0x%06x to %s\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [12 x i8] c"create file\00"
@.str.3 = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
define void @extractfile(i32* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %2, i8* %1)
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %4
%12 = tail call i8* @malloc(i32 %3)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @fseek(i32* %0, i32 %2, i32 %18)
%20 = tail call i32 @fread(i8* %12, i32 %3, i32 1, i32* %0)
%21 = tail call i32 @fwrite(i8* %12, i32 %3, i32 1, i32* %6)
%22 = tail call i32 @free(i8* %12)
%23 = tail call i32 @fclose(i32* %6)
ret void
}