#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 decompress(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s script.d ...\n", argv[0]);
    exit(1);
  }
  while (*(++argv)) {
    char *infile = *argv;
    char *outfile;
    int infile_len = strlen(infile);
    int success = -1;
    printf("Uncompressing %s... ", infile);
    FILE *in;
    if ((in = fopen(*argv, "rb")) == NULL) {
      perror("");
    } else {
      FILE *out;
      outfile = malloc(infile_len + 1);
      strcpy(outfile, infile);
      outfile[infile_len - 1]--;
      ;
      if ((out = fopen(outfile, "wb")) == NULL) {
        perror("");
      } else {
        success = decompress(in, out);
        fclose(out);
      }
      free(outfile);
      fclose(in);
    }
    if (success != 0)
      printf("failed!");
    else
      printf("done!");
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11df <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11e4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
cmpq   $0x0,0x8(%rax)
je     132a <main+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1260 <main+0xc0>
lea    0xdc1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ec <main+0x14c>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
add    $0xff,%dl
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12c1 <main+0x121>
lea    0xd60(%rip),%rdi
call   10a0 <perror@plt>
jmp    12da <main+0x13a>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <decompress@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x28(%rbp)
je     1309 <main+0x169>
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1317 <main+0x177>
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    11e4 <main+0x44>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     12df <main+0x12f>
mov    0x8(%rbx),%rbp
test   %rbp,%rbp
je     12ce <main+0x11e>
add    $0x10,%rbx
lea    0xe4e(%rip),%r15
jmp    1218 <main+0x68>
lea    0xe2f(%rip),%rdi
call   10b0 <perror@plt>
nopl   (%rax)
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    (%rbx),%rbp
add    $0x8,%rbx
test   %rbp,%rbp
je     12ce <main+0x11e>
mov    %rbp,%rdi
call   1070 <strlen@plt>
mov    %eax,%r14d
lea    0xdee(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    -0x8(%rbx),%rdi
mov    %r15,%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11e1 <main+0x31>
mov    %rax,%r12
lea    0x1(%r14),%edi
call   1090 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
movslq %r14d,%rax
addb   $0xff,-0x1(%rax,%r13,1)
mov    %r13,%rdi
lea    0xdbe(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ba <main+0x10a>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   10d0 <decompress@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
lea    0xd8f(%rip),%rdi
test   %r14d,%r14d
jne    11f0 <main+0x40>
jmp    11f7 <main+0x47>
lea    0xd56(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1295 <main+0xe5>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd17(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Usage: %s script.d ...\0A\00"
@.str.1 = [21 x i8] c"Uncompressing %s... \00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [8 x i8] c"failed!\00"
@.str.6 = [6 x i8] c"done!\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
br label %21
21: ; preds = %76, %20
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i32 1
store i8** %23, i8*** %5
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %78
26: ; preds = %21
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %8
store i32 -1, i32* %9
%31 = load i8*, i8** %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i8**, i8*** %5
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %35, i32** %10
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %69
39: ; preds = %26
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = load i8*, i8** %6
%45 = call i32 @strcpy(i8* %43, i8* %44)
%46 = load i8*, i8** %7
%47 = load i32, i32* %8
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = load i8, i8* %50
%52 = add i8 %51, -1
store i8 %52, i8* %50
%53 = load i8*, i8** %7
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %11
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %58
56: ; preds = %39
%57 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %64
58: ; preds = %39
%59 = load i32*, i32** %10
%60 = load i32*, i32** %11
%61 = call i32 @decompress(i32* %59, i32* %60)
store i32 %61, i32* %9
%62 = load i32*, i32** %11
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %58, %56
%65 = load i8*, i8** %7
%66 = call i32 @free(i8* %65)
%67 = load i32*, i32** %10
%68 = call i32 @fclose(i32* %67)
br label %69
69: ; preds = %64, %37
%70 = load i32, i32* %9
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %69
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %76
74: ; preds = %69
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %76
76: ; preds = %74, %72
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %21
78: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Usage: %s script.d ...\0A\00"
@.str.1 = [21 x i8] c"Uncompressing %s... \00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [8 x i8] c"failed!\00"
@.str.6 = [6 x i8] c"done!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %51, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4, %44
%13 = phi i8* [ %49, %44 ], [ %6, %4 ]
%14 = phi i8** [ %48, %44 ], [ %5, %4 ]
%15 = tail call i32 @strlen(i8* %13)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%17 = load i8*, i8** %14
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %44
22: ; preds = %12
%23 = add nsw i32 %15, 1
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 @strcpy(i8* %24, i8* %13)
%26 = add nsw i32 %15, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
%30 = add i8 %29, -1
store i8 %30, i8* %28
%31 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %38
35: ; preds = %22
%36 = tail call i32 @decompress(i32* %18, i32* %31)
%37 = tail call i32 @fclose(i32* %31)
br label %38
38: ; preds = %33, %35
%39 = phi i32 [ -1, %33 ], [ %36, %35 ]
%40 = tail call i32 @free(i8* %24)
%41 = tail call i32 @fclose(i32* %18)
%42 = icmp eq i32 %39, 0
%43 = select i1 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
br label %44
44: ; preds = %38, %20
%45 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), %20 ], [ %43, %38 ]
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %45)
%47 = tail call i32 @putchar(i32 10)
%48 = getelementptr inbounds i8*, i8** %14, i64 1
%49 = load i8*, i8** %48
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %12
51: ; preds = %44, %4
ret i32 0
}