// File: /workspace/github_c_makefile_star2_O0/inolen[P]gdb_server/./gdb_server.h
// Function: gdb_server_recv_packet
static const char *gdb_server_recv_packet(gdb_server_t *sv) {
  if (sv->client == INVALID_SOCKET) {
    return NULL;
  }

  int parsed_ack = 0;

  while (gdb_server_data_available(sv) && sv->conn.recv_state != PARSE_DONE) {
    // read the next byte from the stream
    char c = 0;
    int n = (int)recv(sv->client, &c, 1, 0);

    if (n == 0) {
      // client disconnected
      gdb_server_destroy_client(sv);
      break;
    } else if (n == SOCKET_ERROR) {
      GDB_SERVER_LOG("gdb server recv failed, %d", n);
      break;
    }

    GDB_SERVER_ASSERT(n == 1);

    // process the byte into the current packet
    switch (sv->conn.recv_state) {
      case PARSE_WAIT: {
        if (c == GDB_PACKET_ACK[0] || c == GDB_PACKET_NACK[0] || c == 0x3) {
          GDB_SERVER_ASSERT(sv->conn.recv_length < GDB_MAX_DATA_SIZE);
          sv->conn.recv_data[sv->conn.recv_length++] = c;
          sv->conn.recv_data[sv->conn.recv_length] = 0;
          sv->conn.recv_state = PARSE_DONE;
          parsed_ack = 1;
        } else if (c == GDB_PACKET_BEGIN) {
          sv->conn.recv_state = PARSE_DATA;
        } else {
          GDB_SERVER_ASSERT(0);
        }
      } break;

      case PARSE_DATA: {
        if (c == GDB_PACKET_END) {
          sv->conn.recv_state = PARSE_CHECKSUM_HIGH;
        } else {
          GDB_SERVER_ASSERT(sv->conn.recv_length < GDB_MAX_DATA_SIZE);
          sv->conn.recv_data[sv->conn.recv_length++] = c;
          sv->conn.recv_data[sv->conn.recv_length] = 0;
        }
      } break;

      case PARSE_CHECKSUM_HIGH: {
        sv->conn.recv_checksum = xtoi(c) << 4;
        sv->conn.recv_state = PARSE_CHECKSUM_LOW;
      } break;

      case PARSE_CHECKSUM_LOW: {
        sv->conn.recv_checksum |= xtoi(c);
        sv->conn.recv_state = PARSE_DONE;
      } break;

      case PARSE_DONE:
        GDB_SERVER_ASSERT(0);
        break;
    }
  }

  if (sv->conn.recv_state == PARSE_DONE) {
    // validate and ack non-notification packets
    if (!sv->conn.ack_disabled && !parsed_ack) {
      uint8_t expected_checksum = packet_data_checksum(sv->conn.recv_data);

      if (sv->conn.recv_checksum == expected_checksum) {
        gdb_server_send_raw(sv, GDB_PACKET_ACK);
      } else {
        gdb_server_send_raw(sv, GDB_PACKET_NACK);
      }
    }

    // reset parse state
    sv->conn.recv_state = PARSE_WAIT;
    sv->conn.recv_length = 0;

    return sv->conn.recv_data;
  }

  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
jne 0x81f3
movq $0x0, -0x8(%rbp)
jmp 0x8541
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8ef0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x1e(%rbp)
je 0x821d
movq -0x10(%rbp), %rax
cmpl $0x4, 0x10(%rax)
setne %al
movb %al, -0x1e(%rbp)
movb -0x1e(%rbp), %al
testb $0x1, %al
jne 0x8229
jmp 0x84a2
movb $0x0, -0x15(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x3040
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x825b
movq -0x10(%rbp), %rdi
callq 0x8ea0
jmp 0x84a2
cmpl $-0x1, -0x1c(%rbp)
jne 0x8277
movl -0x1c(%rbp), %esi
leaq 0x2ca4(%rip), %rdi      # 0xaf0f
movb $0x0, %al
callq 0x30d0
jmp 0x84a2
jmp 0x8279
cmpl $0x1, -0x1c(%rbp)
jne 0x8281
jmp 0x82a0
leaq 0x2ca3(%rip), %rdi      # 0xaf2b
leaq 0x2ca3(%rip), %rsi      # 0xaf32
movl $0x20b, %edx            # imm = 0x20B
leaq 0x2ca6(%rip), %rcx      # 0xaf41
callq 0x30f0
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x28(%rbp)
subq $0x4, %rax
ja 0x849d
movq -0x28(%rbp), %rax
leaq 0x2b8c(%rip), %rcx      # 0xae4c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl -0x15(%rbp), %eax
movq 0x8dec(%rip), %rcx      # 0x110c0
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x82f6
movsbl -0x15(%rbp), %eax
movq 0x8de2(%rip), %rcx      # 0x110c8
movsbl (%rcx), %ecx
cmpl %ecx, %eax
je 0x82f6
movsbl -0x15(%rbp), %eax
cmpl $0x3, %eax
jne 0x8372
movq -0x10(%rbp), %rax
cmpl $0x1fffb, 0x20010(%rax) # imm = 0x1FFFB
jge 0x8308
jmp 0x8327
leaq 0x2c65(%rip), %rdi      # 0xaf74
leaq 0x2c1c(%rip), %rsi      # 0xaf32
movl $0x211, %edx            # imm = 0x211
leaq 0x2c1f(%rip), %rcx      # 0xaf41
callq 0x30f0
movb -0x15(%rbp), %dl
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rsi
movl 0x20010(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x20010(%rsi)
movslq %ecx, %rcx
movb %dl, 0x14(%rax,%rcx)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x20010(%rcx), %rcx
movb $0x0, 0x14(%rax,%rcx)
movq -0x10(%rbp), %rax
movl $0x4, 0x10(%rax)
movl $0x1, -0x14(%rbp)
jmp 0x83a9
movsbl -0x15(%rbp), %eax
cmpl $0x24, %eax
jne 0x8388
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x83a7
leaq 0x2357(%rip), %rdi      # 0xa6e6
leaq 0x2b9c(%rip), %rsi      # 0xaf32
movl $0x219, %edx            # imm = 0x219
leaq 0x2b9f(%rip), %rcx      # 0xaf41
callq 0x30f0
jmp 0x83a9
jmp 0x849d
movsbl -0x15(%rbp), %eax
cmpl $0x23, %eax
jne 0x83c4
movq -0x10(%rbp), %rax
movl $0x2, 0x10(%rax)
jmp 0x842c
movq -0x10(%rbp), %rax
cmpl $0x1fffb, 0x20010(%rax) # imm = 0x1FFFB
jge 0x83d6
jmp 0x83f5
leaq 0x2b97(%rip), %rdi      # 0xaf74
leaq 0x2b4e(%rip), %rsi      # 0xaf32
movl $0x221, %edx            # imm = 0x221
leaq 0x2b51(%rip), %rcx      # 0xaf41
callq 0x30f0
movb -0x15(%rbp), %dl
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rsi
movl 0x20010(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x20010(%rsi)
movslq %ecx, %rcx
movb %dl, 0x14(%rax,%rcx)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x20010(%rcx), %rcx
movb $0x0, 0x14(%rax,%rcx)
jmp 0x849d
movsbl -0x15(%rbp), %edi
callq 0x9060
shll $0x4, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x20014(%rax)
movq -0x10(%rbp), %rax
movl $0x3, 0x10(%rax)
jmp 0x849d
movsbl -0x15(%rbp), %edi
callq 0x9060
movl %eax, %edx
movq -0x10(%rbp), %rax
movzbl 0x20014(%rax), %ecx
orl %edx, %ecx
movb %cl, 0x20014(%rax)
movq -0x10(%rbp), %rax
movl $0x4, 0x10(%rax)
jmp 0x849d
leaq 0x2261(%rip), %rdi      # 0xa6e6
leaq 0x2aa6(%rip), %rsi      # 0xaf32
movl $0x232, %edx            # imm = 0x232
leaq 0x2aa9(%rip), %rcx      # 0xaf41
callq 0x30f0
jmp 0x81fa
movq -0x10(%rbp), %rax
cmpl $0x4, 0x10(%rax)
jne 0x8539
movq -0x10(%rbp), %rax
cmpl $0x0, 0x40018(%rax)
jne 0x850e
cmpl $0x0, -0x14(%rbp)
jne 0x850e
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
addq $0x4, %rdi
callq 0x90d0
movb %al, -0x1d(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x20014(%rax), %eax
movzbl -0x1d(%rbp), %ecx
cmpl %ecx, %eax
jne 0x84fc
movq -0x10(%rbp), %rdi
movq 0x8bcb(%rip), %rsi      # 0x110c0
callq 0x9130
jmp 0x850c
movq -0x10(%rbp), %rdi
movq 0x8bc1(%rip), %rsi      # 0x110c8
callq 0x9130
jmp 0x850e
movq -0x10(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x20010(%rax)
movq -0x10(%rbp), %rax
addq $0x10, %rax
addq $0x4, %rax
movq %rax, -0x8(%rbp)
jmp 0x8541
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)