#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ conv32network_endian(int *, int);
size_t cmd_read_memory(uint32_t **bufio, uint32_t addr, uint32_t n) {
  size_t idx;
  uint32_t *buf;
  buf = *bufio;
  if (buf == NULL) {
    buf = (uint32_t *)calloc(4, sizeof(uint32_t));
    if (buf == NULL)
      return -1;
    *bufio = buf;
  }
  idx = 0;
  buf[idx++] = 0x00110000 | (0x0000ffff & addr);
  buf[idx++] = 0x00120000 | (0x0000ffff & (addr >> 16));
  buf[idx++] = 0x00100000 | (0x0000ffff & n);
  buf[idx++] = 0x80140000;
  conv32network_endian(buf, 4);
  return 4 * sizeof(uint32_t);
}


x86 assembly (clang, -O0):
<cmd_read_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117b <cmd_read_memory+0x5b>
mov    $0x4,%esi
mov    %esi,%edi
call   1030 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1170 <cmd_read_memory+0x50>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1225 <cmd_read_memory+0x105>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edx
and    $0xffff,%edx
or     $0x110000,%edx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%edx
sar    $0x10,%edx
and    $0xffff,%edx
or     $0x120000,%edx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%edx
and    $0xffff,%edx
or     $0x100000,%edx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movl   $0x80140000,(%rax,%rcx,4)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1040 <conv32network_endian@plt>
movq   $0x10,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_read_memory>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
jne    114e <cmd_read_memory+0x2e>
mov    $0x4,%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     118b <cmd_read_memory+0x6b>
mov    %rax,%rdi
mov    %rax,(%rbx)
movzwl %bp,%eax
or     $0x110000,%eax
mov    %eax,(%rdi)
shr    $0x10,%ebp
or     $0x120000,%ebp
mov    %ebp,0x4(%rdi)
movzwl %r14w,%eax
or     $0x100000,%eax
mov    %eax,0x8(%rdi)
movl   $0x80140000,0xc(%rdi)
mov    $0x4,%esi
call   1040 <conv32network_endian@plt>
mov    $0x10,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffffffffffff,%rax
jmp    1186 <cmd_read_memory+0x66>

LLVM IR (-O0):
define i64 @cmd_read_memory(i32** %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
store i32** %0, i32*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32**, i32*** %5
%11 = load i32*, i32** %10
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = call i64 @calloc(i32 4, i32 4)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
store i64 -1, i64* %4
br label %52
20: ; preds = %14
%21 = load i32*, i32** %9
%22 = load i32**, i32*** %5
store i32* %21, i32** %22
br label %23
23: ; preds = %20, %3
store i64 0, i64* %8
%24 = load i32, i32* %6
%25 = and i32 65535, %24
%26 = or i32 1114112, %25
%27 = load i32*, i32** %9
%28 = load i64, i64* %8
%29 = add i64 %28, 1
store i64 %29, i64* %8
%30 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 %26, i32* %30
%31 = load i32, i32* %6
%32 = ashr i32 %31, 16
%33 = and i32 65535, %32
%34 = or i32 1179648, %33
%35 = load i32*, i32** %9
%36 = load i64, i64* %8
%37 = add i64 %36, 1
store i64 %37, i64* %8
%38 = getelementptr inbounds i32, i32* %35, i64 %36
store i32 %34, i32* %38
%39 = load i32, i32* %7
%40 = and i32 65535, %39
%41 = or i32 1048576, %40
%42 = load i32*, i32** %9
%43 = load i64, i64* %8
%44 = add i64 %43, 1
store i64 %44, i64* %8
%45 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 %41, i32* %45
%46 = load i32*, i32** %9
%47 = load i64, i64* %8
%48 = add i64 %47, 1
store i64 %48, i64* %8
%49 = getelementptr inbounds i32, i32* %46, i64 %47
store i32 -2146172928, i32* %49
%50 = load i32*, i32** %9
%51 = call i32 @conv32network_endian(i32* %50, i32 4)
store i64 16, i64* %4
br label %52
52: ; preds = %23, %19
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
define i64 @cmd_read_memory(i32** nocapture %0, i32 %1, i32 %2) {
%4 = load i32*, i32** %0
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = tail call i64 @calloc(i32 4, i32 4)
%8 = inttoptr i64 %7 to i32*
%9 = icmp eq i64 %7, 0
br i1 %9, label %23, label %10
10: ; preds = %6
store i32* %8, i32** %0
br label %11
11: ; preds = %10, %3
%12 = phi i32* [ %8, %10 ], [ %4, %3 ]
%13 = and i32 %1, 65535
%14 = or i32 %13, 1114112
store i32 %14, i32* %12
%15 = lshr i32 %1, 16
%16 = or i32 %15, 1179648
%17 = getelementptr inbounds i32, i32* %12, i64 1
store i32 %16, i32* %17
%18 = and i32 %2, 65535
%19 = or i32 %18, 1048576
%20 = getelementptr inbounds i32, i32* %12, i64 2
store i32 %19, i32* %20
%21 = getelementptr inbounds i32, i32* %12, i64 3
store i32 -2146172928, i32* %21
%22 = tail call i32 @conv32network_endian(i32* %12, i32 4)
br label %23
23: ; preds = %6, %11
%24 = phi i64 [ 16, %11 ], [ -1, %6 ]
ret i64 %24
}