#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 */

/* Variables and functions */
int /*<<< orphan*/ cmd_clear_breakpoint(int, char const *);
int /*<<< orphan*/ cmd_memory_read(int, char const *);
int /*<<< orphan*/ cmd_memory_write(int, char const *);
int /*<<< orphan*/ cmd_read_registers(int);
int /*<<< orphan*/ cmd_set_breakpoint(int, char const *);
int /*<<< orphan*/ cmd_write_registers(int, char const *);
int /*<<< orphan*/ target_continue(int);
int /*<<< orphan*/ target_step();
int /*<<< orphan*/ write_resp(int, char *);
void handle_command(int fd, const char *cmd) {
  switch (cmd[0]) {
  case '?':
    write_resp(fd, "S00");
    break;
  case 'c':
    target_continue(fd);
    write_resp(fd, "S00");
    break;
  case 's':
    target_step();
    write_resp(fd, "S00");
    break;
  case 'g':
    cmd_read_registers(fd);
    break;
  case 'G':
    cmd_write_registers(fd, cmd + 1);
    break;
  case 'm':
    cmd_memory_read(fd, cmd + 1);
    break;
  case 'M':
    cmd_memory_write(fd, cmd + 1);
    break;
  case 'Z':
    if (cmd[1] == '1') {
      cmd_set_breakpoint(fd, cmd + 2);
      break;
    }
  case 'z':
    if (cmd[1] == '1') {
      cmd_clear_breakpoint(fd, cmd + 2);
      break;
    }
  default:
    write_resp(fd, "");
    break;
  }
}


