#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int imd_read(char *, unsigned char **);
int /*<<< orphan*/ imd_unpack(unsigned char *, unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  FILE *out;
  unsigned char buffer[512512];
  unsigned char *d;
  int fsize, i;
  char out_fn[256];
  if ((argc < 2) || (argc > 3)) {
    usage();
    exit(1);
  }
  i = 0;
  i++;
  if (argc == 2) {
    int sl = strlen(argv[1]);
    strncpy(out_fn, argv[1], sl - 3);
    strncpy(out_fn + sl - 3, "bin", 3);
  } else {
    strncpy(out_fn, argv[2], 256);
  }
  printf("writing to: %s\n", out_fn);
  fsize = imd_read(argv[1], &d);
  printf("%d bytes read\n", fsize);
  imd_unpack(d, buffer);
  free(d);
  out = fopen(out_fn, "w");
  printf("writing %d bytes\n", (int)fwrite(buffer, 1, 512512, out));
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7d340,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11dd <main+0x2d>
cmpl   $0x3,-0x8(%rbp)
jle    11ee <main+0x3e>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x7d230(%rbp)
mov    -0x7d230(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7d230(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    126c <main+0xbc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x7d334(%rbp)
lea    -0x7d330(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x7d334(%rbp),%edx
sub    $0x3,%edx
call   1040 <strncpy@plt>
lea    -0x7d330(%rbp),%rdi
movslq -0x7d334(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xda3(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
jmp    1285 <main+0xd5>
lea    -0x7d330(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    -0x7d330(%rbp),%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x7d228(%rbp),%rsi
call   1090 <imd_read@plt>
mov    %eax,-0x7d22c(%rbp)
mov    -0x7d22c(%rbp),%esi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x7d228(%rbp),%rdi
lea    -0x7d220(%rbp),%rsi
call   1080 <imd_unpack@plt>
mov    -0x7d228(%rbp),%rdi
call   1030 <free@plt>
lea    -0x7d330(%rbp),%rdi
lea    0xd2e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x7d220(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7d200,%edx
call   10d0 <fwrite@plt>
mov    %eax,%esi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x7d340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x7d310,%rsp
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
jbe    12b9 <main+0x109>
mov    %rsi,%r15
cmp    $0x2,%edi
jne    1205 <main+0x55>
mov    0x8(%r15),%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    0x8(%r15),%rsi
lea    -0x3(%rbx),%edx
lea    0x10(%rsp),%rdi
call   1040 <strncpy@plt>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
add    $0xd,%rdi
lea    0xe02(%rip),%rsi
mov    $0x3,%edx
jmp    1213 <main+0x63>
mov    0x10(%r15),%rsi
lea    0x10(%rsp),%rdi
mov    $0x100,%edx
call   1040 <strncpy@plt>
lea    0xde5(%rip),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%r15),%rdi
lea    0x8(%rsp),%rsi
call   1090 <imd_read@plt>
lea    0xdd1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%rdi
lea    0x110(%rsp),%r15
mov    %r15,%rsi
call   1080 <imd_unpack@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0xdb1(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x7d200,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
lea    0xd8c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x7d310,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"bin\00"
@.str.1 = [16 x i8] c"writing to: %s\0A\00"
@.str.2 = [15 x i8] c"%d bytes read\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [18 x i8] c"writing %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [512512 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 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 slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 3
br i1 %17, label %18, label %21
18: ; preds = %15, %2
%19 = call i32 (...) @usage()
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
store i32 0, i32* %10
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 1
store i32 %23, i32* %10
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %44
26: ; preds = %21
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %12
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* %12
%36 = sub nsw i32 %35, 3
%37 = call i32 @strncpy(i8* %31, i8* %34, i32 %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = getelementptr inbounds i8, i8* %41, i64 -3
%43 = call i32 @strncpy(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %50
44: ; preds = %21
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @strncpy(i8* %45, i8* %48, i32 256)
br label %50
50: ; preds = %44, %26
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @imd_read(i8* %55, i8** %8)
store i32 %56, i32* %9
%57 = load i32, i32* %9
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %57)
%59 = load i8*, i8** %8
%60 = getelementptr inbounds [512512 x i8], [512512 x i8]* %7, i64 0, i64 0
%61 = call i32 @imd_unpack(i8* %59, i8* %60)
%62 = load i8*, i8** %8
%63 = call i32 @free(i8* %62)
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%65 = call i32* @fopen(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %65, i32** %6
%66 = getelementptr inbounds [512512 x i8], [512512 x i8]* %7, i64 0, i64 0
%67 = load i32*, i32** %6
%68 = call i64 @fwrite(i8* %66, i32 1, i32 512512, i32* %67)
%69 = trunc i64 %68 to i32
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"bin\00"
@.str.1 = [16 x i8] c"writing to: %s\0A\00"
@.str.2 = [15 x i8] c"%d bytes read\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [18 x i8] c"writing %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512512 x i8]
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [512512 x i8], [512512 x i8]* %3, i64 0, i64 0
%7 = bitcast i8** %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = add i32 %0, -4
%10 = icmp ult i32 %9, -2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 (...) @usage()
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = icmp eq i32 %0, 2
br i1 %15, label %16, label %27
16: ; preds = %14
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @strlen(i8* %18)
%20 = load i8*, i8** %17
%21 = add nsw i32 %19, -3
%22 = call i32 @strncpy(i8* %8, i8* %20, i32 %21)
%23 = sext i32 %19 to i64
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %23
%25 = getelementptr inbounds i8, i8* %24, i64 -3
%26 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
br label %31
27: ; preds = %14
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @strncpy(i8* %8, i8* %29, i32 256)
br label %31
31: ; preds = %27, %16
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @imd_read(i8* %34, i8** %4)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %4
%38 = call i32 @imd_unpack(i8* %37, i8* %6)
%39 = load i8*, i8** %4
%40 = call i32 @free(i8* %39)
%41 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = call i64 @fwrite(i8* %6, i32 1, i32 512512, i32* %41)
%43 = trunc i64 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = call i32 @fclose(i32* %41)
ret i32 0
}