#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*/ close_(int /*<<< orphan*/);
int /*<<< orphan*/ getsn(unsigned char *, int);
int open_(unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ poke(char, unsigned short, int /*<<< orphan*/);
int /*<<< orphan*/ puts0(char *);
scalar_t__ readc(int /*<<< orphan*/, char *);
int /*<<< orphan*/ run_msdos();
void load_and_run_msdos(void) {
  unsigned short int i, load_segment, offset;
  unsigned char filename[16 + 2];
  unsigned char str[256];
  char c;
  int rc;
  puts0("file ? ");
  for (i = 0; i < 16; i++)
    str[i] = 0;
  getsn(str, 256);
  for (i = 0; i < 16; i++)
    filename[i] = str[i];
  filename[16] = 0;
  rc = open_(filename, 0);
  if (rc == -1) {
    puts0("can't open file\r\n");
    return;
  }
  load_segment = 0x4060;
  offset = 0;
  while (readc(0, &c) == 0) {
    poke(c, load_segment, offset++);
  }
  close_(0);
  run_msdos();
}


x86 assembly (clang, -O0):
<load_and_run_msdos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
lea    0xe7e(%rip),%rdi
call   1070 <puts0@plt>
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
cmp    $0x10,%eax
jge    11b7 <load_and_run_msdos+0x47>
movzwl -0x2(%rbp),%eax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x2(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x2(%rbp)
jmp    118d <load_and_run_msdos+0x1d>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1030 <getsn@plt>
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
cmp    $0x10,%eax
jge    11ff <load_and_run_msdos+0x8f>
movzwl -0x2(%rbp),%eax
mov    -0x120(%rbp,%rax,1),%cl
movzwl -0x2(%rbp),%eax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0x2(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x2(%rbp)
jmp    11ce <load_and_run_msdos+0x5e>
movb   $0x0,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1060 <open_@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0xffffffff,-0x128(%rbp)
jne    1232 <load_and_run_msdos+0xc2>
lea    0xde0(%rip),%rdi
call   1070 <puts0@plt>
jmp    1290 <load_and_run_msdos+0x120>
movw   $0x4060,-0x4(%rbp)
movw   $0x0,-0x6(%rbp)
xor    %edi,%edi
lea    -0x121(%rbp),%rsi
call   1080 <readc@plt>
cmp    $0x0,%rax
jne    1282 <load_and_run_msdos+0x112>
mov    -0x121(%rbp),%cl
mov    -0x4(%rbp),%ax
mov    -0x6(%rbp),%dx
mov    %dx,%si
add    $0x1,%si
mov    %si,-0x6(%rbp)
movzwl %dx,%edx
movsbl %cl,%edi
movzwl %ax,%esi
call   1040 <poke@plt>
jmp    123e <load_and_run_msdos+0xce>
xor    %edi,%edi
call   1090 <close_@plt>
mov    $0x0,%al
call   1050 <run_msdos@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_and_run_msdos>:
push   %rbp
push   %rbx
sub    $0x138,%rsp
lea    0xe80(%rip),%rdi
call   1070 <puts0@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x100,%esi
call   1030 <getsn@plt>
movaps 0x30(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1060 <open_@plt>
cmp    $0xffffffff,%eax
je     1216 <load_and_run_msdos+0xa6>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
call   1080 <readc@plt>
test   %rax,%rax
jne    1206 <load_and_run_msdos+0x96>
xor    %ecx,%ecx
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0xf(%rsp),%edi
lea    0x1(%rcx),%ebp
movzwl %cx,%edx
mov    $0x4060,%esi
call   1040 <poke@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <readc@plt>
mov    %ebp,%ecx
test   %rax,%rax
je     11e0 <load_and_run_msdos+0x70>
xor    %edi,%edi
call   1090 <close_@plt>
xor    %eax,%eax
call   1050 <run_msdos@plt>
jmp    1222 <load_and_run_msdos+0xb2>
lea    0xdeb(%rip),%rdi
call   1070 <puts0@plt>
add    $0x138,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"file ? \00"
@.str.1 = [18 x i8] c"can't open file\0D\0A\00"
define void @load_and_run_msdos() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i16
%4 = alloca [18 x i8]
%5 = alloca [256 x i8]
%6 = alloca i8
%7 = alloca i32
%8 = call i32 @puts0(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i16 0, i16* %1
br label %9
9: ; preds = %17, %0
%10 = load i16, i16* %1
%11 = zext i16 %10 to i32
%12 = icmp slt i32 %11, 16
br i1 %12, label %13, label %20
13: ; preds = %9
%14 = load i16, i16* %1
%15 = zext i16 %14 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %13
%18 = load i16, i16* %1
%19 = add i16 %18, 1
store i16 %19, i16* %1
br label %9
20: ; preds = %9
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%22 = call i32 @getsn(i8* %21, i32 256)
store i16 0, i16* %1
br label %23
23: ; preds = %35, %20
%24 = load i16, i16* %1
%25 = zext i16 %24 to i32
%26 = icmp slt i32 %25, 16
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i16, i16* %1
%29 = zext i16 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = load i16, i16* %1
%33 = zext i16 %32 to i64
%34 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 %33
store i8 %31, i8* %34
br label %35
35: ; preds = %27
%36 = load i16, i16* %1
%37 = add i16 %36, 1
store i16 %37, i16* %1
br label %23
38: ; preds = %23
%39 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 16
store i8 0, i8* %39
%40 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%41 = call i32 @open_(i8* %40, i32 0)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @puts0(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %60
46: ; preds = %38
store i16 16480, i16* %2
store i16 0, i16* %3
br label %47
47: ; preds = %50, %46
%48 = call i64 @readc(i32 0, i8* %6)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = load i8, i8* %6
%52 = load i16, i16* %2
%53 = load i16, i16* %3
%54 = add i16 %53, 1
store i16 %54, i16* %3
%55 = zext i16 %53 to i32
%56 = call i32 @poke(i8 signext %51, i16 zeroext %52, i32 %55)
br label %47
57: ; preds = %47
%58 = call i32 @close_(i32 0)
%59 = call i32 (...) @run_msdos()
br label %60
60: ; preds = %57, %44
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"file ? \00"
@.str.1 = [18 x i8] c"can't open file\0D\0A\00"
define void @load_and_run_msdos() {
%1 = alloca [18 x i8]
%2 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = alloca i8
%6 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = tail call i32 @puts0(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @getsn(i8* %7, i32 256)
%10 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 16
store i8 0, i8* %10
%11 = call i32 @open_(i8* %6, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i64 @readc(i32 0, i8* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %26
16: ; preds = %0
%17 = call i32 @puts0(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %29
18: ; preds = %13, %18
%19 = phi i16 [ %21, %18 ], [ 0, %13 ]
%20 = load i8, i8* %5
%21 = add i16 %19, 1
%22 = zext i16 %19 to i32
%23 = call i32 @poke(i8 signext %20, i16 zeroext 16480, i32 %22)
%24 = call i64 @readc(i32 0, i8* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %18, label %26
26: ; preds = %18, %13
%27 = call i32 @close_(i32 0)
%28 = call i32 (...) @run_msdos()
br label %29
29: ; preds = %26, %16
ret void
}