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

/* Variables and functions */
int /*<<< orphan*/ dbg_cpu0_read(int, int *, int);
int /*<<< orphan*/ dbg_cpu0_read_ctrl(int /*<<< orphan*/, unsigned char *);
int /*<<< orphan*/ dbg_cpu0_write_ctrl(int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void dbg_test() {
  int i;
  uint32_t npc, ppc, r1, insn, result;
  unsigned char stalled;
  unsigned char read_byte;
  printf("  Stall or1k\n");
  dbg_cpu0_write_ctrl(0, 0x01);
  dbg_cpu0_read_ctrl(0, &stalled);
  if (!(stalled & 0x1)) {
    printf("\tor1k stall failed. read: 0x%x\n", stalled);
  }
  uint32_t pcs_and_sr[3];
  ("  Reading npc, ppc\n");
  dbg_cpu0_read(16, (uint32_t *)pcs_and_sr, 3 * 4);
  ("  Reading r1\n");
  dbg_cpu0_read(0x401, &r1, 4);
  printf("  Read      npc = %.8x ppc = %.8x r1 = %.8x\n", pcs_and_sr[0],
         pcs_and_sr[2], r1);
}


x86 assembly (clang, -O0):
<dbg_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1060 <dbg_cpu0_write_ctrl@plt>
xor    %edi,%edi
lea    -0x19(%rbp),%rsi
call   1030 <dbg_cpu0_read_ctrl@plt>
movzbl -0x19(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    118f <dbg_test+0x4f>
movzbl -0x19(%rbp),%esi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x28(%rbp),%rsi
mov    $0x10,%edi
mov    $0xc,%edx
call   1040 <dbg_cpu0_read@plt>
mov    $0x401,%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <dbg_cpu0_read@plt>
mov    -0x28(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbg_test>:
sub    $0x18,%rsp
lea    0xef2(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1070 <dbg_cpu0_write_ctrl@plt>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
call   1030 <dbg_cpu0_read_ctrl@plt>
movzbl 0x7(%rsp),%esi
test   $0x1,%sil
jne    1191 <dbg_test+0x41>
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xc(%rsp),%rsi
mov    $0x10,%edi
mov    $0xc,%edx
call   1050 <dbg_cpu0_read@plt>
lea    0x8(%rsp),%rsi
mov    $0x401,%edi
mov    $0x4,%edx
call   1050 <dbg_cpu0_read@plt>
mov    0xc(%rsp),%esi
mov    0x14(%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [14 x i8] c" Stall or1k\0A\00"
@.str.1 = [32 x i8] c"\09or1k stall failed. read: 0x%x\0A\00"
@.str.2 = [20 x i8] c" Reading npc, ppc\0A\00"
@.str.3 = [14 x i8] c" Reading r1\0A\00"
@.str.4 = [45 x i8] c" Read npc = %.8x ppc = %.8x r1 = %.8x\0A\00"
define void @dbg_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca [3 x i32]
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @dbg_cpu0_write_ctrl(i32 0, i32 1)
%12 = call i32 @dbg_cpu0_read_ctrl(i32 0, i8* %7)
%13 = load i8, i8* %7
%14 = zext i8 %13 to i32
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %0
%18 = load i8, i8* %7
%19 = zext i8 %18 to i32
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %0
%22 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 0
%23 = call i32 @dbg_cpu0_read(i32 16, i32* %22, i32 12)
%24 = call i32 @dbg_cpu0_read(i32 1025, i32* %4, i32 4)
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [3 x i32], [3 x i32]* %9, i64 0, i64 2
%28 = load i32, i32* %27
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %28, i32 %29)
ret void
}

LLVM IR (-O2):
@.str.1 = [32 x i8] c"\09or1k stall failed. read: 0x%x\0A\00"
@.str.4 = [45 x i8] c" Read npc = %.8x ppc = %.8x r1 = %.8x\0A\00"
@str = [13 x i8] c" Stall or1k\00"
define void @dbg_test() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca [3 x i32]
%4 = bitcast i32* %1 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @dbg_cpu0_write_ctrl(i32 0, i32 1)
%7 = call i32 @dbg_cpu0_read_ctrl(i32 0, i8* %2)
%8 = load i8, i8* %2
%9 = zext i8 %8 to i32
%10 = and i32 %9, 1
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %14
14: ; preds = %12, %0
%15 = bitcast [3 x i32]* %3 to i8*
%16 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%17 = call i32 @dbg_cpu0_read(i32 16, i32* %16, i32 12)
%18 = call i32 @dbg_cpu0_read(i32 1025, i32* %1, i32 4)
%19 = load i32, i32* %16
%20 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%21 = load i32, i32* %20
%22 = load i32, i32* %1
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %19, i32 %21, i32 %22)
ret void
}