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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ mdv_make_map();
int /*<<< orphan*/ mdv_write_tape(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void main(int argc, char *argv[]) {
  FILE *fp;
  FILE *fsp = NULL;
  uint8_t zero = 0;
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "%s: microdrive.mdr [filesystem]\n", argv[0]);
    exit(1);
  }
  mdv_make_map();
  fp = fopen(argv[1], "w");
  if (fp == NULL) {
    perror(argv[1]);
    exit(1);
  }
  if (argc == 3) {
    fsp = fopen(argv[2], "r");
    if (fsp == NULL) {
      perror(argv[2]);
      exit(1);
    }
  }
  mdv_write_tape(fp, fsp);
  fwrite(&zero, 1, 1, fp);
  fclose(fp);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11d8 <main+0x58>
cmpl   $0x3,-0x4(%rbp)
je     11d8 <main+0x58>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe37(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <mdv_make_map@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe33(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <main+0x99>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x3,-0x4(%rbp)
jne    1262 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdf1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125d <main+0xdd>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1262 <main+0xe2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <mdv_write_tape@plt>
mov    -0x18(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    11b5 <main+0x35>
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <mdv_make_map@plt>
mov    0x8(%rbx),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11d7 <main+0x57>
mov    0x8(%rbx),%rdi
jmp    11fb <main+0x7b>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%r14
cmp    $0x3,%ebp
jne    120a <main+0x8a>
mov    0x10(%rbx),%rdi
lea    0xe39(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rsi
test   %rax,%rax
jne    120c <main+0x8c>
mov    0x10(%rbx),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %esi,%esi
mov    %r14,%rdi
call   1040 <mdv_write_tape@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"%s: microdrive.mdr [filesystem]\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32* null, i32** %6
store i32 0, i32* %7
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %20
10: ; preds = %2
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %10, %2
%21 = call i32 (...) @mdv_make_map()
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i8**, i8*** %4
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @perror(i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = load i32, i32* %3
%36 = icmp eq i32 %35, 3
br i1 %36, label %37, label %51
37: ; preds = %34
%38 = load i8**, i8*** %4
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %6
%42 = load i32*, i32** %6
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %50
44: ; preds = %37
%45 = load i8**, i8*** %4
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32 @perror(i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %37
br label %51
51: ; preds = %50, %34
%52 = load i32*, i32** %5
%53 = load i32*, i32** %6
%54 = call i32 @mdv_write_tape(i32* %52, i32* %53)
%55 = load i32*, i32** %5
%56 = call i32 @fwrite(i32* %7, i32 1, i32 1, i32* %55)
%57 = load i32*, i32** %5
%58 = call i32 @fclose(i32* %57)
%59 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"%s: microdrive.mdr [filesystem]\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = and i32 %0, -2
%4 = icmp eq i32 %3, 2
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32 (...) @mdv_make_map()
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i8*, i8** %12
%18 = tail call i32 @perror(i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = icmp eq i32 %0, 3
br i1 %21, label %22, label %31
22: ; preds = %20
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %23
%29 = tail call i32 @perror(i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %22, %20
%32 = phi i32* [ %25, %22 ], [ null, %20 ]
%33 = tail call i32 @mdv_write_tape(i32* %14, i32* %32)
%34 = tail call i32 @fputc(i32 0, i32* %14)
%35 = tail call i32 @fclose(i32* %14)
%36 = tail call i32 @exit(i32 0)
unreachable
}