#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 uint32_t;

/* Variables and functions */
int cpu_step();
int debug_run(int *, int *);
int /*<<< orphan*/ keypad_emulate(int /*<<< orphan*/ *);
scalar_t__ screen_emulate(int);
int /*<<< orphan*/ screen_start_frame();
int /*<<< orphan*/ timer_emulate(int);
uint32_t dmg_run(uint32_t delta, uint8_t *keypad, int *debug_flag,
                 int *debug_pause) {
  uint32_t clk, cycles;
  clk = 0;
  screen_start_frame();
  while (clk < 70224) {
    if (*debug_flag) {
      cycles = debug_run(debug_flag, debug_pause);
    } else {
      cycles = cpu_step();
    }
    clk += cycles;
    timer_emulate(cycles);
    keypad_emulate(keypad);
    if (screen_emulate(cycles)) {
      break;
    }
  }
  return clk * 1000 / 4194304;
}


x86 assembly (clang, -O0):
<dmg_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1080 <screen_start_frame@plt>
cmpl   $0x11250,-0x24(%rbp)
jge    11f4 <dmg_run+0x94>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     11b4 <dmg_run+0x54>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <debug_run@plt>
mov    %eax,-0x28(%rbp)
jmp    11be <dmg_run+0x5e>
mov    $0x0,%al
call   1050 <cpu_step@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <timer_emulate@plt>
mov    -0x10(%rbp),%rdi
call   1030 <keypad_emulate@plt>
mov    -0x28(%rbp),%edi
call   1060 <screen_emulate@plt>
cmp    $0x0,%rax
je     11ef <dmg_run+0x8f>
jmp    11f4 <dmg_run+0x94>
jmp    1185 <dmg_run+0x25>
imul   $0x3e8,-0x24(%rbp),%eax
mov    $0x400000,%ecx
cltd
idiv   %ecx
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dmg_run>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
xor    %eax,%eax
call   1080 <screen_start_frame@plt>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmpl   $0x0,(%r12)
je     11a0 <dmg_run+0x40>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <debug_run@plt>
jmp    11a7 <dmg_run+0x47>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <cpu_step@plt>
mov    %eax,%ebp
add    %eax,%ebx
mov    %eax,%edi
call   1040 <timer_emulate@plt>
mov    %r15,%rdi
call   1030 <keypad_emulate@plt>
mov    %ebp,%edi
call   1060 <screen_emulate@plt>
test   %rax,%rax
jne    11ce <dmg_run+0x6e>
cmp    $0x11250,%ebx
jl     1180 <dmg_run+0x20>
imul   $0x3e8,%ebx,%ecx
lea    0x3fffff(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x16,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dmg_run(i32 %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
%11 = call i32 (...) @screen_start_frame()
br label %12
12: ; preds = %37, %4
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 70224
br i1 %14, label %15, label %38
15: ; preds = %12
%16 = load i32*, i32** %7
%17 = load i32, i32* %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32*, i32** %7
%21 = load i32*, i32** %8
%22 = call i32 @debug_run(i32* %20, i32* %21)
store i32 %22, i32* %10
br label %25
23: ; preds = %15
%24 = call i32 (...) @cpu_step()
store i32 %24, i32* %10
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* %10
%27 = load i32, i32* %9
%28 = add nsw i32 %27, %26
store i32 %28, i32* %9
%29 = load i32, i32* %10
%30 = call i32 @timer_emulate(i32 %29)
%31 = load i32*, i32** %6
%32 = call i32 @keypad_emulate(i32* %31)
%33 = load i32, i32* %10
%34 = call i64 @screen_emulate(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %25
br label %38
37: ; preds = %25
br label %12
38: ; preds = %36, %12
%39 = load i32, i32* %9
%40 = mul nsw i32 %39, 1000
%41 = sdiv i32 %40, 4194304
ret i32 %41
}

LLVM IR (-O2):
define i32 @dmg_run(i32 %0, i32* %1, i32* %2, i32* %3) {
%5 = tail call i32 (...) @screen_start_frame()
br label %6
6: ; preds = %4, %14
%7 = phi i32 [ 0, %4 ], [ %16, %14 ]
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @debug_run(i32* %2, i32* %3)
br label %14
12: ; preds = %6
%13 = tail call i32 (...) @cpu_step()
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
%16 = add nsw i32 %15, %7
%17 = tail call i32 @timer_emulate(i32 %15)
%18 = tail call i32 @keypad_emulate(i32* %1)
%19 = tail call i64 @screen_emulate(i32 %15)
%20 = icmp eq i64 %19, 0
%21 = icmp slt i32 %16, 70224
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %6, label %23
23: ; preds = %14
%24 = mul nsw i32 %16, 1000
%25 = sdiv i32 %24, 4194304
ret i32 %25
}