#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*/ SEEK_CUR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ get_os_sectors(int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
void get_os(int fd, char *filename) {
  char buffer[16384];
  FILE *out, *in;
  int track;
  if (fd != -1) {
    get_os_sectors(fd, buffer);
    out = fopen(filename, "w");
    if (!out) {
      fprintf(stderr, "cannot open %s\n", filename);
      exit(1);
    }
    if (fwrite(buffer, 1, 16384, out) != 16384) {
      fprintf(stderr, "error writing %s\n", filename);
      fclose(out);
      exit(1);
    }
    fclose(out);
  } else {
    in = fopen(filename, "r");
    if (!in) {
      fprintf(stderr, "cannot open %s\n", filename);
      exit(1);
    }
    if (fread(buffer, 1, 11264, in) != 11264) {
    dumpfile_readfail:
      fprintf(stderr, "error reading %s\n", filename);
      fclose(in);
      exit(-1);
    }
    for (track = 2; track <= 11; track++) {
      fseek(in, 5120, SEEK_CUR);
      if (fread(buffer + 11264 + (track - 2) * 512, 1, 512, in) != 512)
        goto dumpfile_readfail;
    }
    fwrite(buffer, 1, 16384, stdout);
  }
}


x86 assembly (clang, -O0):
<get_os>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     125b <get_os+0xdb>
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
call   1050 <get_os_sectors@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x4018(%rbp)
cmpq   $0x0,-0x4018(%rbp)
jne    11f3 <get_os+0x73>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x4010(%rbp),%rdi
mov    -0x4018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   10a0 <fwrite@plt>
cmp    $0x4000,%eax
je     124a <get_os+0xca>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde3(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4018(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4018(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13ae <get_os+0x22e>
mov    -0x10(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x4020(%rbp)
cmpq   $0x0,-0x4020(%rbp)
jne    12a3 <get_os+0x123>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd6e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x4010(%rbp),%rdi
mov    -0x4020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2c00,%edx
call   1030 <fread@plt>
cmp    $0x2c00,%eax
je     12ff <get_os+0x17f>
jmp    12d0 <get_os+0x150>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd42(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4020(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x2,-0x4024(%rbp)
cmpl   $0xb,-0x4024(%rbp)
jg     138e <get_os+0x20e>
mov    -0x4020(%rbp),%rdi
mov    0x2cc4(%rip),%rax
mov    (%rax),%edx
mov    $0x1400,%esi
call   1070 <fseek@plt>
lea    -0x4010(%rbp),%rdi
add    $0x2c00,%rdi
mov    -0x4024(%rbp),%eax
sub    $0x2,%eax
shl    $0x9,%eax
cltq
add    %rax,%rdi
mov    -0x4020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
cmp    $0x200,%eax
je     1375 <get_os+0x1f5>
jmp    12d0 <get_os+0x150>
jmp    137a <get_os+0x1fa>
mov    -0x4024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4024(%rbp)
jmp    1309 <get_os+0x189>
lea    -0x4010(%rbp),%rdi
mov    0x2c34(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   10a0 <fwrite@plt>
add    $0x4030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_os>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4000,%rsp
mov    %rsi,%r14
cmp    $0xffffffff,%edi
je     11e8 <get_os+0x68>
mov    %rsp,%rsi
call   1050 <get_os_sectors@plt>
lea    0xe59(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12d5 <get_os+0x155>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
cmp    $0x4000,%eax
jne    12f7 <get_os+0x177>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    129a <get_os+0x11a>
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12d5 <get_os+0x155>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x2c00,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x2c00,%eax
jne    12ab <get_os+0x12b>
lea    0x2c00(%rsp),%r15
xor    %ebx,%ebx
mov    0x2db4(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    $0x1400,%esi
call   1070 <fseek@plt>
lea    (%r15,%rbx,1),%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x200,%eax
jne    12ab <get_os+0x12b>
add    $0x200,%rbx
cmp    $0x1400,%rbx
jne    1240 <get_os+0xc0>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rcx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
call   10a0 <fwrite@plt>
add    $0x4000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [18 x i8] c"error writing %s\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [18 x i8] c"error reading %s\0A\00"
@SEEK_CUR = global i32 0
@stdout = global i32* null
define void @get_os(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [16384 x i8]
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %39
11: ; preds = %2
%12 = load i32, i32* %3
%13 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%14 = call i32 @get_os_sectors(i32 %12, i8* %13)
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %11
%25 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%26 = load i32*, i32** %6
%27 = call i32 @fwrite(i8* %25, i32 1, i32 16384, i32* %26)
%28 = icmp ne i32 %27, 16384
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %4
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %24
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
br label %89
39: ; preds = %2
%40 = load i8*, i8** %4
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %41, i32** %7
%42 = load i32*, i32** %7
%43 = icmp ne i32* %42, null
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %4
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %39
%50 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%51 = load i32*, i32** %7
%52 = call i32 @fread(i8* %50, i32 1, i32 11264, i32* %51)
%53 = icmp ne i32 %52, 11264
br i1 %53, label %54, label %62
54: ; preds = %49
br label %55
55: ; preds = %80, %54
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %4
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i32*, i32** %7
%60 = call i32 @fclose(i32* %59)
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %49
store i32 2, i32* %8
br label %63
63: ; preds = %82, %62
%64 = load i32, i32* %8
%65 = icmp sle i32 %64, 11
br i1 %65, label %66, label %85
66: ; preds = %63
%67 = load i32*, i32** %7
%68 = load i32, i32* @SEEK_CUR
%69 = call i32 @fseek(i32* %67, i32 5120, i32 %68)
%70 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%71 = getelementptr inbounds i8, i8* %70, i64 11264
%72 = load i32, i32* %8
%73 = sub nsw i32 %72, 2
%74 = mul nsw i32 %73, 512
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %71, i64 %75
%77 = load i32*, i32** %7
%78 = call i32 @fread(i8* %76, i32 1, i32 512, i32* %77)
%79 = icmp ne i32 %78, 512
br i1 %79, label %80, label %81
80: ; preds = %66
br label %55
81: ; preds = %66
br label %82
82: ; preds = %81
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
br label %63
85: ; preds = %63
%86 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%87 = load i32*, i32** @stdout
%88 = call i32 @fwrite(i8* %86, i32 1, i32 16384, i32* %87)
br label %89
89: ; preds = %85, %36
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"cannot open %s\0A\00"
@.str.2 = [18 x i8] c"error writing %s\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [18 x i8] c"error reading %s\0A\00"
@SEEK_CUR = global i32 0
@stdout = global i32* null
define void @get_os(i32 %0, i8* %1) {
%3 = alloca [16384 x i8]
%4 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, -1
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = call i32 @get_os_sectors(i32 %0, i8* %4)
%8 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = call i32 @fwrite(i8* %4, i32 1, i32 16384, i32* %8)
%16 = icmp eq i32 %15, 16384
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%20 = call i32 @fclose(i32* %8)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = call i32 @fclose(i32* %8)
br label %56
24: ; preds = %2
%25 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = call i32 @fread(i8* %4, i32 1, i32 11264, i32* %25)
%33 = icmp eq i32 %32, 11264
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 11264
br label %44
36: ; preds = %44, %31
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%39 = tail call i32 @fclose(i32* %25)
%40 = tail call i32 @exit(i32 -1)
unreachable
41: ; preds = %44
%42 = add nuw nsw i64 %45, 1
%43 = icmp eq i64 %42, 12
br i1 %43, label %53, label %44
44: ; preds = %34, %41
%45 = phi i64 [ 2, %34 ], [ %42, %41 ]
%46 = load i32, i32* @SEEK_CUR
%47 = tail call i32 @fseek(i32* %25, i32 5120, i32 %46)
%48 = shl nuw nsw i64 %45, 9
%49 = add nsw i64 %48, -1024
%50 = getelementptr inbounds i8, i8* %35, i64 %49
%51 = call i32 @fread(i8* %50, i32 1, i32 512, i32* %25)
%52 = icmp eq i32 %51, 512
br i1 %52, label %41, label %36
53: ; preds = %41
%54 = load i32*, i32** @stdout
%55 = call i32 @fwrite(i8* %4, i32 1, i32 16384, i32* %54)
br label %56
56: ; preds = %53, %22
ret void
}