#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*/ gdb_response_not_supported();
int /*<<< orphan*/ gdb_send_packet(char *, int);
int util_read_dec(char *, int *);
void gdb_cmd_set_thread(char *gdb_packet, int packet_len) {
  int len;
  int num;
  char *packet;
  packet = (char *)gdb_packet;
  if (packet_len > 1) {
    if (*packet == 'H') {
      packet++;
      if (--packet_len == 0) {
        gdb_send_packet("E01", 3);
        return;
      }
      if ((*packet == 'g') || (*packet == 'c')) {
        packet++;
        if (--packet_len == 0) {
          gdb_send_packet("E02", 3);
          return;
        }
        len = util_read_dec(packet, &num);
        if (len == 0) {
          gdb_send_packet("E03", 3);
          return;
        }
        if ((packet_len -= len) > 0) {
          gdb_response_not_supported();
          return;
        }
        if ((num == -1) || (num == 0)) {
          gdb_send_packet("OK", 2);
        } else {
          gdb_response_not_supported();
        }
      } else {
        gdb_response_not_supported();
      }
    } else {
      gdb_send_packet("E04", 3);
    }
  } else {
    gdb_send_packet("E05", 3);
  }
}


x86 assembly (clang, -O0):
<gdb_cmd_set_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    1291 <gdb_cmd_set_thread+0x161>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x48,%eax
jne    127b <gdb_cmd_set_thread+0x14b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    1195 <gdb_cmd_set_thread+0x65>
lea    0xe7a(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
jmp    12a2 <gdb_cmd_set_thread+0x172>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x67,%eax
je     11b5 <gdb_cmd_set_thread+0x85>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x63,%eax
jne    126f <gdb_cmd_set_thread+0x13f>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    11e9 <gdb_cmd_set_thread+0xb9>
lea    0xe2a(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
jmp    12a2 <gdb_cmd_set_thread+0x172>
mov    -0x20(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <util_read_dec@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1219 <gdb_cmd_set_thread+0xe9>
lea    0xdfe(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
jmp    12a2 <gdb_cmd_set_thread+0x172>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1239 <gdb_cmd_set_thread+0x109>
mov    $0x0,%al
call   1040 <gdb_response_not_supported@plt>
jmp    12a2 <gdb_cmd_set_thread+0x172>
cmpl   $0xffffffff,-0x14(%rbp)
je     124d <gdb_cmd_set_thread+0x11d>
cmpl   $0x0,-0x14(%rbp)
jne    1263 <gdb_cmd_set_thread+0x133>
lea    0xdb8(%rip),%rdi
mov    $0x2,%esi
call   1050 <gdb_send_packet@plt>
jmp    126a <gdb_cmd_set_thread+0x13a>
mov    $0x0,%al
call   1040 <gdb_response_not_supported@plt>
jmp    1276 <gdb_cmd_set_thread+0x146>
mov    $0x0,%al
call   1040 <gdb_response_not_supported@plt>
jmp    128c <gdb_cmd_set_thread+0x15c>
lea    0xd8d(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
jmp    12a2 <gdb_cmd_set_thread+0x172>
lea    0xd7b(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_cmd_set_thread>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%esi
jl     117f <gdb_cmd_set_thread+0x4f>
cmpb   $0x48,(%rdi)
jne    1188 <gdb_cmd_set_thread+0x58>
mov    0x1(%rdi),%al
or     $0x4,%al
cmp    $0x67,%al
jne    119e <gdb_cmd_set_thread+0x6e>
mov    %esi,%ebx
add    $0xfffffffe,%ebx
je     11aa <gdb_cmd_set_thread+0x7a>
add    $0x2,%rdi
lea    0xc(%rsp),%rsi
call   1030 <util_read_dec@plt>
test   %eax,%eax
je     11bc <gdb_cmd_set_thread+0x8c>
cmp    %eax,%ebx
jg     11b3 <gdb_cmd_set_thread+0x83>
mov    0xc(%rsp),%eax
add    $0x1,%eax
cmp    $0x1,%eax
ja     11b3 <gdb_cmd_set_thread+0x83>
lea    0xe90(%rip),%rdi
mov    $0x2,%esi
jmp    11c8 <gdb_cmd_set_thread+0x98>
lea    0xe89(%rip),%rdi
jmp    118f <gdb_cmd_set_thread+0x5f>
lea    0xe7c(%rip),%rdi
mov    $0x3,%esi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <gdb_send_packet@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
jmp    1040 <gdb_response_not_supported@plt>
lea    0xe4f(%rip),%rdi
jmp    118f <gdb_cmd_set_thread+0x5f>
xor    %eax,%eax
call   1040 <gdb_response_not_supported@plt>
jmp    11cd <gdb_cmd_set_thread+0x9d>
lea    0xe41(%rip),%rdi
mov    $0x3,%esi
call   1050 <gdb_send_packet@plt>
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str.1 = [4 x i8] c"E02\00"
@.str.2 = [4 x i8] c"E03\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [4 x i8] c"E04\00"
@.str.5 = [4 x i8] c"E05\00"
define void @gdb_cmd_set_thread(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %1, 1
br i1 %5, label %6, label %39
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 72
br i1 %8, label %9, label %37
9: ; preds = %6
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = load i8, i8* %10
switch i8 %11, label %35 [
i8 103, label %12
i8 99, label %12
]
12: ; preds = %9, %9
%13 = add nsw i32 %1, -2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
br label %41
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %0, i64 2
%19 = call i32 @util_read_dec(i8* %18, i32* %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %41
23: ; preds = %17
%24 = icmp sgt i32 %13, %19
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = call i32 (...) @gdb_response_not_supported()
br label %41
27: ; preds = %23
%28 = load i32, i32* %3
%29 = add i32 %28, 1
%30 = icmp ult i32 %29, 2
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @gdb_send_packet(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
br label %41
33: ; preds = %27
%34 = call i32 (...) @gdb_response_not_supported()
br label %41
35: ; preds = %9
%36 = tail call i32 (...) @gdb_response_not_supported()
br label %41
37: ; preds = %6
%38 = tail call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
br label %41
39: ; preds = %2
%40 = tail call i32 @gdb_send_packet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 3)
br label %41
41: ; preds = %39, %35, %33, %31, %37, %25, %21, %15
ret void
}