// File: /workspace/github_cpp_makefile_star2_O1/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 0x594b
leaq 0x4(%rsp), %r15
movq 0x8(%r14), %rax
subq (%r14), %rax
cmpq $0x4, %rax
ja 0x594b
cmpl $-0x1, %r12d
je 0x5920
movl 0x2018(%r14), %eax
movl %eax, 0x4(%rsp)
movl %r12d, %ebp
testl %r12d, %r12d
jle 0x58fd
movslq 0x4(%rsp), %rax
cmpb $0x7e, (%r13,%rax)
jne 0x58dd
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x58d2
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r14)
jmp 0x58dd
movq %r14, %rdi
movq %r15, %rdx
callq 0x5d3a
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 0x58af
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
movslq 0x2018(%r14), %rax
leaq (%rax,%r13), %rsi
movl $0x2000, %edx           # imm = 0x2000
subq %rax, %rdx
callq 0x3330
movq %rax, %r12
testl %r12d, %r12d
jne 0x5881
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
cmpq $0x4, %rcx
jbe 0x5a51
cmpq $0x8, %rcx
jne 0x5a56
movl 0x4(%rax), %ecx
xorl %edx, %edx
cmpl $0x1fff, %ecx           # imm = 0x1FFF
setl %dl
shll $0xd, %edx
addl %edx, %ecx
addl $0xffffe001, %ecx       # imm = 0xFFFFE001
movl %ecx, 0x4(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movl (%rax), %ecx
movl 0x4(%rax), %eax
leal 0x2000(%rax), %edx
cmpl %ecx, %eax
cmovgl %eax, %edx
subl %ecx, %edx
movslq %edx, %rsi
movq %rbx, %rdi
callq 0x5af8
movq (%r14), %rcx
movl (%rcx), %eax
cmpl 0x4(%rcx), %eax
je 0x5a23
leaq 0x3(%rsp), %r15
movslq %eax, %rcx
cmpb $0x7d, (%r13,%rcx)
jne 0x59e7
xorl %ecx, %ecx
cmpl $0x1fff, %eax           # imm = 0x1FFF
setl %cl
shll $0xd, %ecx
addl %ecx, %eax
addl $0xffffe001, %eax       # imm = 0xFFFFE001
movslq %eax, %rcx
xorb $0x20, (%r13,%rcx)
movslq %eax, %r12
movb (%r13,%r12), %al
movb %al, 0x3(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5c1c
movb $0x0, 0x18(%r14,%r12)
xorl %eax, %eax
cmpl $0x1fff, %r12d          # imm = 0x1FFF
setl %al
shll $0xd, %eax
addl %r12d, %eax
addl $0xffffe001, %eax       # imm = 0xFFFFE001
movq (%r14), %rcx
cmpl 0x4(%rcx), %eax
jne 0x59bf
cmpq %rcx, 0x8(%r14)
je 0x5a2d
movq %rcx, 0x8(%r14)
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x5a3f
movq %rbx, %rdi
callq 0x5e16
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3846
leaq 0x78aa(%rip), %rdi      # 0xd307
leaq 0x78b5(%rip), %rsi      # 0xd319
leaq 0x78b9(%rip), %rcx      # 0xd324
movl $0x33, %edx
callq 0x3160
jmp 0x5a77
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x5a8e
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x3240
movq %r14, %rdi
callq 0x33a0