#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 */
struct gdb_data {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ gdb_flush(struct gdb_data *);
int gdb_getc(struct gdb_data *);
int /*<<< orphan*/ gdb_printf(struct gdb_data *, char *);
int hexval(int);
scalar_t__ process_gdb_command(struct gdb_data *, char *, int);
int /*<<< orphan*/ stderr;
void gdb_reader_loop(struct gdb_data *data) {
  for (;;) {
    char buf[1024 * 2 + 64];
    int len = 0;
    int cksum_calc = 0;
    int cksum_recv = 0;
    int c;
    do {
      c = gdb_getc(data);
      if (c < 0)
        return;
    } while (c != '$');
    while (len + 1 < sizeof(buf)) {
      c = gdb_getc(data);
      if (c < 0)
        return;
      if (c == '#')
        break;
      buf[len++] = c;
      cksum_calc = (cksum_calc + c) & 0xff;
    }
    buf[len] = 0;
    c = gdb_getc(data);
    if (c < 0)
      return;
    cksum_recv = hexval(c);
    c = gdb_getc(data);
    if (c < 0)
      return;
    cksum_recv = (cksum_recv << 4) | hexval(c);
    if (cksum_recv != cksum_calc) {
      fprintf(stderr,
              "gdb: bad checksum (calc = 0x%02x, "
              "recv = 0x%02x)\n",
              cksum_calc, cksum_recv);
      fprintf(stderr, "gdb: packet data was: %s\n", buf);
      gdb_printf(data, "-");
      if (gdb_flush(data) < 0)
        return;
      continue;
    }
    gdb_printf(data, "+");
    if (gdb_flush(data) < 0)
      return;
    if (len && process_gdb_command(data, buf, len) < 0)
      return;
  }
}


x86 assembly (clang, -O0):
<gdb_reader_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x854(%rbp)
movl   $0x0,-0x858(%rbp)
movl   $0x0,-0x85c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <gdb_getc@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jge    11ae <gdb_reader_loop+0x4e>
jmp    13b8 <gdb_reader_loop+0x258>
jmp    11b3 <gdb_reader_loop+0x53>
cmpl   $0x24,-0x860(%rbp)
jne    118d <gdb_reader_loop+0x2d>
jmp    11c5 <gdb_reader_loop+0x65>
mov    -0x854(%rbp),%eax
add    $0x1,%eax
cltq
cmp    $0x840,%rax
jae    124d <gdb_reader_loop+0xed>
mov    -0x8(%rbp),%rdi
call   1080 <gdb_getc@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jge    11fd <gdb_reader_loop+0x9d>
jmp    13b8 <gdb_reader_loop+0x258>
cmpl   $0x23,-0x860(%rbp)
jne    120f <gdb_reader_loop+0xaf>
jmp    124d <gdb_reader_loop+0xed>
mov    -0x860(%rbp),%eax
mov    %al,%cl
mov    -0x854(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x854(%rbp)
cltq
mov    %cl,-0x850(%rbp,%rax,1)
mov    -0x858(%rbp),%eax
add    -0x860(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x858(%rbp)
jmp    11c5 <gdb_reader_loop+0x65>
movslq -0x854(%rbp),%rax
movb   $0x0,-0x850(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
call   1080 <gdb_getc@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jge    127d <gdb_reader_loop+0x11d>
jmp    13b8 <gdb_reader_loop+0x258>
mov    -0x860(%rbp),%edi
call   1040 <hexval@plt>
mov    %eax,-0x85c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <gdb_getc@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jge    12af <gdb_reader_loop+0x14f>
jmp    13b8 <gdb_reader_loop+0x258>
mov    -0x85c(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x864(%rbp)
mov    -0x860(%rbp),%edi
call   1040 <hexval@plt>
mov    %eax,%ecx
mov    -0x864(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x85c(%rbp)
mov    -0x85c(%rbp),%eax
cmp    -0x858(%rbp),%eax
je     1359 <gdb_reader_loop+0x1f9>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    -0x858(%rbp),%edx
mov    -0x85c(%rbp),%ecx
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    -0x850(%rbp),%rdx
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xd15(%rip),%rsi
call   1070 <gdb_printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gdb_flush@plt>
cmp    $0x0,%rax
jge    1354 <gdb_reader_loop+0x1f4>
jmp    13b8 <gdb_reader_loop+0x258>
jmp    116f <gdb_reader_loop+0xf>
mov    -0x8(%rbp),%rdi
lea    0xcea(%rip),%rsi
call   1070 <gdb_printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <gdb_flush@plt>
cmp    $0x0,%rax
jge    1381 <gdb_reader_loop+0x221>
jmp    13b8 <gdb_reader_loop+0x258>
cmpl   $0x0,-0x854(%rbp)
je     13b3 <gdb_reader_loop+0x253>
mov    -0x8(%rbp),%rdi
lea    -0x850(%rbp),%rsi
mov    -0x854(%rbp),%edx
call   1030 <process_gdb_command@plt>
cmp    $0x0,%rax
jge    13b3 <gdb_reader_loop+0x253>
jmp    13b8 <gdb_reader_loop+0x258>
jmp    116f <gdb_reader_loop+0xf>
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_reader_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
mov    %rdi,%rbx
mov    %rsp,%r15
mov    0x2e62(%rip),%rbp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1080 <gdb_getc@plt>
test   %eax,%eax
js     129f <gdb_reader_loop+0x13f>
cmp    $0x24,%eax
jne    1180 <gdb_reader_loop+0x20>
xor    %r14d,%r14d
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <gdb_getc@plt>
test   %eax,%eax
js     129f <gdb_reader_loop+0x13f>
cmp    $0x23,%eax
je     11d3 <gdb_reader_loop+0x73>
mov    %al,(%rsp,%r14,1)
add    $0x1,%r14
add    %eax,%r12d
movzbl %r12b,%r12d
cmp    $0x83f,%r14
jne    11a0 <gdb_reader_loop+0x40>
mov    $0x83f,%r14d
mov    %r14d,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rbx,%rdi
call   1080 <gdb_getc@plt>
test   %eax,%eax
js     129f <gdb_reader_loop+0x13f>
mov    %eax,%edi
call   1040 <hexval@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1080 <gdb_getc@plt>
test   %eax,%eax
js     129f <gdb_reader_loop+0x13f>
shl    $0x4,%r13d
mov    %eax,%edi
call   1040 <hexval@plt>
or     %r13d,%eax
cmp    %r12d,%eax
jne    1255 <gdb_reader_loop+0xf5>
mov    %rbx,%rdi
lea    0xe2d(%rip),%rsi
call   1070 <gdb_printf@plt>
mov    %rbx,%rdi
call   1060 <gdb_flush@plt>
test   %rax,%rax
js     129f <gdb_reader_loop+0x13f>
test   %r14d,%r14d
je     1180 <gdb_reader_loop+0x20>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <process_gdb_command@plt>
test   %rax,%rax
jns    1180 <gdb_reader_loop+0x20>
jmp    129f <gdb_reader_loop+0x13f>
mov    0x0(%rbp),%edi
lea    0xda1(%rip),%rsi
mov    %r12d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xdbd(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
lea    0xdc3(%rip),%rsi
call   1070 <gdb_printf@plt>
mov    %rbx,%rdi
call   1060 <gdb_flush@plt>
test   %rax,%rax
jns    1180 <gdb_reader_loop+0x20>
add    $0x848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gdb_data = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"gdb: bad checksum (calc = 0x%02x, recv = 0x%02x)\0A\00"
@.str.1 = [26 x i8] c"gdb: packet data was: %s\0A\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [2 x i8] c"+\00"
define void @gdb_reader_loop(%struct.gdb_data* %0) {
%2 = alloca %struct.gdb_data*
%3 = alloca [2112 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.gdb_data* %0, %struct.gdb_data** %2
br label %8
8: ; preds = %104, %86, %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %16, %8
%10 = load %struct.gdb_data*, %struct.gdb_data** %2
%11 = call i32 @gdb_getc(%struct.gdb_data* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %105
15: ; preds = %9
br label %16
16: ; preds = %15
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 36
br i1 %18, label %9, label %19
19: ; preds = %16
br label %20
20: ; preds = %35, %19
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, 2112
br i1 %24, label %25, label %46
25: ; preds = %20
%26 = load %struct.gdb_data*, %struct.gdb_data** %2
%27 = call i32 @gdb_getc(%struct.gdb_data* %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %105
31: ; preds = %25
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 35
br i1 %33, label %34, label %35
34: ; preds = %31
br label %46
35: ; preds = %31
%36 = load i32, i32* %7
%37 = trunc i32 %36 to i8
%38 = load i32, i32* %4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %4
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds [2112 x i8], [2112 x i8]* %3, i64 0, i64 %40
store i8 %37, i8* %41
%42 = load i32, i32* %5
%43 = load i32, i32* %7
%44 = add nsw i32 %42, %43
%45 = and i32 %44, 255
store i32 %45, i32* %5
br label %20
46: ; preds = %34, %20
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [2112 x i8], [2112 x i8]* %3, i64 0, i64 %48
store i8 0, i8* %49
%50 = load %struct.gdb_data*, %struct.gdb_data** %2
%51 = call i32 @gdb_getc(%struct.gdb_data* %50)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %46
br label %105
55: ; preds = %46
%56 = load i32, i32* %7
%57 = call i32 @hexval(i32 %56)
store i32 %57, i32* %6
%58 = load %struct.gdb_data*, %struct.gdb_data** %2
%59 = call i32 @gdb_getc(%struct.gdb_data* %58)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %55
br label %105
63: ; preds = %55
%64 = load i32, i32* %6
%65 = shl i32 %64, 4
%66 = load i32, i32* %7
%67 = call i32 @hexval(i32 %66)
%68 = or i32 %65, %67
store i32 %68, i32* %6
%69 = load i32, i32* %6
%70 = load i32, i32* %5
%71 = icmp ne i32 %69, %70
br i1 %71, label %72, label %87
72: ; preds = %63
%73 = load i32, i32* @stderr
%74 = load i32, i32* %5
%75 = load i32, i32* %6
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %74, i32 %75)
%77 = load i32, i32* @stderr
%78 = getelementptr inbounds [2112 x i8], [2112 x i8]* %3, i64 0, i64 0
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %78)
%80 = load %struct.gdb_data*, %struct.gdb_data** %2
%81 = call i32 @gdb_printf(%struct.gdb_data* %80, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%82 = load %struct.gdb_data*, %struct.gdb_data** %2
%83 = call i64 @gdb_flush(%struct.gdb_data* %82)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %72
br label %105
86: ; preds = %72
br label %8
87: ; preds = %63
%88 = load %struct.gdb_data*, %struct.gdb_data** %2
%89 = call i32 @gdb_printf(%struct.gdb_data* %88, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%90 = load %struct.gdb_data*, %struct.gdb_data** %2
%91 = call i64 @gdb_flush(%struct.gdb_data* %90)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %87
br label %105
94: ; preds = %87
%95 = load i32, i32* %4
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %104
97: ; preds = %94
%98 = load %struct.gdb_data*, %struct.gdb_data** %2
%99 = getelementptr inbounds [2112 x i8], [2112 x i8]* %3, i64 0, i64 0
%100 = load i32, i32* %4
%101 = call i64 @process_gdb_command(%struct.gdb_data* %98, i8* %99, i32 %100)
%102 = icmp slt i64 %101, 0
br i1 %102, label %103, label %104
103: ; preds = %97
br label %105
104: ; preds = %97, %94
br label %8
105: ; preds = %103, %93, %85, %62, %54, %30, %14
ret void
}

LLVM IR (-O2):
%struct.gdb_data = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"gdb: bad checksum (calc = 0x%02x, recv = 0x%02x)\0A\00"
@.str.1 = [26 x i8] c"gdb: packet data was: %s\0A\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [2 x i8] c"+\00"
define void @gdb_reader_loop(%struct.gdb_data* %0) {
%2 = alloca [2112 x i8]
%3 = getelementptr inbounds [2112 x i8], [2112 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %60, %1
br label %5
5: ; preds = %8, %4
%6 = call i32 @gdb_getc(%struct.gdb_data* %0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %59, label %8
8: ; preds = %5
%9 = icmp eq i32 %6, 36
br i1 %9, label %10, label %5
10: ; preds = %8, %18
%11 = phi i64 [ %13, %18 ], [ 0, %8 ]
%12 = phi i32 [ %22, %18 ], [ 0, %8 ]
%13 = add nuw nsw i64 %11, 1
%14 = call i32 @gdb_getc(%struct.gdb_data* %0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %59, label %16
16: ; preds = %10
%17 = icmp eq i32 %14, 35
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = trunc i32 %14 to i8
%20 = getelementptr inbounds [2112 x i8], [2112 x i8]* %2, i64 0, i64 %11
store i8 %19, i8* %20
%21 = add nsw i32 %14, %12
%22 = and i32 %21, 255
%23 = icmp eq i64 %13, 2111
br i1 %23, label %26, label %10
24: ; preds = %16
%25 = trunc i64 %11 to i32
br label %26
26: ; preds = %18, %24
%27 = phi i32 [ %25, %24 ], [ 2111, %18 ]
%28 = phi i32 [ %12, %24 ], [ %22, %18 ]
%29 = zext i32 %27 to i64
%30 = getelementptr inbounds [2112 x i8], [2112 x i8]* %2, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @gdb_getc(%struct.gdb_data* %0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %59, label %33
33: ; preds = %26
%34 = call i32 @hexval(i32 %31)
%35 = call i32 @gdb_getc(%struct.gdb_data* %0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %59, label %37
37: ; preds = %33
%38 = shl i32 %34, 4
%39 = call i32 @hexval(i32 %35)
%40 = or i32 %39, %38
%41 = icmp eq i32 %40, %28
br i1 %41, label %50, label %42
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %40)
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%47 = call i32 @gdb_printf(%struct.gdb_data* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%48 = call i64 @gdb_flush(%struct.gdb_data* %0)
%49 = icmp slt i64 %48, 0
br i1 %49, label %61, label %60
50: ; preds = %37
%51 = call i32 @gdb_printf(%struct.gdb_data* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%52 = call i64 @gdb_flush(%struct.gdb_data* %0)
%53 = icmp slt i64 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %50
%55 = icmp eq i32 %27, 0
br i1 %55, label %60, label %56
56: ; preds = %54
%57 = call i64 @process_gdb_command(%struct.gdb_data* %0, i8* %3, i32 %27)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %26, %33, %50, %56, %5, %10
br label %62
60: ; preds = %42, %54, %56
br label %4
61: ; preds = %42
br label %62
62: ; preds = %61, %59
ret void
}