#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  FILE *megafile, *output;
  char *extracted_file;
  char name[127];
  char buffer[127];
  char output_name[127];
  int len;
  if (argc != 2) {
    fprintf(stderr,
            "Usage:\n %s <megafile>\n\n"
            "Where <megafile> is the name of a viewlogic megafile\n"
            "whithout any extensions.  The file <megafile>.lib and \n"
            "<megafile>.tbl must exist in the same directory\n",
            argv[0]);
    return 1;
  }
  strcpy(name, argv[1]);
  strcat(name, ".lib");
  megafile = fopen(name, "r");
  if (megafile == NULL) {
    fprintf(stderr, "Error: unable to open magefile `%s' for reading\n", name);
    return (1);
  }
  mkdir(argv[1], 0777);
  while (!feof(megafile)) {
    if (fread(buffer, 0x14, 1, megafile) == 0)
      break;
    buffer[0x14 + 1] = 0;
    sscanf(buffer, "%s %d", name, &len);
    printf("%s:%d\n", name, len);
    extracted_file = malloc(len);
    fread(extracted_file, len, 1, megafile);
    strcpy(output_name, argv[1]);
    strcat(output_name, "/");
    strcat(output_name, name);
    output = fopen(output_name, "wb");
    if (output == NULL) {
      fclose(megafile);
      fprintf(stderr, "Error: unable to open file `%s' for writing\n",
              output_name);
      return 1;
    }
    fwrite(extracted_file, len, 1, output);
    fclose(output);
    fgetc(megafile);
  }
  fclose(megafile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121b <main+0x4b>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1419 <main+0x249>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128c <main+0xbc>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    -0xb0(%rbp),%rdx
lea    0xe40(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1419 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12bc <main+0xec>
jmp    1409 <main+0x239>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x14,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x0,%rax
jne    12e5 <main+0x115>
jmp    1409 <main+0x239>
movb   $0x0,-0x11b(%rbp)
lea    -0x130(%rbp),%rdi
lea    -0xb0(%rbp),%rdx
lea    0xdeb(%rip),%rsi
lea    -0x1b4(%rbp),%rcx
call   10c0 <sscanf@plt>
lea    -0xb0(%rbp),%rsi
mov    -0x1b4(%rbp),%edx
lea    0xdd1(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1b4(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1b4(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
lea    -0x1b0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x1b0(%rbp),%rdi
lea    0xd8a(%rip),%rsi
call   10e0 <strcat@plt>
lea    -0x1b0(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
call   10e0 <strcat@plt>
lea    -0x1b0(%rbp),%rdi
lea    0xd66(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    13da <main+0x20a>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2c27(%rip),%rax
mov    (%rax),%edi
lea    -0x1b0(%rbp),%rdx
lea    0xd35(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1419 <main+0x249>
mov    -0x28(%rbp),%rdi
mov    -0x1b4(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10f0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1080 <fgetc@plt>
jmp    129e <main+0xce>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jne    1246 <main+0x76>
mov    0x8(%r15),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
lea    0xeb3(%rip),%rsi
mov    %rbp,%rdi
call   10e0 <strcat@plt>
lea    0xea9(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     125b <main+0x8b>
mov    %rax,%r13
mov    0x8(%r15),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %r13,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
je     128c <main+0xbc>
mov    %r13,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    127a <main+0xaa>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xda7(%rip),%rsi
jmp    1270 <main+0xa0>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x110(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x14,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     123a <main+0x6a>
movb   $0x0,0x125(%rsp)
mov    %rbp,%r14
mov    %rbp,%rdi
lea    0xe1d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdx
lea    0xc(%rsp),%rcx
call   10c0 <sscanf@plt>
mov    0xc(%rsp),%edx
lea    0xe06(%rip),%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
mov    %r15,%rbx
mov    0x8(%r15),%rsi
lea    0x90(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
lea    0xdc4(%rip),%rsi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
lea    0x10(%rsp),%rsi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
lea    0xdaa(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     139d <main+0x1cd>
mov    %rax,%r15
mov    0xc(%rsp),%esi
mov    %r12,%rdi
mov    $0x1,%edx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   1080 <fgetc@plt>
mov    %r13,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
mov    %r14,%rbp
mov    %rbx,%r15
je     12a0 <main+0xd0>
jmp    123a <main+0x6a>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
lea    0x90(%rsp),%rdx
jmp    1270 <main+0xa0>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [180 x i8] c"Usage:\0A %s <megafile>\0A\0AWhere <megafile> is the name of a viewlogic megafile\0Awhithout any extensions. The file <megafile>.lib and \0A<megafile>.tbl must exist in the same directory\0A\00"
@.str.1 = [5 x i8] c".lib\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [49 x i8] c"Error: unable to open magefile `%s' for reading\0A\00"
@.str.4 = [6 x i8] c"%s %d\00"
@.str.5 = [7 x i8] c"%s:%d\0A\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [3 x i8] c"wb\00"
@.str.8 = [45 x i8] c"Error: unable to open file `%s' for writing\0A\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 [127 x i8]
%10 = alloca [127 x i8]
%11 = alloca [127 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([180 x i8], [180 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %99
21: ; preds = %2
%22 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strcpy(i8* %22, i8* %25)
%27 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %21
%34 = load i32, i32* @stderr
%35 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i32 1, i32* %3
br label %99
37: ; preds = %21
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @mkdir(i8* %40, i32 511)
br label %42
42: ; preds = %87, %37
%43 = load i32*, i32** %6
%44 = call i32 @feof(i32* %43)
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
br i1 %46, label %47, label %96
47: ; preds = %42
%48 = getelementptr inbounds [127 x i8], [127 x i8]* %10, i64 0, i64 0
%49 = load i32*, i32** %6
%50 = call i64 @fread(i8* %48, i32 20, i32 1, i32* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
br label %96
53: ; preds = %47
%54 = getelementptr inbounds [127 x i8], [127 x i8]* %10, i64 0, i64 21
store i8 0, i8* %54
%55 = getelementptr inbounds [127 x i8], [127 x i8]* %10, i64 0, i64 0
%56 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%57 = call i32 @sscanf(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32* %12)
%58 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%59 = load i32, i32* %12
%60 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %58, i32 %59)
%61 = load i32, i32* %12
%62 = call i8* @malloc(i32 %61)
store i8* %62, i8** %8
%63 = load i8*, i8** %8
%64 = load i32, i32* %12
%65 = load i32*, i32** %6
%66 = call i64 @fread(i8* %63, i32 %64, i32 1, i32* %65)
%67 = getelementptr inbounds [127 x i8], [127 x i8]* %11, i64 0, i64 0
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32 @strcpy(i8* %67, i8* %70)
%72 = getelementptr inbounds [127 x i8], [127 x i8]* %11, i64 0, i64 0
%73 = call i32 @strcat(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%74 = getelementptr inbounds [127 x i8], [127 x i8]* %11, i64 0, i64 0
%75 = getelementptr inbounds [127 x i8], [127 x i8]* %9, i64 0, i64 0
%76 = call i32 @strcat(i8* %74, i8* %75)
%77 = getelementptr inbounds [127 x i8], [127 x i8]* %11, i64 0, i64 0
%78 = call i32* @fopen(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32* %78, i32** %7
%79 = load i32*, i32** %7
%80 = icmp eq i32* %79, null
br i1 %80, label %81, label %87
81: ; preds = %53
%82 = load i32*, i32** %6
%83 = call i32 @fclose(i32* %82)
%84 = load i32, i32* @stderr
%85 = getelementptr inbounds [127 x i8], [127 x i8]* %11, i64 0, i64 0
%86 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i8* %85)
store i32 1, i32* %3
br label %99
87: ; preds = %53
%88 = load i8*, i8** %8
%89 = load i32, i32* %12
%90 = load i32*, i32** %7
%91 = call i32 @fwrite(i8* %88, i32 %89, i32 1, i32* %90)
%92 = load i32*, i32** %7
%93 = call i32 @fclose(i32* %92)
%94 = load i32*, i32** %6
%95 = call i32 @fgetc(i32* %94)
br label %42
96: ; preds = %52, %42
%97 = load i32*, i32** %6
%98 = call i32 @fclose(i32* %97)
store i32 0, i32* %3
br label %99
99: ; preds = %96, %81, %33, %15
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [180 x i8] c"Usage:\0A %s <megafile>\0A\0AWhere <megafile> is the name of a viewlogic megafile\0Awhithout any extensions. The file <megafile>.lib and \0A<megafile>.tbl must exist in the same directory\0A\00"
@.str.1 = [5 x i8] c".lib\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [49 x i8] c"Error: unable to open magefile `%s' for reading\0A\00"
@.str.4 = [6 x i8] c"%s %d\00"
@.str.5 = [7 x i8] c"%s:%d\0A\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [3 x i8] c"wb\00"
@.str.8 = [45 x i8] c"Error: unable to open file `%s' for writing\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [127 x i8]
%4 = alloca [127 x i8]
%5 = alloca [127 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [127 x i8], [127 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [127 x i8], [127 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [127 x i8], [127 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %1
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([180 x i8], [180 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %62
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strcpy(i8* %7, i8* %18)
%20 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %62
26: ; preds = %16
%27 = load i8*, i8** %17
%28 = call i32 @mkdir(i8* %27, i32 511)
%29 = call i32 @feof(i32* %21)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %26
%32 = getelementptr inbounds [127 x i8], [127 x i8]* %4, i64 0, i64 21
br label %33
33: ; preds = %31, %53
%34 = call i64 @fread(i8* %8, i32 20, i32 1, i32* %21)
%35 = icmp eq i64 %34, 0
br i1 %35, label %60, label %36
36: ; preds = %33
store i8 0, i8* %32
%37 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32* %6)
%38 = load i32, i32* %6
%39 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %7, i32 %38)
%40 = load i32, i32* %6
%41 = call i8* @malloc(i32 %40)
%42 = call i64 @fread(i8* %41, i32 %40, i32 1, i32* %21)
%43 = load i8*, i8** %17
%44 = call i32 @strcpy(i8* %9, i8* %43)
%45 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%46 = call i32 @strcat(i8* %9, i8* %7)
%47 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %53
49: ; preds = %36
%50 = call i32 @fclose(i32* %21)
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0), i8* %9)
br label %62
53: ; preds = %36
%54 = load i32, i32* %6
%55 = call i32 @fwrite(i8* %41, i32 %54, i32 1, i32* %47)
%56 = call i32 @fclose(i32* %47)
%57 = call i32 @fgetc(i32* %21)
%58 = call i32 @feof(i32* %21)
%59 = icmp eq i32 %58, 0
br i1 %59, label %33, label %60
60: ; preds = %53, %33, %26
%61 = call i32 @fclose(i32* %21)
br label %62
62: ; preds = %60, %49, %23, %12
%63 = phi i32 [ 1, %12 ], [ 1, %23 ], [ 0, %60 ], [ 1, %49 ]
ret i32 %63
}