// File: /workspace/github_cpp_makefile_star2_O3/Thomas1995[P]Rentz/src/common.cpp
// Function: Common::readFrame()
std::vector<uint8_t> Common::readFrame() {

	int n;

	while((n = read(sfd, buff + at, MAX_BUFF - at)) != 0 && flags.size() < 2) {
    if(n == -1)
      continue;

    int cr = at;
    for(int i = 0; i < n; ++i, inc(cr)) {
      if(buff[cr] == FLAG)
        flags.push_back(cr);
    }

		at += n;
		if(at >= MAX_BUFF)
			at -= MAX_BUFF;
	}

	if(flags.size() < 2) {
		debug("The client has closed the connection\n");
		exit(1);
		//TODO
		//add disconnect handling
	}

	assert(flags.size() == 2);

	inc(flags[1]);
	//flags[1] refers to the
	//past the flag byte

	std::vector<uint8_t> frame;

	const int cnt = (flags[1] > flags[0]) ? flags[1] - flags[0] : MAX_BUFF - flags[0] + flags[1];

	frame.reserve(cnt);

	for(int i = flags[0]; i != flags[1]; inc(i)) {
    if(buff[i] == ESC) {
      inc(i);
      buff[i] ^= MAGIC;
    }
		frame.push_back(buff[i]);
		buff[i] = 0;
	}

	flags.clear();

	frame.shrink_to_fit();
	return frame;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsi), %r13
movl 0x201c(%rsi), %edi
movslq 0x2018(%rsi), %rax
addq %rax, %rsi
addq $0x18, %rsi
movl $0x2000, %edx           # imm = 0x2000
subq %rax, %rdx
callq 0x3330
movq %rax, %r12
testl %r12d, %r12d
je 0x5d42
leaq 0x4(%rsp), %r15
movq 0x8(%r14), %rax
subq (%r14), %rax
cmpq $0x4, %rax
ja 0x5d42
movl 0x2018(%r14), %eax
cmpl $-0x1, %r12d
je 0x5d1a
movl %eax, 0x4(%rsp)
movl %r12d, %ebp
testl %r12d, %r12d
jle 0x5cfe
movslq %eax, %rcx
cmpb $0x7e, (%r13,%rcx)
jne 0x5cd7
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x5ccc
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x5cd7
movq %r14, %rdi
movq %r15, %rdx
callq 0x613e
movl 0x4(%rsp), %eax
xorl %ecx, %ecx
cmpl $0x1fff, %eax           # imm = 0x1FFF
setl %cl
shll $0xd, %ecx
addl %ecx, %eax
addl $0xffffe001, %eax       # imm = 0xFFFFE001
movl %eax, 0x4(%rsp)
decl %ebp
jne 0x5cab
movl 0x2018(%r14), %eax
leal (%rax,%r12), %ecx
cmpl $0x2000, %ecx           # imm = 0x2000
leal -0x2000(%rax,%r12), %eax
cmovll %ecx, %eax
movl %eax, 0x2018(%r14)
movl 0x201c(%r14), %edi
cltq
leaq (%rax,%r13), %rsi
movl $0x2000, %ecx           # imm = 0x2000
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x3330
movq %rax, %r12
testl %r12d, %r12d
jne 0x5c81
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x4, %rcx
jbe 0x5e4d
cmpq $0x8, %rcx
jne 0x5e52
movl 0x4(%rax), %ecx
xorl %edx, %edx
cmpl $0x1fff, %ecx           # imm = 0x1FFF
setl %dl
shll $0xd, %edx
leal (%rdx,%rcx), %esi
addl $0xffffe001, %esi       # imm = 0xFFFFE001
movl %esi, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movl (%rax), %eax
cmpl %eax, %esi
leal 0x1(%rdx,%rcx), %ecx
cmovgl %esi, %ecx
subl %eax, %ecx
movslq %ecx, %rsi
movq %rbx, %rdi
callq 0x5ef4
movq (%r14), %rax
movl (%rax), %r12d
cmpl 0x4(%rax), %r12d
je 0x5e1f
leaq 0x3(%rsp), %r15
movslq %r12d, %rbp
movb (%r13,%rbp), %al
cmpb $0x7d, %al
jne 0x5de8
xorl %eax, %eax
cmpl $0x1fff, %r12d          # imm = 0x1FFF
setl %al
shll $0xd, %eax
addl %eax, %r12d
addl $0xffffe001, %r12d      # imm = 0xFFFFE001
movslq %r12d, %rbp
movb (%r13,%rbp), %al
xorb $0x20, %al
movb %al, (%r13,%rbp)
movb %al, 0x3(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6018
movb $0x0, 0x18(%r14,%rbp)
xorl %eax, %eax
cmpl $0x1fff, %r12d          # imm = 0x1FFF
setl %al
shll $0xd, %eax
addl %eax, %r12d
addl $0xffffe001, %r12d      # imm = 0xFFFFE001
movq (%r14), %rax
cmpl 0x4(%rax), %r12d
jne 0x5db4
cmpq %rax, 0x8(%r14)
je 0x5e29
movq %rax, 0x8(%r14)
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x5e3b
movq %rbx, %rdi
callq 0x621a
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3850
leaq 0x74ee(%rip), %rdi      # 0xd347
leaq 0x74f9(%rip), %rsi      # 0xd359
leaq 0x74fd(%rip), %rcx      # 0xd364
movl $0x33, %edx
callq 0x3160
jmp 0x5e73
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x5e8a
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x3240
movq %r14, %rdi
callq 0x33a0