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

/* Variables and functions */
scalar_t__ dgb_set_trap(void *, int);
int /*<<< orphan*/ gdb_response_not_supported();
int /*<<< orphan*/ gdb_send_packet(char *, int /*<<< orphan*/);
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_breakpoint(char *gdb_in_packet, int packet_len) {
  int len;
  uint32_t addr;
  uint32_t kind;
  char *err = "E01";
  char *okresp = "OK";
  const int scratch_len = 16;
  char scratchpad[scratch_len];
  if (packet_len < 1) {
    gdb_send_packet(err, util_str_len(err));
    return;
  }
  if (*gdb_in_packet == ',')
    gdb_in_packet++;
  len = util_cpy_substr(scratchpad, (char *)gdb_in_packet, ',', scratch_len);
  gdb_in_packet += len + 1;
  addr = util_hex_to_word(scratchpad);
  kind = util_hex_to_word((char *)gdb_in_packet);
  if (kind == 2) {
    gdb_response_not_supported();
  } else if (kind == 4) {
    if (dgb_set_trap((void *)addr, 1)) {
      gdb_send_packet(err, util_str_len(err));
    } else {
      gdb_send_packet(okresp, util_str_len(okresp));
    }
  } else {
    gdb_response_not_supported();
  }
}


x86 assembly (clang, -O0):
<gdb_cmd_add_breakpoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe8a(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x10,-0x2c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    11c5 <gdb_cmd_add_breakpoint+0x65>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1070 <gdb_send_packet@plt>
movl   $0x1,-0x3c(%rbp)
jmp    12b4 <gdb_cmd_add_breakpoint+0x154>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2c,%eax
jne    11e1 <gdb_cmd_add_breakpoint+0x81>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x50(%rbp),%rdi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1050 <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    -0x50(%rbp),%rdi
call   1080 <util_hex_to_word@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <util_hex_to_word@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x18(%rbp)
jne    123d <gdb_cmd_add_breakpoint+0xdd>
mov    $0x0,%al
call   1040 <gdb_response_not_supported@plt>
jmp    12ad <gdb_cmd_add_breakpoint+0x14d>
cmpl   $0x4,-0x18(%rbp)
jne    12a1 <gdb_cmd_add_breakpoint+0x141>
movslq -0x14(%rbp),%rdi
mov    $0x1,%esi
call   1060 <dgb_set_trap@plt>
cmp    $0x0,%rax
je     1280 <gdb_cmd_add_breakpoint+0x120>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1070 <gdb_send_packet@plt>
jmp    129c <gdb_cmd_add_breakpoint+0x13c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <util_str_len@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%esi
call   1070 <gdb_send_packet@plt>
jmp    12a8 <gdb_cmd_add_breakpoint+0x148>
mov    $0x0,%al
call   1040 <gdb_response_not_supported@plt>
jmp    12ad <gdb_cmd_add_breakpoint+0x14d>
movl   $0x0,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_cmd_add_breakpoint>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
jle    11bb <gdb_cmd_add_breakpoint+0x5b>
xor    %ebx,%ebx
cmpb   $0x2c,(%rdi)
sete   %bl
add    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x2c,%edx
mov    $0x10,%ecx
call   1050 <util_cpy_substr@plt>
cltq
add    %rax,%rbx
add    $0x1,%rbx
mov    %r14,%rdi
call   1080 <util_hex_to_word@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <util_hex_to_word@plt>
cmp    $0x4,%eax
je     11dc <gdb_cmd_add_breakpoint+0x7c>
cmp    $0x2,%eax
xor    %eax,%eax
call   1040 <gdb_response_not_supported@plt>
jmp    1210 <gdb_cmd_add_breakpoint+0xb0>
lea    0xe3e(%rip),%rbx
mov    %rbx,%rdi
call   1030 <util_str_len@plt>
mov    %rbx,%rdi
mov    %eax,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <gdb_send_packet@plt>
movslq %ebp,%rdi
mov    $0x1,%esi
call   1060 <dgb_set_trap@plt>
test   %rax,%rax
je     11f7 <gdb_cmd_add_breakpoint+0x97>
lea    0xe0b(%rip),%rbx
jmp    11fe <gdb_cmd_add_breakpoint+0x9e>
lea    0xe06(%rip),%rbx
mov    %rbx,%rdi
call   1030 <util_str_len@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <gdb_send_packet@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"E01\00"
@.str.1 = [3 x i8] c"OK\00"
define void @gdb_cmd_add_breakpoint(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %8
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %9
store i32 16, i32* %10
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %11
%14 = alloca i8, i64 16
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8*, i8** %8
%19 = load i8*, i8** %8
%20 = call i32 @util_str_len(i8* %19)
%21 = call i32 @gdb_send_packet(i8* %18, i32 %20)
store i32 1, i32* %12
br label %69
22: ; preds = %2
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 44
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %3
br label %30
30: ; preds = %27, %22
%31 = load i8*, i8** %3
%32 = call i32 @util_cpy_substr(i8* %14, i8* %31, i8 signext 44, i32 16)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
%35 = load i8*, i8** %3
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %3
%38 = call i32 @util_hex_to_word(i8* %14)
store i32 %38, i32* %6
%39 = load i8*, i8** %3
%40 = call i32 @util_hex_to_word(i8* %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 2
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = call i32 (...) @gdb_response_not_supported()
br label %68
45: ; preds = %30
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, 4
br i1 %47, label %48, label %65
48: ; preds = %45
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = inttoptr i64 %50 to i8*
%52 = call i64 @dgb_set_trap(i8* %51, i32 1)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i8*, i8** %8
%56 = load i8*, i8** %8
%57 = call i32 @util_str_len(i8* %56)
%58 = call i32 @gdb_send_packet(i8* %55, i32 %57)
br label %64
59: ; preds = %48
%60 = load i8*, i8** %9
%61 = load i8*, i8** %9
%62 = call i32 @util_str_len(i8* %61)
%63 = call i32 @gdb_send_packet(i8* %60, i32 %62)
br label %64
64: ; preds = %59, %54
br label %67
65: ; preds = %45
%66 = call i32 (...) @gdb_response_not_supported()
br label %67
67: ; preds = %65, %64
br label %68
68: ; preds = %67, %43
store i32 0, i32* %12
br label %69
69: ; preds = %68, %17
%70 = load i8*, i8** %11
%71 = load i32, i32* %12
switch i32 %71, label %73 [
i32 0, label %72
i32 1, label %72
]
72: ; preds = %69, %69
ret void
73: ; preds = %69
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"E01\00"
@.str.1 = [3 x i8] c"OK\00"
define void @gdb_cmd_add_breakpoint(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 slt i32 %1, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @util_str_len(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %35
9: ; preds = %2
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 44
%12 = zext i1 %11 to i64
%13 = getelementptr i8, i8* %0, i64 %12
%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)
switch i32 %19, label %33 [
i32 2, label %20
i32 4, label %22
]
20: ; preds = %9
%21 = call i32 (...) @gdb_response_not_supported()
br label %35
22: ; preds = %9
%23 = sext i32 %18 to i64
%24 = inttoptr i64 %23 to i8*
%25 = call i64 @dgb_set_trap(i8* %24, i32 1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = call i32 @util_str_len(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %35
30: ; preds = %22
%31 = call i32 @util_str_len(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %35
33: ; preds = %9
%34 = call i32 (...) @gdb_response_not_supported()
br label %35
35: ; preds = %20, %27, %30, %33, %6
ret void
}