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

/* Variables and functions */
int dgb_add_watchpoint(int, int, int);
int /*<<< orphan*/ gdb_send_packet(char *, int /*<<< orphan*/);
int /*<<< orphan*/ util_byte_to_hex(char *, unsigned char);
int util_cpy_substr(char *, char *, char, int const);
int util_hex_to_word(char *);
int /*<<< orphan*/ util_str_len(char *);
void gdb_cmd_add_watchpoint(char *gdb_in_packet, int packet_len) {
  int len;
  int result;
  uint32_t addr;
  uint32_t bytes;
  uint32_t type;
  uint8_t tmp;
  char *okresp = "OK";
  const int scratch_len = 16;
  char scratchpad[scratch_len];
  switch (*gdb_in_packet) {
  case '2':
    type = 2;
    break;
  case '3':
    type = 1;
    break;
  case '4':
    type = 3;
    break;
  default:
    return;
    break;
  }
  gdb_in_packet += 2;
  packet_len -= 2;
  len = util_cpy_substr(scratchpad, (char *)gdb_in_packet, ',', scratch_len);
  gdb_in_packet += len + 1;
  addr = util_hex_to_word(scratchpad);
  bytes = util_hex_to_word((char *)gdb_in_packet);
  result = dgb_add_watchpoint(type, addr, bytes);
  if (result < 0) {
    tmp = (uint8_t)(-result);
    scratchpad[0] = 'E';
    util_byte_to_hex(scratchpad + 1, (unsigned char)tmp);
    gdb_send_packet(scratchpad, util_str_len(scratchpad));
  } else {
    gdb_send_packet(okresp, util_str_len(okresp));
  }
}


