#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  register int i;
  int fd;
  char drive;
  static unsigned char sector[128];
  static char fn[] = "disks/drive?.cpm";
  static char usage[] = "usage: format a | b | c | d | i | j | p";
  if (argc != 2) {
    puts(usage);
    exit(1);
  }
  i = *argv[1];
  if (argc != 2 || (i != 'a' && i != 'b' && i != 'c' && i != 'd' && i != 'i' &&
                    i != 'j' && i != 'p')) {
    puts(usage);
    exit(1);
  }
  fn[11] = drive = (char)i;
  memset((char *)sector, 0xe5, 128);
  if ((fd = open(fn, O_RDONLY)) != -1) {
    printf("disk file %s exists, aborting\n", fn);
    exit(1);
  }
  if ((fd = creat(fn, 0644)) == -1) {
    perror("disk file");
    exit(1);
  }
  if (drive <= 'd') {
    for (i = 0; i < 77 * 26; i++)
      write(fd, (char *)sector, 128);
  } else if (drive == 'i' || drive == 'j') {
    for (i = 0; i < 255 * 128; i++)
      write(fd, (char *)sector, 128);
  } else if (drive == 'p') {
    for (i = 0; i < 256 * 16384; i++)
      write(fd, (char *)sector, 128);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c6 <main+0x36>
lea    0x2ed9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1224 <main+0x94>
cmpl   $0x61,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x62,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x63,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x64,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x69,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x6a,-0x14(%rbp)
je     123a <main+0xaa>
cmpl   $0x70,-0x14(%rbp)
je     123a <main+0xaa>
lea    0x2e65(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %al,-0x19(%rbp)
mov    %al,0x2e35(%rip)
lea    0x2e83(%rip),%rdi
mov    $0xe5,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0x2e04(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     129a <main+0x10a>
lea    0xd7c(%rip),%rdi
lea    0x2de5(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0x2dcf(%rip),%rdi
mov    $0x1a4,%esi
call   10a0 <creat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12cd <main+0x13d>
lea    0xd61(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x64,%eax
jg     1315 <main+0x185>
movl   $0x0,-0x14(%rbp)
cmpl   $0x7d2,-0x14(%rbp)
jge    1310 <main+0x180>
mov    -0x18(%rbp),%edi
lea    0x2dd8(%rip),%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12e1 <main+0x151>
jmp    13bc <main+0x22c>
movsbl -0x19(%rbp),%eax
cmp    $0x69,%eax
je     132f <main+0x19f>
movsbl -0x19(%rbp),%eax
cmp    $0x6a,%eax
jne    136a <main+0x1da>
movl   $0x0,-0x14(%rbp)
cmpl   $0x7f80,-0x14(%rbp)
jge    1365 <main+0x1d5>
mov    -0x18(%rbp),%edi
lea    0x2d83(%rip),%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1336 <main+0x1a6>
jmp    13b7 <main+0x227>
movsbl -0x19(%rbp),%eax
cmp    $0x70,%eax
jne    13b2 <main+0x222>
movl   $0x0,-0x14(%rbp)
cmpl   $0x400000,-0x14(%rbp)
jge    13ad <main+0x21d>
mov    -0x18(%rbp),%edi
lea    0x2d3b(%rip),%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    137e <main+0x1ee>
jmp    13b2 <main+0x222>
jmp    13b7 <main+0x227>
jmp    13bc <main+0x22c>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    12b2 <main+0x122>
mov    0x8(%rsi),%rax
mov    (%rax),%r14b
lea    -0x61(%r14),%eax
cmp    $0xf,%al
ja     12b2 <main+0x122>
movzbl %al,%eax
mov    $0x830f,%ecx
bt     %rax,%rcx
jae    12b2 <main+0x122>
mov    %r14b,0x2eb2(%rip)
lea    0x2ea0(%rip),%rbx
lea    0x2ef9(%rip),%rdi
mov    $0xe5,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    12c8 <main+0x138>
lea    0x2e69(%rip),%rdi
mov    $0x1a4,%esi
call   10a0 <creat@plt>
cmp    $0xffffffff,%eax
je     12e5 <main+0x155>
mov    %eax,%ebx
cmp    $0x64,%r14b
ja     1246 <main+0xb6>
mov    $0x7d2,%ebp
lea    0x2ea2(%rip),%r14
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
jne    1230 <main+0xa0>
jmp    12a4 <main+0x114>
lea    -0x69(%r14),%eax
cmp    $0x1,%al
ja     1276 <main+0xe6>
mov    $0x7f80,%ebp
lea    0x2e76(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
jne    1260 <main+0xd0>
jmp    12a4 <main+0x114>
cmp    $0x70,%r14b
jne    12a4 <main+0x114>
mov    $0x400000,%ebp
lea    0x2e48(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebp
jne    1290 <main+0x100>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0x2dd7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd31(%rip),%rdi
lea    0x2d9a(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd33(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@main.sector = internal global [128 x i8] zeroinitializer
@main.fn = internal global [17 x i8] c"disks/drive?.cpm\00"
@main.usage = internal global [40 x i8] c"usage: format a | b | c | d | i | j | p\00"
@O_RDONLY = global i32 0
@.str = [31 x i8] c"disk file %s exists, aborting\0A\00"
@.str.1 = [10 x i8] c"disk file\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @main.usage, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
store i32 %19, i32* %6
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 2
br i1 %21, label %43, label %22
22: ; preds = %14
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 97
br i1 %24, label %25, label %46
25: ; preds = %22
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 98
br i1 %27, label %28, label %46
28: ; preds = %25
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 99
br i1 %30, label %31, label %46
31: ; preds = %28
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 100
br i1 %33, label %34, label %46
34: ; preds = %31
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 105
br i1 %36, label %37, label %46
37: ; preds = %34
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 106
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 112
br i1 %42, label %43, label %46
43: ; preds = %40, %14
%44 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @main.usage, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %40, %37, %34, %31, %28, %25, %22
%47 = load i32, i32* %6
%48 = trunc i32 %47 to i8
store i8 %48, i8* %8
store i8 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 11)
%49 = call i32 @memset(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 229, i32 128)
%50 = load i32, i32* @O_RDONLY
%51 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0), i32 %50)
store i32 %51, i32* %7
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %46
%57 = call i32 @creat(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0), i32 420)
store i32 %57, i32* %7
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %56
%63 = load i8, i8* %8
%64 = sext i8 %63 to i32
%65 = icmp sle i32 %64, 100
br i1 %65, label %66, label %77
66: ; preds = %62
store i32 0, i32* %6
br label %67
67: ; preds = %73, %66
%68 = load i32, i32* %6
%69 = icmp slt i32 %68, 2002
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = load i32, i32* %7
%72 = call i32 @write(i32 %71, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
br label %73
73: ; preds = %70
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
store i32 %75, i32* %6
br label %67
76: ; preds = %67
br label %113
77: ; preds = %62
%78 = load i8, i8* %8
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 105
br i1 %80, label %85, label %81
81: ; preds = %77
%82 = load i8, i8* %8
%83 = sext i8 %82 to i32
%84 = icmp eq i32 %83, 106
br i1 %84, label %85, label %96
85: ; preds = %81, %77
store i32 0, i32* %6
br label %86
86: ; preds = %92, %85
%87 = load i32, i32* %6
%88 = icmp slt i32 %87, 32640
br i1 %88, label %89, label %95
89: ; preds = %86
%90 = load i32, i32* %7
%91 = call i32 @write(i32 %90, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
br label %92
92: ; preds = %89
%93 = load i32, i32* %6
%94 = add nsw i32 %93, 1
store i32 %94, i32* %6
br label %86
95: ; preds = %86
br label %112
96: ; preds = %81
%97 = load i8, i8* %8
%98 = sext i8 %97 to i32
%99 = icmp eq i32 %98, 112
br i1 %99, label %100, label %111
100: ; preds = %96
store i32 0, i32* %6
br label %101
101: ; preds = %107, %100
%102 = load i32, i32* %6
%103 = icmp slt i32 %102, 4194304
br i1 %103, label %104, label %110
104: ; preds = %101
%105 = load i32, i32* %7
%106 = call i32 @write(i32 %105, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
br label %107
107: ; preds = %104
%108 = load i32, i32* %6
%109 = add nsw i32 %108, 1
store i32 %109, i32* %6
br label %101
110: ; preds = %101
br label %111
111: ; preds = %110, %96
br label %112
112: ; preds = %111, %95
br label %113
113: ; preds = %112, %76
%114 = load i32, i32* %7
%115 = call i32 @close(i32 %114)
ret i32 0
}

LLVM IR (-O2):
@main.sector = internal global [128 x i8] zeroinitializer
@main.fn = internal global [17 x i8] c"disks/drive?.cpm\00"
@main.usage = internal global [40 x i8] c"usage: format a | b | c | d | i | j | p\00"
@O_RDONLY = global i32 0
@.str = [31 x i8] c"disk file %s exists, aborting\0A\00"
@.str.1 = [10 x i8] c"disk file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @main.usage, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
switch i8 %10, label %11 [
i8 112, label %14
i8 106, label %14
i8 105, label %14
i8 100, label %14
i8 99, label %14
i8 98, label %14
i8 97, label %14
]
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @main.usage, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %7, %7, %7, %7, %7, %7, %7
store i8 %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 11)
%15 = tail call i32 @memset(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 229, i32 128)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = tail call i32 @creat(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @main.fn, i64 0, i64 0), i32 420)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = icmp ult i8 %10, 101
br i1 %29, label %30, label %35
30: ; preds = %28, %30
%31 = phi i32 [ %33, %30 ], [ 0, %28 ]
%32 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
%33 = add nuw nsw i32 %31, 1
%34 = icmp eq i32 %33, 2002
br i1 %34, label %50, label %30
35: ; preds = %28
%36 = add nsw i8 %10, -105
%37 = icmp ult i8 %36, 2
br i1 %37, label %38, label %43
38: ; preds = %35, %38
%39 = phi i32 [ %41, %38 ], [ 0, %35 ]
%40 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
%41 = add nuw nsw i32 %39, 1
%42 = icmp eq i32 %41, 32640
br i1 %42, label %50, label %38
43: ; preds = %35
%44 = icmp eq i8 %10, 112
br i1 %44, label %45, label %50
45: ; preds = %43, %45
%46 = phi i32 [ %48, %45 ], [ 0, %43 ]
%47 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([128 x i8], [128 x i8]* @main.sector, i64 0, i64 0), i32 128)
%48 = add nuw nsw i32 %46, 1
%49 = icmp eq i32 %48, 4194304
br i1 %49, label %50, label %45
50: ; preds = %45, %38, %30, %43
%51 = tail call i32 @close(i32 %23)
ret i32 0
}