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

/* Variables and functions */
int /*<<< orphan*/ util_byte_to_hex(char *, int /*<<< orphan*/);
int gdb_write_hex_data(uint8_t *indata, int count, char *hexdata, int max) {
  int i = 0;
  int j = 0;
  uint8_t byte;
  unsigned char *src = (unsigned char *)indata;
  unsigned char *dst = (unsigned char *)hexdata;
  while (i < count) {
    if (j + 2 > max)
      break;
    byte = *(src++);
    util_byte_to_hex((char *)dst, byte);
    dst += 2;
    j += 2;
    i++;
  }
  *dst = '\0';
  return j;
}


x86 assembly (clang, -O0):
<gdb_write_hex_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a8 <gdb_write_hex_data+0x98>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
cmp    -0x1c(%rbp),%eax
jle    1164 <gdb_write_hex_data+0x54>
jmp    11a8 <gdb_write_hex_data+0x98>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <util_byte_to_hex@plt>
mov    -0x38(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1144 <gdb_write_hex_data+0x34>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_write_hex_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
test   %esi,%esi
jle    116d <gdb_write_hex_data+0x5d>
mov    %ecx,%r12d
mov    %rdi,%r13
mov    %esi,%ebp
add    %esi,%esi
mov    %esi,0x4(%rsp)
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r15,%rbx,1),%rdi
lea    0x2(%rbx),%eax
cmp    %r12d,%eax
jg     1171 <gdb_write_hex_data+0x61>
movzbl 0x0(%r13,%r14,1),%esi
call   1030 <util_byte_to_hex@plt>
add    $0x2,%rbx
add    $0x1,%r14
cmp    %r14d,%ebp
jne    1140 <gdb_write_hex_data+0x30>
add    %rbx,%r15
mov    0x4(%rsp),%eax
jmp    1176 <gdb_write_hex_data+0x66>
xor    %eax,%eax
jmp    1176 <gdb_write_hex_data+0x66>
mov    %ebx,%eax
mov    %rdi,%r15
movb   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gdb_write_hex_data(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%14 = load i32*, i32** %5
%15 = bitcast i32* %14 to i8*
store i8* %15, i8** %12
%16 = load i8*, i8** %7
store i8* %16, i8** %13
br label %17
17: ; preds = %27, %4
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 2
%24 = load i32, i32* %8
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %21
br label %41
27: ; preds = %21
%28 = load i8*, i8** %12
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %12
%30 = load i8, i8* %28
%31 = zext i8 %30 to i32
store i32 %31, i32* %11
%32 = load i8*, i8** %13
%33 = load i32, i32* %11
%34 = call i32 @util_byte_to_hex(i8* %32, i32 %33)
%35 = load i8*, i8** %13
%36 = getelementptr inbounds i8, i8* %35, i64 2
store i8* %36, i8** %13
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 2
store i32 %38, i32* %10
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
store i32 %40, i32* %9
br label %17
41: ; preds = %26, %17
%42 = load i8*, i8** %13
store i8 0, i8* %42
%43 = load i32, i32* %10
ret i32 %43
}

LLVM IR (-O2):
define i32 @gdb_write_hex_data(i32* nocapture %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %24
6: ; preds = %4
%7 = bitcast i32* %0 to i8*
%8 = shl nuw i32 %1, 1
br label %9
9: ; preds = %6, %16
%10 = phi i8* [ %21, %16 ], [ %2, %6 ]
%11 = phi i8* [ %17, %16 ], [ %7, %6 ]
%12 = phi i32 [ %22, %16 ], [ 0, %6 ]
%13 = phi i32 [ %14, %16 ], [ 0, %6 ]
%14 = add nuw nsw i32 %13, 2
%15 = icmp sgt i32 %14, %3
br i1 %15, label %24, label %16
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %11, i64 1
%18 = load i8, i8* %11
%19 = zext i8 %18 to i32
%20 = tail call i32 @util_byte_to_hex(i8* %10, i32 %19)
%21 = getelementptr inbounds i8, i8* %10, i64 2
%22 = add nuw nsw i32 %12, 1
%23 = icmp eq i32 %22, %1
br i1 %23, label %24, label %9
24: ; preds = %16, %9, %4
%25 = phi i32 [ 0, %4 ], [ %13, %9 ], [ %8, %16 ]
%26 = phi i8* [ %2, %4 ], [ %10, %9 ], [ %21, %16 ]
store i8 0, i8* %26
ret i32 %25
}