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

/* Variables and functions */
int /*<<< orphan*/ gdb_read_hex_data(int /*<<< orphan*/ *, int,
                                     int /*<<< orphan*/ *, int);
int /*<<< orphan*/ gdb_send_packet(char *, int /*<<< orphan*/);
int util_cpy_substr(char *, char *, char, int const);
scalar_t__ util_hex_to_word(char *);
int /*<<< orphan*/ util_str_len(char *);
void gdb_cmd_write_mem_hex(char *gdb_in_packet, int packet_len) {
  uint32_t addr;
  uint32_t bytes;
  int len;
  char *resp_str = "OK";
  const int scratch_len = 16;
  char scratchpad[scratch_len];
  if (packet_len > 0) {
    len = util_cpy_substr(scratchpad, (char *)gdb_in_packet, ',', scratch_len);
    gdb_in_packet += len + 1;
    packet_len -= (len + 1);
    addr = util_hex_to_word(scratchpad);
    len = util_cpy_substr(scratchpad, (char *)gdb_in_packet, ':', scratch_len);
    gdb_in_packet += len + 1;
    packet_len -= (len + 1);
    bytes = util_hex_to_word(scratchpad);
    gdb_read_hex_data((uint8_t *)gdb_in_packet, (int)bytes, (uint8_t *)addr,
                      1024);
    gdb_send_packet(resp_str, util_str_len(resp_str));
  }
}


x86 assembly (clang, -O0):
<gdb_cmd_write_mem_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x10,-0x34(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1248 <gdb_cmd_write_mem_hex+0xf8>
mov    -0x8(%rbp),%rsi
lea    -0x50(%rbp),%rdi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <util_hex_to_word@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x50(%rbp),%rdi
mov    $0x3a,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <util_hex_to_word@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
mov    $0x400,%ecx
call   1070 <gdb_read_hex_data@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_cmd_write_mem_hex>:
test   %esi,%esi
jle    11f3 <gdb_cmd_write_mem_hex+0xa3>
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
cltq
lea    (%rax,%rbx,1),%r12
add    %rax,%rbx
add    $0x1,%rbx
mov    %r14,%rdi
call   1060 <util_hex_to_word@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x3a,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
cltq
lea    (%rax,%r12,1),%rbx
add    $0x2,%rbx
mov    %r14,%rdi
call   1060 <util_hex_to_word@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r15,%rdx
mov    $0x400,%ecx
call   1070 <gdb_read_hex_data@plt>
lea    0xe2a(%rip),%rbx
mov    %rbx,%rdi
call   1030 <util_str_len@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"OK\00"
define void @gdb_cmd_write_mem_hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %8
store i32 16, i32* %9
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %10
%12 = alloca i8, i64 16
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i32 @util_cpy_substr(i8* %12, i8* %16, i8 signext 44, i32 16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = load i8*, i8** %3
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %3
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %4
%27 = call i64 @util_hex_to_word(i8* %12)
store i64 %27, i64* %5
%28 = load i8*, i8** %3
%29 = call i32 @util_cpy_substr(i8* %12, i8* %28, i8 signext 58, i32 16)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
%32 = load i8*, i8** %3
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %3
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
%37 = load i32, i32* %4
%38 = sub nsw i32 %37, %36
store i32 %38, i32* %4
%39 = call i64 @util_hex_to_word(i8* %12)
store i64 %39, i64* %6
%40 = load i8*, i8** %3
%41 = bitcast i8* %40 to i32*
%42 = load i64, i64* %6
%43 = trunc i64 %42 to i32
%44 = load i64, i64* %5
%45 = inttoptr i64 %44 to i32*
%46 = call i32 @gdb_read_hex_data(i32* %41, i32 %43, i32* %45, i32 1024)
%47 = load i8*, i8** %8
%48 = load i8*, i8** %8
%49 = call i32 @util_str_len(i8* %48)
%50 = call i32 @gdb_send_packet(i8* %47, i32 %49)
br label %51
51: ; preds = %15, %2
%52 = load i8*, i8** %10
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"OK\00"
define void @gdb_cmd_write_mem_hex(i8* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %23
6: ; preds = %2
%7 = call i32 @util_cpy_substr(i8* %4, i8* %0, i8 signext 44, i32 16)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = call i64 @util_hex_to_word(i8* %4)
%12 = call i32 @util_cpy_substr(i8* %4, i8* %10, i8 signext 58, i32 16)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %10, i64 %14
%16 = call i64 @util_hex_to_word(i8* %4)
%17 = bitcast i8* %15 to i32*
%18 = trunc i64 %16 to i32
%19 = inttoptr i64 %11 to i32*
%20 = call i32 @gdb_read_hex_data(i32* %17, i32 %18, i32* %19, i32 1024)
%21 = call i32 @util_str_len(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %6, %2
ret void
}