#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t fread(void *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t fwrite(void *, int, unsigned int, int /*<<< orphan*/ *);
void *malloc(unsigned int);
int /*<<< orphan*/ stderr;
int extract(FILE *src_file, unsigned offset, unsigned size, char *out_name) {
  FILE *out_file;
  void *buff;
  size_t n_bytes;
  out_file = fopen(out_name, "wb");
  if (out_file == NULL) {
    fprintf(stderr, "failed to create output file\n");
    return -11;
  }
  buff = malloc(size);
  if (buff == NULL) {
    fprintf(stderr, "failed to allocate memory for chunk\n");
    fclose(out_file);
    return -12;
  }
  fseek(src_file, (long)offset, SEEK_SET);
  n_bytes = fread(buff, 1, size, src_file);
  if (n_bytes != size) {
    fprintf(stderr, "failed to read chunk\n");
    fclose(out_file);
    free(buff);
    return -13;
  }
  n_bytes = fwrite(buff, 1, size, out_file);
  if (n_bytes != size) {
    fprintf(stderr, "failed to write chunk\n");
    fclose(out_file);
    free(buff);
    return -14;
  }
  free(buff);
  fclose(out_file);
  return 0;
}


x86 assembly (clang, -O0):
<extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d6 <extract+0x56>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xfffffff5,-0x4(%rbp)
jmp    12ff <extract+0x17f>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1217 <extract+0x97>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xfffffff4,-0x4(%rbp)
jmp    12ff <extract+0x17f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    %eax,%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%ecx
cmp    %rcx,%rax
je     128a <extract+0x10a>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffff3,-0x4(%rbp)
jmp    12ff <extract+0x17f>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%ecx
cmp    %rcx,%rax
je     12e6 <extract+0x166>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffff2,-0x4(%rbp)
jmp    12ff <extract+0x17f>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r12
lea    0xe6a(%rip),%rsi
mov    %rcx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     121a <extract+0x9a>
mov    %rax,%r15
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1236 <extract+0xb6>
mov    %rax,%r14
mov    %ebx,%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
call   1080 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %ebp,%eax
cmp    %rax,%rbx
jne    125a <extract+0xda>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    %rbx,%rax
jne    1286 <extract+0x106>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12b0 <extract+0x130>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xfffffff5,%eax
jmp    12b0 <extract+0x130>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0xfffffff4,%eax
jmp    12b0 <extract+0x130>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffff3,%eax
jmp    12b0 <extract+0x130>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffff2,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"failed to create output file\0A\00"
@.str.2 = [37 x i8] c"failed to allocate memory for chunk\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [22 x i8] c"failed to read chunk\0A\00"
@.str.4 = [23 x i8] c"failed to write chunk\0A\00"
define i32 @extract(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %9
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -11, i32* %5
br label %72
20: ; preds = %4
%21 = load i32, i32* %8
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32*, i32** %10
%29 = call i32 @fclose(i32* %28)
store i32 -12, i32* %5
br label %72
30: ; preds = %20
%31 = load i32*, i32** %6
%32 = load i32, i32* %7
%33 = zext i32 %32 to i64
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %31, i64 %33, i32 %34)
%36 = load i8*, i8** %11
%37 = load i32, i32* %8
%38 = load i32*, i32** %6
%39 = call i64 @fread(i8* %36, i32 1, i32 %37, i32* %38)
store i64 %39, i64* %12
%40 = load i64, i64* %12
%41 = load i32, i32* %8
%42 = zext i32 %41 to i64
%43 = icmp ne i64 %40, %42
br i1 %43, label %44, label %51
44: ; preds = %30
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32*, i32** %10
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %11
%50 = call i32 @free(i8* %49)
store i32 -13, i32* %5
br label %72
51: ; preds = %30
%52 = load i8*, i8** %11
%53 = load i32, i32* %8
%54 = load i32*, i32** %10
%55 = call i64 @fwrite(i8* %52, i32 1, i32 %53, i32* %54)
store i64 %55, i64* %12
%56 = load i64, i64* %12
%57 = load i32, i32* %8
%58 = zext i32 %57 to i64
%59 = icmp ne i64 %56, %58
br i1 %59, label %60, label %67
60: ; preds = %51
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32*, i32** %10
%64 = call i32 @fclose(i32* %63)
%65 = load i8*, i8** %11
%66 = call i32 @free(i8* %65)
store i32 -14, i32* %5
br label %72
67: ; preds = %51
%68 = load i8*, i8** %11
%69 = call i32 @free(i8* %68)
%70 = load i32*, i32** %10
%71 = call i32 @fclose(i32* %70)
store i32 0, i32* %5
br label %72
72: ; preds = %67, %60, %44, %25, %17
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"failed to create output file\0A\00"
@.str.2 = [37 x i8] c"failed to allocate memory for chunk\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [22 x i8] c"failed to read chunk\0A\00"
@.str.4 = [23 x i8] c"failed to write chunk\0A\00"
define i32 @extract(i32* nocapture %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %40
10: ; preds = %4
%11 = tail call i8* @malloc(i32 %2)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @fclose(i32* %5)
br label %40
17: ; preds = %10
%18 = zext i32 %1 to i64
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %0, i64 %18, i32 %19)
%21 = tail call i64 @fread(i8* %11, i32 1, i32 %2, i32* %0)
%22 = zext i32 %2 to i64
%23 = icmp eq i64 %21, %22
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @fclose(i32* %5)
%28 = tail call i32 @free(i8* %11)
br label %40
29: ; preds = %17
%30 = tail call i64 @fwrite(i8* %11, i32 1, i32 %2, i32* %5)
%31 = icmp eq i64 %30, %21
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @fclose(i32* %5)
%36 = tail call i32 @free(i8* %11)
br label %40
37: ; preds = %29
%38 = tail call i32 @free(i8* %11)
%39 = tail call i32 @fclose(i32* %5)
br label %40
40: ; preds = %37, %32, %24, %13, %7
%41 = phi i32 [ -11, %7 ], [ -12, %13 ], [ -13, %24 ], [ -14, %32 ], [ 0, %37 ]
ret i32 %41
}