x86 assembly (clang, -O0):
<gdb_cmd_add_watchpoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x10,-0x34(%rbp)
mov    %rsp,%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x64(%rbp)
sub    $0x32,%eax
je     11c2 <gdb_cmd_add_watchpoint+0x62>
jmp    11a0 <gdb_cmd_add_watchpoint+0x40>
mov    -0x64(%rbp),%eax
sub    $0x33,%eax
je     11ce <gdb_cmd_add_watchpoint+0x6e>
jmp    11b1 <gdb_cmd_add_watchpoint+0x51>
mov    -0x64(%rbp),%eax
sub    $0x34,%eax
je     11da <gdb_cmd_add_watchpoint+0x7a>
jmp    11e6 <gdb_cmd_add_watchpoint+0x86>
movl   $0x2,-0x20(%rbp)
jmp    11f2 <gdb_cmd_add_watchpoint+0x92>
movl   $0x1,-0x20(%rbp)
jmp    11f2 <gdb_cmd_add_watchpoint+0x92>
movl   $0x3,-0x20(%rbp)
jmp    11f2 <gdb_cmd_add_watchpoint+0x92>
movl   $0x1,-0x44(%rbp)
jmp    12c7 <gdb_cmd_add_watchpoint+0x167>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x60(%rbp),%rdi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <util_hex_to_word@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <util_hex_to_word@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1070 <dgb_add_watchpoint@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a4 <gdb_cmd_add_watchpoint+0x144>
xor    %eax,%eax
sub    -0x14(%rbp),%eax
cltq
mov    %rax,-0x28(%rbp)
movb   $0x45,-0x60(%rbp)
lea    -0x60(%rbp),%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rax
movzbl %al,%esi
call   1080 <util_byte_to_hex@plt>
lea    -0x60(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    %eax,%esi
lea    -0x60(%rbp),%rdi
call   1050 <gdb_send_packet@plt>
jmp    12c0 <gdb_cmd_add_watchpoint+0x160>
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_cmd_add_watchpoint>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movsbl (%rdi),%eax
lea    -0x32(%rax),%ecx
cmp    $0x2,%ecx
ja     1200 <gdb_cmd_add_watchpoint+0xa0>
mov    %rdi,%rbx
cltq
lea    0xe81(%rip),%rcx
mov    -0xc8(%rcx,%rax,4),%r14d
ff
lea    0x2(%rdi),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1040 <util_cpy_substr@plt>
cltq
add    %rax,%rbx
add    $0x3,%rbx
mov    %rbp,%rdi
call   1060 <util_hex_to_word@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <util_hex_to_word@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <dgb_add_watchpoint@plt>
test   %eax,%eax
js     11d8 <gdb_cmd_add_watchpoint+0x78>
lea    0xe2a(%rip),%rbx
jmp    11ee <gdb_cmd_add_watchpoint+0x8e>
movb   $0x45,(%rsp)
lea    0x1(%rsp),%rdi
neg    %al
movzbl %al,%esi
call   1080 <util_byte_to_hex@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <util_str_len@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <gdb_send_packet@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"OK\00"
define void @gdb_cmd_add_watchpoint(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
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** %11
store i32 16, i32* %12
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %13
%16 = alloca i8, i64 16
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
switch i32 %19, label %23 [
i32 50, label %20
i32 51, label %21
i32 52, label %22
]
20: ; preds = %2
store i32 2, i32* %9
br label %24
21: ; preds = %2
store i32 1, i32* %9
br label %24
22: ; preds = %2
store i32 3, i32* %9
br label %24
23: ; preds = %2
store i32 1, i32* %14
br label %62
24: ; preds = %22, %21, %20
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 2
store i8* %26, i8** %3
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 2
store i32 %28, i32* %4
%29 = load i8*, i8** %3
%30 = call i32 @util_cpy_substr(i8* %16, i8* %29, i8 signext 44, i32 16)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%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 = call i32 @util_hex_to_word(i8* %16)
store i32 %36, i32* %7
%37 = load i8*, i8** %3
%38 = call i32 @util_hex_to_word(i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = call i32 @dgb_add_watchpoint(i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %24
%46 = load i32, i32* %6
%47 = sub nsw i32 0, %46
%48 = sext i32 %47 to i64
store i64 %48, i64* %10
%49 = getelementptr inbounds i8, i8* %16, i64 0
store i8 69, i8* %49
%50 = getelementptr inbounds i8, i8* %16, i64 1
%51 = load i64, i64* %10
%52 = trunc i64 %51 to i8
%53 = call i32 @util_byte_to_hex(i8* %50, i8 zeroext %52)
%54 = call i32 @util_str_len(i8* %16)
%55 = call i32 @gdb_send_packet(i8* %16, i32 %54)
br label %61
56: ; preds = %24
%57 = load i8*, i8** %11
%58 = load i8*, i8** %11
%59 = call i32 @util_str_len(i8* %58)
%60 = call i32 @gdb_send_packet(i8* %57, i32 %59)
br label %61
61: ; preds = %56, %45
store i32 0, i32* %14
br label %62
62: ; preds = %61, %23
%63 = load i8*, i8** %13
%64 = load i32, i32* %14
switch i32 %64, label %66 [
i32 0, label %65
i32 1, label %65
]
65: ; preds = %62, %62
ret void
66: ; preds = %62
unreachable
}

LLVM IR (-O2):
@.str = [3 x i8] c"OK\00"
@switch.table.gdb_cmd_add_watchpoint = [3 x i32] [i32 2, i32 1, i32 3]
define void @gdb_cmd_add_watchpoint(i8* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %0
%6 = sext i8 %5 to i32
%7 = add nsw i32 %6, -50
%8 = icmp ult i32 %7, 3
br i1 %8, label %9, label %32
9: ; preds = %2
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.gdb_cmd_add_watchpoint, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = getelementptr inbounds i8, i8* %0, i64 2
%14 = call i32 @util_cpy_substr(i8* %4, i8* %13, i8 signext 44, i32 16)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = call i32 @util_hex_to_word(i8* %4)
%19 = call i32 @util_hex_to_word(i8* %17)
%20 = call i32 @dgb_add_watchpoint(i32 %12, i32 %18, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %9
store i8 69, i8* %4
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 1
%24 = trunc i32 %20 to i8
%25 = sub i8 0, %24
%26 = call i32 @util_byte_to_hex(i8* %23, i8 zeroext %25)
%27 = call i32 @util_str_len(i8* %4)
%28 = call i32 @gdb_send_packet(i8* %4, i32 %27)
br label %32
29: ; preds = %9
%30 = call i32 @util_str_len(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %2, %22, %29
ret void
}