#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 Z_OK;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, unsigned long,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, unsigned long,
                          int /*<<< orphan*/ *);
unsigned char *malloc(unsigned long);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ terminate(char *, ...);
int uncompress(unsigned char *, unsigned long *, unsigned char *,
               unsigned long);
void unpack_resource(FILE *fd, char *name, unsigned long len, int pos,
                     unsigned long clen) {
  unsigned char *data, *cdata;
  FILE *ofd;
  int r;
  cdata = malloc(clen);
  data = malloc(len + 16);
  memset(cdata, 0, clen);
  memset(data, 0, len + 16);
  fseek(fd, pos, SEEK_SET);
  fread(cdata, 1, clen, fd);
  r = uncompress(data, &len, cdata, clen);
  if (r != Z_OK) {
    terminate("decompression error");
  }
  ofd = fopen(name, "wb");
  if (ofd == NULL)
    terminate("Cannot open output file %s", name);
  fwrite(data, 1, len, ofd);
  fclose(ofd);
  free(cdata);
  free(data);
}


x86 assembly (clang, -O0):
<unpack_resource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x10,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x10,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x18(%rbp),%rsi
call   1060 <uncompress@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%eax
je     125e <unpack_resource+0xbe>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1090 <terminate@plt>
mov    -0x10(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    128f <unpack_resource+0xef>
mov    -0x10(%rbp),%rsi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1090 <terminate@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpack_resource>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbx
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,0x10(%rsp)
mov    %rdi,%r13
mov    %rdx,0x8(%rsp)
mov    %r8,%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
add    $0x10,%r15
mov    %r15,%rdi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1070 <memset@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1070 <memset@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    %ebp,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <uncompress@plt>
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%eax
je     1249 <unpack_resource+0xa9>
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1090 <terminate@plt>
lea    0xdc4(%rip),%rsi
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1276 <unpack_resource+0xd6>
lea    0xdab(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1090 <terminate@plt>
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@Z_OK = global i32 0
@.str = [20 x i8] c"decompression error\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [27 x i8] c"Cannot open output file %s\00"
define void @unpack_resource(i32* %0, i8* %1, i64 %2, i32 %3, i64 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i64 %4, i64* %10
%15 = load i64, i64* %10
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %12
%17 = load i64, i64* %8
%18 = add i64 %17, 16
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %11
%20 = load i8*, i8** %12
%21 = load i64, i64* %10
%22 = call i32 @memset(i8* %20, i32 0, i64 %21)
%23 = load i8*, i8** %11
%24 = load i64, i64* %8
%25 = add i64 %24, 16
%26 = call i32 @memset(i8* %23, i32 0, i64 %25)
%27 = load i32*, i32** %6
%28 = load i32, i32* %9
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %27, i32 %28, i32 %29)
%31 = load i8*, i8** %12
%32 = load i64, i64* %10
%33 = load i32*, i32** %6
%34 = call i32 @fread(i8* %31, i32 1, i64 %32, i32* %33)
%35 = load i8*, i8** %11
%36 = load i8*, i8** %12
%37 = load i64, i64* %10
%38 = call i32 @uncompress(i8* %35, i64* %8, i8* %36, i64 %37)
store i32 %38, i32* %14
%39 = load i32, i32* %14
%40 = load i32, i32* @Z_OK
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %5
%43 = call i32 (i8*, ...) @terminate(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %5
%45 = load i8*, i8** %7
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %46, i32** %13
%47 = load i32*, i32** %13
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i8*, i8** %7
%51 = call i32 (i8*, ...) @terminate(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %44
%53 = load i8*, i8** %11
%54 = load i64, i64* %8
%55 = load i32*, i32** %13
%56 = call i32 @fwrite(i8* %53, i32 1, i64 %54, i32* %55)
%57 = load i32*, i32** %13
%58 = call i32 @fclose(i32* %57)
%59 = load i8*, i8** %12
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %11
%62 = call i32 @free(i8* %61)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@Z_OK = global i32 0
@.str = [20 x i8] c"decompression error\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [27 x i8] c"Cannot open output file %s\00"
define void @unpack_resource(i32* nocapture %0, i8* %1, i64 %2, i32 %3, i64 %4) {
%6 = alloca i64
store i64 %2, i64* %6
%7 = tail call i8* @malloc(i64 %4)
%8 = add i64 %2, 16
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i32 @memset(i8* %7, i32 0, i64 %4)
%11 = tail call i32 @memset(i8* %9, i32 0, i64 %8)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %0, i32 %3, i32 %12)
%14 = tail call i32 @fread(i8* %7, i32 1, i64 %4, i32* %0)
%15 = call i32 @uncompress(i8* %9, i64* %6, i8* %7, i64 %4)
%16 = load i32, i32* @Z_OK
%17 = icmp eq i32 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %5
%19 = call i32 (i8*, ...) @terminate(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %5
%21 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @terminate(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %25
25: ; preds = %23, %20
%26 = load i64, i64* %6
%27 = call i32 @fwrite(i8* %9, i32 1, i64 %26, i32* %21)
%28 = call i32 @fclose(i32* %21)
%29 = call i32 @free(i8* %7)
%30 = call i32 @free(i8* %9)
ret void
}