#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;
typedef int uint16_t;
struct _memory {
  int dummy;
};

/* Variables and functions */
int disasm_sweet16(struct _memory *, int, char *, int *, int *);
int memory_read16_m(struct _memory *, int);
int /*<<< orphan*/ printf(char *, ...);
void disasm_range_sweet16(struct _memory *memory, uint32_t flags,
                          uint32_t start, uint32_t end) {
  char instruction[128];
  int cycles_min = 0, cycles_max = 0;
  int count;
  uint16_t opcode;
  printf("\n");
  printf("%-7s %-5s %-40s\n", "Addr", "Opcode", "Instruction");
  printf("------- ------ ----------------------------------       ------\n");
  while (start <= end) {
    count =
        disasm_sweet16(memory, start, instruction, &cycles_min, &cycles_max);
    opcode = memory_read16_m(memory, start);
    printf("0x%04x: 0x%04x %-40s", start / 2, opcode, instruction);
    start = start + count;
  }
}


x86 assembly (clang, -O0):
<disasm_range_sweet16>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0xa4(%rbp)
movl   $0x0,-0xa8(%rbp)
lea    0xf04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8f(%rip),%rdi
lea    0xe99(%rip),%rsi
lea    0xe97(%rip),%rdx
lea    0xe97(%rip),%rcx
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1219 <disasm_range_sweet16+0xe9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    -0xa4(%rbp),%rcx
lea    -0xa8(%rbp),%r8
call   1050 <disasm_sweet16@plt>
mov    %eax,-0xac(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <memory_read16_m@plt>
mov    %eax,-0xb0(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0xb0(%rbp),%edx
lea    -0xa0(%rbp),%rcx
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    -0xac(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    119b <disasm_range_sweet16+0x6b>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disasm_range_sweet16>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe76(%rip),%rdi
lea    0xe80(%rip),%rsi
lea    0xe7e(%rip),%rdx
lea    0xe7e(%rip),%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe91(%rip),%rdi
call   1050 <puts@plt>
cmp    %r14d,%ebp
jg     1214 <disasm_range_sweet16+0xc4>
lea    0x10(%rsp),%r15
lea    0xe66(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
call   1070 <disasm_sweet16@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <memory_read16_m@plt>
mov    %ebp,%esi
shr    $0x1f,%esi
add    %ebp,%esi
sar    %esi
mov    %r12,%rdi
mov    %eax,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
add    %r13d,%ebp
cmp    %r14d,%ebp
jle    11d0 <disasm_range_sweet16+0x80>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct._memory = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [17 x i8] c"%-7s %-5s %-40s\0A\00"
@.str.2 = [5 x i8] c"Addr\00"
@.str.3 = [7 x i8] c"Opcode\00"
@.str.4 = [12 x i8] c"Instruction\00"
@.str.5 = [64 x i8] c"------- ------ ---------------------------------- ------\0A\00"
@.str.6 = [21 x i8] c"0x%04x: 0x%04x %-40s\00"
define void @disasm_range_sweet16(%struct._memory* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct._memory*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct._memory* %0, %struct._memory** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.5, i64 0, i64 0))
br label %17
17: ; preds = %21, %4
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %37
21: ; preds = %17
%22 = load %struct._memory*, %struct._memory** %5
%23 = load i32, i32* %7
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%25 = call i32 @disasm_sweet16(%struct._memory* %22, i32 %23, i8* %24, i32* %10, i32* %11)
store i32 %25, i32* %12
%26 = load %struct._memory*, %struct._memory** %5
%27 = load i32, i32* %7
%28 = call i32 @memory_read16_m(%struct._memory* %26, i32 %27)
store i32 %28, i32* %13
%29 = load i32, i32* %7
%30 = sdiv i32 %29, 2
%31 = load i32, i32* %13
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %30, i32 %31, i8* %32)
%34 = load i32, i32* %7
%35 = load i32, i32* %12
%36 = add nsw i32 %34, %35
store i32 %36, i32* %7
br label %17
37: ; preds = %17
ret void
}

LLVM IR (-O2):
%struct._memory = type { i32 }
@.str.1 = [17 x i8] c"%-7s %-5s %-40s\0A\00"
@.str.2 = [5 x i8] c"Addr\00"
@.str.3 = [7 x i8] c"Opcode\00"
@.str.4 = [12 x i8] c"Instruction\00"
@.str.6 = [21 x i8] c"0x%04x: 0x%04x %-40s\00"
@str = [63 x i8] c"------- ------ ---------------------------------- ------\00"
define void @disasm_range_sweet16(%struct._memory* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%10 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%11 = tail call i32 @putchar(i32 10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @str, i64 0, i64 0))
%14 = icmp sgt i32 %2, %3
br i1 %14, label %23, label %15
15: ; preds = %4, %15
%16 = phi i32 [ %21, %15 ], [ %2, %4 ]
%17 = call i32 @disasm_sweet16(%struct._memory* %0, i32 %16, i8* %8, i32* %6, i32* %7)
%18 = call i32 @memory_read16_m(%struct._memory* %0, i32 %16)
%19 = sdiv i32 %16, 2
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %19, i32 %18, i8* %8)
%21 = add nsw i32 %17, %16
%22 = icmp sgt i32 %21, %3
br i1 %22, label %23, label %15
23: ; preds = %15, %4
ret void
}