x86 assembly (clang, -O0):
<handle_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffc1,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x3b,%eax
ja     12b9 <handle_command+0x129>
mov    -0x18(%rbp),%rax
lea    0xe3d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x4(%rbp),%edi
lea    0xf1a(%rip),%rsi
call   1050 <write_resp@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x4(%rbp),%edi
call   1090 <target_continue@plt>
mov    -0x4(%rbp),%edi
lea    0xefe(%rip),%rsi
call   1050 <write_resp@plt>
jmp    12c8 <handle_command+0x138>
mov    $0x0,%al
call   1070 <target_step@plt>
mov    -0x4(%rbp),%edi
lea    0xee3(%rip),%rsi
call   1050 <write_resp@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x4(%rbp),%edi
call   1060 <cmd_read_registers@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1030 <cmd_write_registers@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1080 <cmd_memory_read@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <cmd_memory_write@plt>
jmp    12c8 <handle_command+0x138>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x31,%eax
jne    1289 <handle_command+0xf9>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   10b0 <cmd_set_breakpoint@plt>
jmp    12c8 <handle_command+0x138>
jmp    128e <handle_command+0xfe>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x31,%eax
jne    12b4 <handle_command+0x124>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1040 <cmd_clear_breakpoint@plt>
jmp    12c8 <handle_command+0x138>
jmp    12b9 <handle_command+0x129>
mov    -0x4(%rbp),%edi
lea    0xe30(%rip),%rsi
call   1050 <write_resp@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_command>:
push   %rbx
mov    %edi,%ebx
movsbl (%rsi),%eax
add    $0xffffffc1,%eax
cmp    $0x3b,%eax
ja     1217 <handle_command+0x87>
lea    0xe5b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
add    $0x1,%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1030 <cmd_write_registers@plt>
add    $0x1,%rsi
mov    %ebx,%edi
pop    %rbx
jmp    10a0 <cmd_memory_write@plt>
cmpb   $0x31,0x1(%rsi)
jne    1217 <handle_command+0x87>
add    $0x2,%rsi
mov    %ebx,%edi
pop    %rbx
jmp    10b0 <cmd_set_breakpoint@plt>
mov    %ebx,%edi
call   1090 <target_continue@plt>
jmp    11fc <handle_command+0x6c>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <cmd_read_registers@plt>
add    $0x1,%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1080 <cmd_memory_read@plt>
xor    %eax,%eax
call   1070 <target_step@plt>
lea    0xeed(%rip),%rsi
jmp    121e <handle_command+0x8e>
cmpb   $0x31,0x1(%rsi)
jne    1217 <handle_command+0x87>
add    $0x2,%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1040 <cmd_clear_breakpoint@plt>
lea    0xed5(%rip),%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1050 <write_resp@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"S00\00"
@.str.1 = [1 x i8] zeroinitializer
define void @handle_command(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
switch i32 %8, label %63 [
i32 63, label %9
i32 99, label %12
i32 115, label %17
i32 103, label %21
i32 71, label %24
i32 109, label %29
i32 77, label %34
i32 90, label %39
i32 122, label %51
]
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i32 @write_resp(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %66
12: ; preds = %2
%13 = load i32, i32* %3
%14 = call i32 @target_continue(i32 %13)
%15 = load i32, i32* %3
%16 = call i32 @write_resp(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %66
17: ; preds = %2
%18 = call i32 (...) @target_step()
%19 = load i32, i32* %3
%20 = call i32 @write_resp(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %66
21: ; preds = %2
%22 = load i32, i32* %3
%23 = call i32 @cmd_read_registers(i32 %22)
br label %66
24: ; preds = %2
%25 = load i32, i32* %3
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = call i32 @cmd_write_registers(i32 %25, i8* %27)
br label %66
29: ; preds = %2
%30 = load i32, i32* %3
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = call i32 @cmd_memory_read(i32 %30, i8* %32)
br label %66
34: ; preds = %2
%35 = load i32, i32* %3
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = call i32 @cmd_memory_write(i32 %35, i8* %37)
br label %66
39: ; preds = %2
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 49
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* %3
%47 = load i8*, i8** %4
%48 = getelementptr inbounds i8, i8* %47, i64 2
%49 = call i32 @cmd_set_breakpoint(i32 %46, i8* %48)
br label %66
50: ; preds = %39
br label %51
51: ; preds = %2, %50
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 49
br i1 %56, label %57, label %62
57: ; preds = %51
%58 = load i32, i32* %3
%59 = load i8*, i8** %4
%60 = getelementptr inbounds i8, i8* %59, i64 2
%61 = call i32 @cmd_clear_breakpoint(i32 %58, i8* %60)
br label %66
62: ; preds = %51
br label %63
63: ; preds = %2, %62
%64 = load i32, i32* %3
%65 = call i32 @write_resp(i32 %64, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %63, %57, %45, %34, %29, %24, %21, %17, %12, %9
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"S00\00"
@.str.1 = [1 x i8] zeroinitializer
define void @handle_command(i32 %0, i8* %1) {
%3 = load i8, i8* %1
%4 = sext i8 %3 to i32
switch i32 %4, label %38 [
i32 63, label %5
i32 99, label %7
i32 115, label %10
i32 103, label %13
i32 71, label %15
i32 109, label %18
i32 77, label %21
i32 90, label %24
i32 122, label %31
]
5: ; preds = %2
%6 = tail call i32 @write_resp(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %40
7: ; preds = %2
%8 = tail call i32 @target_continue(i32 %0)
%9 = tail call i32 @write_resp(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %40
10: ; preds = %2
%11 = tail call i32 (...) @target_step()
%12 = tail call i32 @write_resp(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %40
13: ; preds = %2
%14 = tail call i32 @cmd_read_registers(i32 %0)
br label %40
15: ; preds = %2
%16 = getelementptr inbounds i8, i8* %1, i64 1
%17 = tail call i32 @cmd_write_registers(i32 %0, i8* %16)
br label %40
18: ; preds = %2
%19 = getelementptr inbounds i8, i8* %1, i64 1
%20 = tail call i32 @cmd_memory_read(i32 %0, i8* %19)
br label %40
21: ; preds = %2
%22 = getelementptr inbounds i8, i8* %1, i64 1
%23 = tail call i32 @cmd_memory_write(i32 %0, i8* %22)
br label %40
24: ; preds = %2
%25 = getelementptr inbounds i8, i8* %1, i64 1
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 49
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = getelementptr inbounds i8, i8* %1, i64 2
%30 = tail call i32 @cmd_set_breakpoint(i32 %0, i8* %29)
br label %40
31: ; preds = %2
%32 = getelementptr inbounds i8, i8* %1, i64 1
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 49
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = getelementptr inbounds i8, i8* %1, i64 2
%37 = tail call i32 @cmd_clear_breakpoint(i32 %0, i8* %36)
br label %40
38: ; preds = %24, %31, %2
%39 = tail call i32 @write_resp(i32 %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %35, %28, %21, %18, %15, %13, %10, %7, %5
ret void
}