#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*/ abort();
int /*<<< orphan*/ banner();
int /*<<< orphan*/ eccedc_init();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcasecmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ unecmify(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  FILE *fin, *fout;
  char *infilename;
  char *outfilename;
  banner();
  eccedc_init();
  if ((argc != 2) && (argc != 3)) {
    fprintf(stderr, "usage: %s ecmfile [outputfile]\n", argv[0]);
    return 1;
  }
  infilename = argv[1];
  if (strlen(infilename) < 5) {
    fprintf(stderr, "filename '%s' is too short\n", infilename);
    return 1;
  }
  if (strcasecmp(infilename + strlen(infilename) - 4, ".ecm")) {
    fprintf(stderr, "filename must end in .ecm\n");
    return 1;
  }
  if (argc == 3) {
    outfilename = argv[2];
  } else {
    outfilename = malloc(strlen(infilename) - 3);
    if (!outfilename)
      abort();
    memcpy(outfilename, infilename, strlen(infilename) - 4);
    outfilename[strlen(infilename) - 4] = 0;
  }
  fprintf(stderr, "Decoding %s to %s.\n", infilename, outfilename);
  fin = fopen(infilename, "rb");
  if (!fin) {
    perror(infilename);
    return 1;
  }
  fout = fopen(outfilename, "wb");
  if (!fout) {
    perror(outfilename);
    fclose(fin);
    return 1;
  }
  unecmify(fin, fout);
  fclose(fout);
  fclose(fin);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <banner@plt>
mov    $0x0,%al
call   1090 <eccedc_init@plt>
cmpl   $0x2,-0x8(%rbp)
je     1222 <main+0x62>
cmpl   $0x3,-0x8(%rbp)
je     1222 <main+0x62>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x244>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x5,%eax
jge    1267 <main+0xa7>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x244>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xdb0(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
je     12be <main+0xfe>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x244>
cmpl   $0x3,-0x8(%rbp)
jne    12d9 <main+0x119>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    134e <main+0x18e>
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
sub    $0x3,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1302 <main+0x142>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
sub    $0x4,%edx
call   10a0 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xcf8(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    13a1 <main+0x1e1>
mov    -0x28(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x244>
mov    -0x30(%rbp),%rdi
lea    0xcc7(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    13de <main+0x21e>
mov    -0x30(%rbp),%rdi
call   10e0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1404 <main+0x244>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10c0 <unecmify@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <banner@plt>
xor    %eax,%eax
call   1090 <eccedc_init@plt>
mov    %ebp,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    120a <main+0x4a>
mov    0x8(%rbx),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
cmp    $0x4,%eax
jg     1234 <main+0x74>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %r15,%rdx
jmp    121d <main+0x5d>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xde3(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xdef(%rip),%rsi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     1270 <main+0xb0>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1224 <main+0x64>
cmp    $0x3,%ebp
jne    127b <main+0xbb>
mov    0x10(%rbx),%r14
jmp    12bd <main+0xfd>
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    -0x3(%rax),%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     134c <main+0x18c>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    -0x4(%rax),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <memcpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x4(%rax,%r14,1)
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xd8f(%rip),%rsi
mov    %r15,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     132a <main+0x16a>
mov    %rax,%rbx
lea    0xd7b(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1337 <main+0x177>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
call   10c0 <unecmify@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1229 <main+0x69>
mov    %r15,%rdi
call   10e0 <perror@plt>
jmp    1224 <main+0x64>
mov    %r14,%rdi
call   10e0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1224 <main+0x64>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s ecmfile [outputfile]\0A\00"
@.str.1 = [28 x i8] c"filename '%s' is too short\0A\00"
@.str.2 = [5 x i8] c".ecm\00"
@.str.3 = [27 x i8] c"filename must end in .ecm\0A\00"
@.str.4 = [20 x i8] c"Decoding %s to %s.\0A\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @banner()
%11 = call i32 (...) @eccedc_init()
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %105
23: ; preds = %14, %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = call i32 @strlen(i8* %27)
%29 = icmp slt i32 %28, 5
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %8
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 1, i32* %3
br label %105
34: ; preds = %23
%35 = load i8*, i8** %8
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 -4
%41 = call i64 @strcasecmp(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %105
46: ; preds = %34
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, 3
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
store i8* %52, i8** %9
br label %75
53: ; preds = %46
%54 = load i8*, i8** %8
%55 = call i32 @strlen(i8* %54)
%56 = sub nsw i32 %55, 3
%57 = call i8* @malloc(i32 %56)
store i8* %57, i8** %9
%58 = load i8*, i8** %9
%59 = icmp ne i8* %58, null
br i1 %59, label %62, label %60
60: ; preds = %53
%61 = call i32 (...) @abort()
unreachable
62: ; preds = %53
%63 = load i8*, i8** %9
%64 = load i8*, i8** %8
%65 = load i8*, i8** %8
%66 = call i32 @strlen(i8* %65)
%67 = sub nsw i32 %66, 4
%68 = call i32 @memcpy(i8* %63, i8* %64, i32 %67)
%69 = load i8*, i8** %9
%70 = load i8*, i8** %8
%71 = call i32 @strlen(i8* %70)
%72 = sub nsw i32 %71, 4
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %69, i64 %73
store i8 0, i8* %74
br label %75
75: ; preds = %62, %49
%76 = load i32, i32* @stderr
%77 = load i8*, i8** %8
%78 = load i8*, i8** %9
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %77, i8* %78)
%80 = load i8*, i8** %8
%81 = call i32* @fopen(i8* %80, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %81, i32** %6
%82 = load i32*, i32** %6
%83 = icmp ne i32* %82, null
br i1 %83, label %87, label %84
84: ; preds = %75
%85 = load i8*, i8** %8
%86 = call i32 @perror(i8* %85)
store i32 1, i32* %3
br label %105
87: ; preds = %75
%88 = load i8*, i8** %9
%89 = call i32* @fopen(i8* %88, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %89, i32** %7
%90 = load i32*, i32** %7
%91 = icmp ne i32* %90, null
br i1 %91, label %97, label %92
92: ; preds = %87
%93 = load i8*, i8** %9
%94 = call i32 @perror(i8* %93)
%95 = load i32*, i32** %6
%96 = call i32 @fclose(i32* %95)
store i32 1, i32* %3
br label %105
97: ; preds = %87
%98 = load i32*, i32** %6
%99 = load i32*, i32** %7
%100 = call i32 @unecmify(i32* %98, i32* %99)
%101 = load i32*, i32** %7
%102 = call i32 @fclose(i32* %101)
%103 = load i32*, i32** %6
%104 = call i32 @fclose(i32* %103)
store i32 0, i32* %3
br label %105
105: ; preds = %97, %92, %84, %43, %30, %17
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"usage: %s ecmfile [outputfile]\0A\00"
@.str.1 = [28 x i8] c"filename '%s' is too short\0A\00"
@.str.2 = [5 x i8] c".ecm\00"
@.str.3 = [27 x i8] c"filename must end in .ecm\0A\00"
@.str.4 = [20 x i8] c"Decoding %s to %s.\0A\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @banner()
%4 = tail call i32 (...) @eccedc_init()
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %67
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @strlen(i8* %13)
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %67
19: ; preds = %11
%20 = tail call i32 @strlen(i8* %13)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %13, i64 %21
%23 = getelementptr inbounds i8, i8* %22, i64 -4
%24 = tail call i64 @strcasecmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %67
29: ; preds = %19
%30 = icmp eq i32 %0, 3
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
br label %49
34: ; preds = %29
%35 = tail call i32 @strlen(i8* %13)
%36 = add nsw i32 %35, -3
%37 = tail call i8* @malloc(i32 %36)
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = tail call i32 (...) @abort()
unreachable
41: ; preds = %34
%42 = tail call i32 @strlen(i8* %13)
%43 = add nsw i32 %42, -4
%44 = tail call i32 @memcpy(i8* %37, i8* %13, i32 %43)
%45 = tail call i32 @strlen(i8* %13)
%46 = add nsw i32 %45, -4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %37, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %41, %31
%50 = phi i8* [ %33, %31 ], [ %37, %41 ]
%51 = load i32, i32* @stderr
%52 = tail call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %13, i8* %50)
%53 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp eq i32* %53, null
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = tail call i32 @perror(i8* %13)
br label %67
57: ; preds = %49
%58 = tail call i32* @fopen(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = tail call i32 @perror(i8* %50)
%62 = tail call i32 @fclose(i32* %53)
br label %67
63: ; preds = %57
%64 = tail call i32 @unecmify(i32* %53, i32* %58)
%65 = tail call i32 @fclose(i32* %58)
%66 = tail call i32 @fclose(i32* %53)
br label %67
67: ; preds = %63, %60, %55, %26, %16, %7
%68 = phi i32 [ 1, %7 ], [ 1, %16 ], [ 1, %26 ], [ 0, %63 ], [ 1, %60 ], [ 1, %55 ]
ret i32 %68
}