#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 */
struct state {
  int dummy;
};
struct debug_state {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ byte2ascii(int /*<<< orphan*/, char *);
int /*<<< orphan*/ get_byte_at(struct state *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
void cli_read(struct state *context, struct debug_state *dbg_context,
              char **args) {
  int start, nbytes, i;
  if (args[1] == NULL) {
    printf("insufficient arguments for read command\n");
  } else {
    start = strtol(args[1], NULL, 0);
  }
  if (args[2] == NULL) {
    nbytes = 16;
  } else {
    nbytes = strtol(args[2], NULL, 0);
  }
  if (start + nbytes > 0x10000) {
    printf("command reads out-of-bounds memory\n");
  } else {
    for (i = 0; i < nbytes; i += 2) {
      if (i % 16 == 0) {
        if (i) {
          printf("\n");
        }
        printf("0x%04x:  ", start + i);
      }
      char byteval[2];
      byte2ascii(get_byte_at(context, NULL, start + i), byteval);
      printf("%c%c", byteval[0], byteval[1]);
      byte2ascii(get_byte_at(context, NULL, start + i + 1), byteval);
      printf("%c%c ", byteval[0], byteval[1]);
    }
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<cli_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1176 <cli_read+0x36>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118c <cli_read+0x4c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    11a7 <cli_read+0x67>
movl   $0x10,-0x20(%rbp)
jmp    11bd <cli_read+0x7d>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
cmp    $0x10000,%eax
jle    11e1 <cli_read+0xa1>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bb <cli_read+0x17b>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12ad <cli_read+0x16d>
mov    -0x24(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1234 <cli_read+0xf4>
cmpl   $0x0,-0x24(%rbp)
je     1220 <cli_read+0xe0>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
add    -0x24(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    -0x24(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <get_byte_at@plt>
mov    %eax,%edi
lea    -0x26(%rbp),%rsi
call   1050 <byte2ascii@plt>
movsbl -0x26(%rbp),%esi
movsbl -0x25(%rbp),%edx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
add    -0x24(%rbp),%edx
add    $0x1,%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <get_byte_at@plt>
mov    %eax,%edi
lea    -0x26(%rbp),%rsi
call   1050 <byte2ascii@plt>
movsbl -0x26(%rbp),%esi
movsbl -0x25(%rbp),%edx
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    11e8 <cli_read+0xa8>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rdi,%rbx
mov    0x8(%rdx),%rdi
test   %rdi,%rdi
je     1188 <cli_read+0x28>
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r14d
jmp    1194 <cli_read+0x34>
lea    0xea9(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rbp),%rdi
test   %rdi,%rdi
je     11be <cli_read+0x5e>
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r15d
lea    (%r15,%r14,1),%eax
cmp    $0x10000,%eax
jg     11cd <cli_read+0x6d>
test   %r15d,%r15d
jg     11e7 <cli_read+0x87>
jmp    1316 <cli_read+0x1b6>
mov    $0x10,%r15d
cmp    $0xfff0,%r14d
jle    11e7 <cli_read+0x87>
lea    0xe41(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
lea    0xe12(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1080 <get_byte_at@plt>
lea    0x6(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
call   1070 <byte2ascii@plt>
movsbl 0x6(%rsp),%esi
movsbl 0x7(%rsp),%edx
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x1(%r14),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <get_byte_at@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1070 <byte2ascii@plt>
movsbl 0x6(%rsp),%esi
movsbl 0x7(%rsp),%edx
lea    0xdba(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x3,%r15d
jb     1316 <cli_read+0x1b6>
mov    $0x2,%r12d
lea    0x6(%rsp),%r13
jmp    1306 <cli_read+0x1a6>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    (%r12,%r14,1),%ebp
lea    0xd6b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1080 <get_byte_at@plt>
mov    %eax,%edi
mov    %r13,%rsi
call   1070 <byte2ascii@plt>
movsbl 0x6(%rsp),%esi
movsbl 0x7(%rsp),%edx
lea    0xd45(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1080 <get_byte_at@plt>
mov    %eax,%edi
mov    %r13,%rsi
call   1070 <byte2ascii@plt>
movsbl 0x6(%rsp),%esi
movsbl 0x7(%rsp),%edx
lea    0xd19(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x2,%r12d
cmp    %r15d,%r12d
jge    1316 <cli_read+0x1b6>
test   $0xe,%r12b
je     1280 <cli_read+0x120>
lea    (%r14,%r12,1),%ebp
jmp    129e <cli_read+0x13e>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.state = type { i32 }
%struct.debug_state = type { i32 }
@.str = [41 x i8] c"insufficient arguments for read command\0A\00"
@.str.1 = [36 x i8] c"command reads out-of-bounds memory\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [10 x i8] c"0x%04x: \00"
@.str.4 = [5 x i8] c"%c%c\00"
@.str.5 = [6 x i8] c"%c%c \00"
define void @cli_read(%struct.state* %0, %struct.debug_state* %1, i8** %2) {
%4 = alloca %struct.state*
%5 = alloca %struct.debug_state*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2 x i8]
store %struct.state* %0, %struct.state** %4
store %struct.debug_state* %1, %struct.debug_state** %5
store i8** %2, i8*** %6
%11 = load i8**, i8*** %6
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %22
17: ; preds = %3
%18 = load i8**, i8*** %6
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @strtol(i8* %20, i32* null, i32 0)
store i32 %21, i32* %7
br label %22
22: ; preds = %17, %15
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 16, i32* %8
br label %33
28: ; preds = %22
%29 = load i8**, i8*** %6
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @strtol(i8* %31, i32* null, i32 0)
store i32 %32, i32* %8
br label %33
33: ; preds = %28, %27
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = add nsw i32 %34, %35
%37 = icmp sgt i32 %36, 65536
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %94
40: ; preds = %33
store i32 0, i32* %9
br label %41
41: ; preds = %89, %40
%42 = load i32, i32* %9
%43 = load i32, i32* %8
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %92
45: ; preds = %41
%46 = load i32, i32* %9
%47 = srem i32 %46, 16
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %45
%50 = load i32, i32* %9
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = load i32, i32* %7
%56 = load i32, i32* %9
%57 = add nsw i32 %55, %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %54, %45
%60 = load %struct.state*, %struct.state** %4
%61 = load i32, i32* %7
%62 = load i32, i32* %9
%63 = add nsw i32 %61, %62
%64 = call i32 @get_byte_at(%struct.state* %60, i32* null, i32 %63)
%65 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%66 = call i32 @byte2ascii(i32 %64, i8* %65)
%67 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %69, i32 %72)
%74 = load %struct.state*, %struct.state** %4
%75 = load i32, i32* %7
%76 = load i32, i32* %9
%77 = add nsw i32 %75, %76
%78 = add nsw i32 %77, 1
%79 = call i32 @get_byte_at(%struct.state* %74, i32* null, i32 %78)
%80 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%81 = call i32 @byte2ascii(i32 %79, i8* %80)
%82 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%83 = load i8, i8* %82
%84 = sext i8 %83 to i32
%85 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %84, i32 %87)
br label %89
89: ; preds = %59
%90 = load i32, i32* %9
%91 = add nsw i32 %90, 2
store i32 %91, i32* %9
br label %41
92: ; preds = %41
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %94
94: ; preds = %92, %38
ret void
}

LLVM IR (-O2):
%struct.state = type { i32 }
%struct.debug_state = type { i32 }
@.str.3 = [10 x i8] c"0x%04x: \00"
@.str.4 = [5 x i8] c"%c%c\00"
@.str.5 = [6 x i8] c"%c%c \00"
@str = [35 x i8] c"command reads out-of-bounds memory\00"
@str.6 = [40 x i8] c"insufficient arguments for read command\00"
define void @cli_read(%struct.state* %0, %struct.debug_state* nocapture readnone %1, i8** nocapture %2) {
%4 = alloca [2 x i8]
%5 = getelementptr inbounds i8*, i8** %2, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.6, i64 0, i64 0))
br label %12
10: ; preds = %3
%11 = tail call i32 @strtol(i8* nocapture %6, i32* null, i32 0)
br label %12
12: ; preds = %10, %8
%13 = phi i32 [ undef, %8 ], [ %11, %10 ]
%14 = getelementptr inbounds i8*, i8** %2, i64 2
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 0)
%19 = add nsw i32 %18, %13
%20 = icmp sgt i32 %19, 65536
br i1 %20, label %46, label %23
21: ; preds = %12
%22 = icmp sgt i32 %13, 65520
br i1 %22, label %46, label %25
23: ; preds = %17
%24 = icmp sgt i32 %18, 0
br i1 %24, label %25, label %77
25: ; preds = %21, %23
%26 = phi i32 [ %18, %23 ], [ 16, %21 ]
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%30 = call i32 @get_byte_at(%struct.state* %0, i32* null, i32 %13)
%31 = call i32 @byte2ascii(i32 %30, i8* %27)
%32 = load i8, i8* %27
%33 = sext i8 %32 to i32
%34 = load i8, i8* %28
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %33, i32 %35)
%37 = add nsw i32 %13, 1
%38 = call i32 @get_byte_at(%struct.state* %0, i32* null, i32 %37)
%39 = call i32 @byte2ascii(i32 %38, i8* %27)
%40 = load i8, i8* %27
%41 = sext i8 %40 to i32
%42 = load i8, i8* %28
%43 = sext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %41, i32 %43)
%45 = icmp ugt i32 %26, 2
br i1 %45, label %48, label %77
46: ; preds = %21, %17
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %79
48: ; preds = %25, %58
%49 = phi i32 [ %75, %58 ], [ 2, %25 ]
%50 = and i32 %49, 14
%51 = icmp eq i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %48
%53 = add nsw i32 %49, %13
br label %58
54: ; preds = %48
%55 = call i32 @putchar(i32 10)
%56 = add nsw i32 %49, %13
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %52, %54
%59 = phi i32 [ %53, %52 ], [ %56, %54 ]
%60 = call i32 @get_byte_at(%struct.state* %0, i32* null, i32 %59)
%61 = call i32 @byte2ascii(i32 %60, i8* %27)
%62 = load i8, i8* %27
%63 = sext i8 %62 to i32
%64 = load i8, i8* %28
%65 = sext i8 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %63, i32 %65)
%67 = add nsw i32 %59, 1
%68 = call i32 @get_byte_at(%struct.state* %0, i32* null, i32 %67)
%69 = call i32 @byte2ascii(i32 %68, i8* %27)
%70 = load i8, i8* %27
%71 = sext i8 %70 to i32
%72 = load i8, i8* %28
%73 = sext i8 %72 to i32
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %71, i32 %73)
%75 = add nuw nsw i32 %49, 2
%76 = icmp slt i32 %75, %26
br i1 %76, label %48, label %77
77: ; preds = %58, %25, %23
%78 = call i32 @putchar(i32 10)
br label %79
79: ; preds = %77, %46
ret void
}