#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_bin_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_bin(char *gdb_in_packet, int packet_len) {
  char ok_resp[] = "OK";
  uint32_t addr;
  uint32_t bytes;
  int len;
  const int scratch_len = 16;
  char scratchpad[scratch_len];
  if (packet_len > 1) {
    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_bin_data((uint8_t *)gdb_in_packet, (int)bytes, (uint8_t *)addr,
                      1024);
    gdb_send_packet(ok_resp, util_str_len(ok_resp));
  }
}


x86 assembly (clang, -O0):
<gdb_cmd_write_mem_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xe9a(%rip),%ax
mov    %ax,-0xf(%rbp)
mov    0xe92(%rip),%al
mov    %al,-0xd(%rbp)
movl   $0x10,-0x28(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    1251 <gdb_cmd_write_mem_bin+0x101>
mov    -0x8(%rbp),%rsi
lea    -0x40(%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    -0x40(%rbp),%rdi
call   1060 <util_hex_to_word@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x40(%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    -0x40(%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_bin_data@plt>
lea    -0xf(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0xf(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_cmd_write_mem_bin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%r12
movb   $0x0,0xe(%rsp)
movw   $0x4b4f,0xc(%rsp)
cmp    $0x2,%esi
jl     11fa <gdb_cmd_write_mem_bin+0xaa>
mov    %rdi,%rbx
lea    0x10(%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),%r13
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,%r13,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_bin_data@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <util_str_len@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
mov    %r12,%rsp
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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