#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*/ txtPathMod;
typedef int /*<<< orphan*/ outPathMod;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ModifyExt(char const *, char *);
int /*<<< orphan*/ PrintUsage();
int /*<<< orphan*/ RemoveExt(char const *);
int Unpack(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strncpy(char const *, char const *, int);
int main(int argc, char *argv[]) {
  typedef enum {
    OPERATION_NONE,
    OPERATION_UNPACK,
    OPERATION_REPACK,
  } OPERATION;
  puts("CPKTool 1.0\n"
       "Developed by Luciano Ciccariello "
       "(http://github.com/Xeeynamo/SMTPersona)\n");
  if (argc > 2) {
    const char *cpkPath = NULL;
    const char *txtPath = NULL;
    const char *outPath = NULL;
    char txtPathMod[260];
    char outPathMod[260];
    OPERATION op = OPERATION_NONE;
    int i;
    for (i = 1; i < argc; i++) {
      if (argv[i][0] == '-') {
        switch (argv[i][1]) {
        case 'd':
          cpkPath = argv[++i];
          op = OPERATION_UNPACK;
          break;
        case 'c':
          cpkPath = argv[++i];
          op = OPERATION_REPACK;
          break;
        case 'o':
          txtPath = argv[++i];
          break;
        }
      }
    }
    if (cpkPath != NULL) {
      FILE *fCpk = NULL, *fTxt = NULL;
      if (txtPath == NULL) {
        txtPath = txtPathMod;
        strncpy(txtPath, cpkPath, sizeof(txtPathMod));
        ModifyExt(txtPath, "TXT");
      }
      if (outPath == NULL) {
        outPath = outPathMod;
        strncpy(outPath, cpkPath, sizeof(outPathMod));
        RemoveExt(outPath);
      }
      switch (op) {
      case OPERATION_UNPACK:
        fCpk = fopen(cpkPath, "rb");
        fTxt = fopen(txtPath, "wb");
        return Unpack(fCpk, fTxt, outPath);
      case OPERATION_REPACK:
        fCpk = fopen(cpkPath, "wb");
        fTxt = fopen(txtPath, "rb");
        return 0;
      }
      fclose(fCpk);
      fclose(fTxt);
    }
  }
  PrintUsage();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x2,-0x8(%rbp)
jle    1446 <main+0x2c6>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x244(%rbp)
movl   $0x1,-0x248(%rbp)
mov    -0x248(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12ea <main+0x16a>
mov    -0x10(%rbp),%rax
movslq -0x248(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    12d1 <main+0x151>
mov    -0x10(%rbp),%rax
movslq -0x248(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl 0x1(%rax),%eax
mov    %eax,-0x25c(%rbp)
sub    $0x63,%eax
je     1281 <main+0x101>
jmp    122c <main+0xac>
mov    -0x25c(%rbp),%eax
sub    $0x64,%eax
je     1254 <main+0xd4>
jmp    1240 <main+0xc0>
mov    -0x25c(%rbp),%eax
sub    $0x6f,%eax
je     12ae <main+0x12e>
jmp    12cc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    -0x248(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x248(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x244(%rbp)
jmp    12cc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    -0x248(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x248(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x244(%rbp)
jmp    12cc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    -0x248(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x248(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
jmp    12d1 <main+0x151>
jmp    12d6 <main+0x156>
mov    -0x248(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x248(%rbp)
jmp    11db <main+0x5b>
cmpq   $0x0,-0x18(%rbp)
je     1441 <main+0x2c1>
movq   $0x0,-0x250(%rbp)
movq   $0x0,-0x258(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1343 <main+0x1c3>
lea    -0x130(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x104,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   1060 <ModifyExt@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1374 <main+0x1f4>
lea    -0x240(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x104,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <RemoveExt@plt>
mov    -0x244(%rbp),%eax
mov    %eax,-0x260(%rbp)
sub    $0x1,%eax
je     13a2 <main+0x222>
jmp    138e <main+0x20e>
mov    -0x260(%rbp),%eax
sub    $0x2,%eax
je     13ef <main+0x26f>
jmp    1429 <main+0x2a9>
mov    -0x18(%rbp),%rdi
lea    0xcad(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x250(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xc99(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x258(%rbp)
mov    -0x250(%rbp),%rdi
mov    -0x258(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <Unpack@plt>
mov    %eax,-0x4(%rbp)
jmp    1454 <main+0x2d4>
mov    -0x18(%rbp),%rdi
lea    0xc63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x250(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xc49(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x258(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    1454 <main+0x2d4>
mov    -0x250(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x258(%rbp),%rdi
call   1070 <fclose@plt>
jmp    1446 <main+0x2c6>
mov    $0x0,%al
call   1050 <PrintUsage@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe65(%rip),%rdi
call   1040 <puts@plt>
cmp    $0x3,%ebp
jl     12d5 <main+0x155>
xor    %r12d,%r12d
mov    $0x1,%eax
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11d7 <main+0x57>
lea    0x1(%rcx),%eax
mov    0x8(%rbx,%rcx,8),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%eax
cmp    %ebp,%eax
jge    1216 <main+0x96>
movslq %eax,%rcx
mov    (%rbx,%rcx,8),%rdx
cmpb   $0x2d,(%rdx)
jne    11d0 <main+0x50>
movsbl 0x1(%rdx),%edx
cmp    $0x63,%edx
je     1206 <main+0x86>
cmp    $0x6f,%edx
je     11b9 <main+0x39>
cmp    $0x64,%edx
jne    11d0 <main+0x50>
lea    0x1(%rcx),%eax
mov    0x8(%rbx,%rcx,8),%r15
mov    $0x1,%r12d
jmp    11d0 <main+0x50>
lea    0x1(%rcx),%eax
mov    0x8(%rbx,%rcx,8),%r15
mov    $0x2,%r12d
jmp    11d0 <main+0x50>
test   %r15,%r15
je     12d5 <main+0x155>
test   %r14,%r14
jne    124b <main+0xcb>
lea    0x110(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x104,%edx
call   1030 <strncpy@plt>
lea    0xe13(%rip),%rsi
mov    %r14,%rdi
call   1060 <ModifyExt@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x104,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   10a0 <RemoveExt@plt>
cmp    $0x2,%r12d
je     12a5 <main+0x125>
cmp    $0x1,%r12d
jne    12c7 <main+0x147>
lea    0xde1(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xdd2(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1080 <Unpack@plt>
mov    %eax,%ebx
jmp    12de <main+0x15e>
lea    0xdb1(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
lea    0xd9f(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
xor    %ebx,%ebx
jmp    12de <main+0x15e>
xor    %edi,%edi
call   1070 <fclose@plt>
xor    %edi,%edi
call   1070 <fclose@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <PrintUsage@plt>
mov    %ebx,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [86 x i8] c"CPKTool 1.0\0ADeveloped by Luciano Ciccariello (http://github.com/Xeeynamo/SMTPersona)\0A\00"
@.str.1 = [4 x i8] c"TXT\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [3 x i8] c"wb\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 i8*
%9 = alloca [260 x i8]
%10 = alloca [260 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 @puts(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %113
18: ; preds = %2
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i32 0, i32* %11
store i32 1, i32* %12
br label %19
19: ; preds = %65, %18
%20 = load i32, i32* %12
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %68
23: ; preds = %19
%24 = load i8**, i8*** %5
%25 = load i32, i32* %12
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 45
br i1 %32, label %33, label %64
33: ; preds = %23
%34 = load i8**, i8*** %5
%35 = load i32, i32* %12
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
switch i32 %41, label %63 [
i32 100, label %42
i32 99, label %49
i32 111, label %56
]
42: ; preds = %33
%43 = load i8**, i8*** %5
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %12
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %43, i64 %46
%48 = load i8*, i8** %47
store i8* %48, i8** %6
store i32 1, i32* %11
br label %63
49: ; preds = %33
%50 = load i8**, i8*** %5
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %50, i64 %53
%55 = load i8*, i8** %54
store i8* %55, i8** %6
store i32 2, i32* %11
br label %63
56: ; preds = %33
%57 = load i8**, i8*** %5
%58 = load i32, i32* %12
%59 = add nsw i32 %58, 1
store i32 %59, i32* %12
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %57, i64 %60
%62 = load i8*, i8** %61
store i8* %62, i8** %7
br label %63
63: ; preds = %33, %56, %49, %42
br label %64
64: ; preds = %63, %23
br label %65
65: ; preds = %64
%66 = load i32, i32* %12
%67 = add nsw i32 %66, 1
store i32 %67, i32* %12
br label %19
68: ; preds = %19
%69 = load i8*, i8** %6
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %112
71: ; preds = %68
store i32* null, i32** %13
store i32* null, i32** %14
%72 = load i8*, i8** %7
%73 = icmp eq i8* %72, null
br i1 %73, label %74, label %81
74: ; preds = %71
%75 = getelementptr inbounds [260 x i8], [260 x i8]* %9, i64 0, i64 0
store i8* %75, i8** %7
%76 = load i8*, i8** %7
%77 = load i8*, i8** %6
%78 = call i32 @strncpy(i8* %76, i8* %77, i32 260)
%79 = load i8*, i8** %7
%80 = call i32 @ModifyExt(i8* %79, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %81
81: ; preds = %74, %71
%82 = load i8*, i8** %8
%83 = icmp eq i8* %82, null
br i1 %83, label %84, label %91
84: ; preds = %81
%85 = getelementptr inbounds [260 x i8], [260 x i8]* %10, i64 0, i64 0
store i8* %85, i8** %8
%86 = load i8*, i8** %8
%87 = load i8*, i8** %6
%88 = call i32 @strncpy(i8* %86, i8* %87, i32 260)
%89 = load i8*, i8** %8
%90 = call i32 @RemoveExt(i8* %89)
br label %91
91: ; preds = %84, %81
%92 = load i32, i32* %11
switch i32 %92, label %107 [
i32 1, label %93
i32 2, label %102
]
93: ; preds = %91
%94 = load i8*, i8** %6
%95 = call i32* @fopen(i8* %94, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %95, i32** %13
%96 = load i8*, i8** %7
%97 = call i32* @fopen(i8* %96, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %97, i32** %14
%98 = load i32*, i32** %13
%99 = load i32*, i32** %14
%100 = load i8*, i8** %8
%101 = call i32 @Unpack(i32* %98, i32* %99, i8* %100)
store i32 %101, i32* %3
br label %115
102: ; preds = %91
%103 = load i8*, i8** %6
%104 = call i32* @fopen(i8* %103, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %104, i32** %13
%105 = load i8*, i8** %7
%106 = call i32* @fopen(i8* %105, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %106, i32** %14
store i32 0, i32* %3
br label %115
107: ; preds = %91
%108 = load i32*, i32** %13
%109 = call i32 @fclose(i32* %108)
%110 = load i32*, i32** %14
%111 = call i32 @fclose(i32* %110)
br label %112
112: ; preds = %107, %68
br label %113
113: ; preds = %112, %2
%114 = call i32 (...) @PrintUsage()
store i32 0, i32* %3
br label %115
115: ; preds = %113, %102, %93
%116 = load i32, i32* %3
ret i32 %116
}

LLVM IR (-O2):
@.str = [86 x i8] c"CPKTool 1.0\0ADeveloped by Luciano Ciccariello (http://github.com/Xeeynamo/SMTPersona)\0A\00"
@.str.1 = [4 x i8] c"TXT\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [260 x i8]
%4 = alloca [260 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %0, 2
br i1 %6, label %7, label %70
7: ; preds = %2
%8 = getelementptr inbounds [260 x i8], [260 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [260 x i8], [260 x i8]* %4, i64 0, i64 0
br label %10
10: ; preds = %7, %39
%11 = phi i32 [ %44, %39 ], [ 1, %7 ]
%12 = phi i32 [ %42, %39 ], [ 0, %7 ]
%13 = phi i8* [ %41, %39 ], [ null, %7 ]
%14 = phi i8* [ %40, %39 ], [ null, %7 ]
%15 = sext i32 %11 to i64
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 45
br i1 %19, label %20, label %39
20: ; preds = %10
%21 = getelementptr inbounds i8, i8* %17, i64 1
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
switch i32 %23, label %39 [
i32 100, label %24
i32 99, label %29
i32 111, label %34
]
24: ; preds = %20
%25 = add nsw i32 %11, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %1, i64 %26
%28 = load i8*, i8** %27
br label %39
29: ; preds = %20
%30 = add nsw i32 %11, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %1, i64 %31
%33 = load i8*, i8** %32
br label %39
34: ; preds = %20
%35 = add nsw i32 %11, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %1, i64 %36
%38 = load i8*, i8** %37
br label %39
39: ; preds = %10, %20, %34, %29, %24
%40 = phi i8* [ %14, %20 ], [ %14, %34 ], [ %33, %29 ], [ %28, %24 ], [ %14, %10 ]
%41 = phi i8* [ %13, %20 ], [ %38, %34 ], [ %13, %29 ], [ %13, %24 ], [ %13, %10 ]
%42 = phi i32 [ %12, %20 ], [ %12, %34 ], [ 2, %29 ], [ 1, %24 ], [ %12, %10 ]
%43 = phi i32 [ %11, %20 ], [ %35, %34 ], [ %30, %29 ], [ %25, %24 ], [ %11, %10 ]
%44 = add nsw i32 %43, 1
%45 = icmp slt i32 %44, %0
br i1 %45, label %10, label %46
46: ; preds = %39
%47 = icmp eq i8* %40, null
br i1 %47, label %67, label %48
48: ; preds = %46
%49 = icmp eq i8* %41, null
br i1 %49, label %50, label %53
50: ; preds = %48
%51 = call i32 @strncpy(i8* %8, i8* %40, i32 260)
%52 = call i32 @ModifyExt(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %48, %50
%54 = phi i8* [ %8, %50 ], [ %41, %48 ]
%55 = call i32 @strncpy(i8* %9, i8* %40, i32 260)
%56 = call i32 @RemoveExt(i8* %9)
switch i32 %42, label %64 [
i32 1, label %57
i32 2, label %61
]
57: ; preds = %53
%58 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%59 = call i32* @fopen(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @Unpack(i32* %58, i32* %59, i8* %9)
br label %68
61: ; preds = %53
%62 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32* @fopen(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %68
64: ; preds = %53
%65 = call i32 @fclose(i32* null)
%66 = call i32 @fclose(i32* null)
br label %67
67: ; preds = %64, %46
br label %70
68: ; preds = %61, %57
%69 = phi i32 [ %60, %57 ], [ 0, %61 ]
br label %72
70: ; preds = %67, %2
%71 = call i32 (...) @PrintUsage()
br label %72
72: ; preds = %68, %70
%73 = phi i32 [ %69, %68 ], [ 0, %70 ]
ret i32 %73
}