// File: /workspace/github_cpp_makefile_star2_O2/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
leaq 0x4(%rsp), %r15
movl 0x201c(%r14), %edi
movslq 0x2018(%r14), %rax
leaq (%rax,%r13), %rsi
movl $0x2000, %edx           # imm = 0x2000
subq %rax, %rdx
callq 0x3340
movq %rax, %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
testl %r12d, %r12d
je 0x5631
cmpq $0x4, %rcx
ja 0x563b
cmpl $-0x1, %r12d
je 0x5578
movl 0x2018(%r14), %eax
testl %r12d, %r12d
movl $0x0, %ebp
cmovgl %r12d, %ebp
movl %eax, 0x4(%rsp)
subl $0x1, %ebp
jb 0x5609
movslq %eax, %rcx
cmpb $0x7e, (%r13,%rcx)
jne 0x55f3
movq %r14, %rdi
movq %r15, %rsi
callq 0x57c6
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
jmp 0x55d0
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)
jmp 0x5578
cmpq $0x4, %rcx
jbe 0x573e
cmpq $0x8, %rcx
jne 0x5721
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
andq $0x0, 0x10(%rbx)
movups %xmm0, (%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 0x57e4
movq (%r14), %rax
movl (%rax), %r12d
leaq 0x3(%rsp), %r15
cmpl 0x4(%rax), %r12d
je 0x56fd
movslq %r12d, %rbp
movb (%r13,%rbp), %al
cmpb $0x7d, %al
jne 0x56ca
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 0x5952
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
jmp 0x5690
cmpq %rax, 0x8(%r14)
je 0x5707
movq %rax, 0x8(%r14)
movq %rbx, %rdi
callq 0x5b42
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6bef(%rip), %rdi      # 0xc317
leaq 0x6bfa(%rip), %rsi      # 0xc329
leaq 0x6bfe(%rip), %rcx      # 0xc334
pushq $0x33
popq %rdx
callq 0x3180
movq 0xd88b(%rip), %rbx      # 0x12fd0
movq (%rbx), %rcx
leaq 0x6ba2(%rip), %rdi      # 0xc2f1
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
pushq $0x1
popq %rdi
callq 0x32b0
jmp 0x576c
movq %rax, %r14
movq %rbx, %rdi
callq 0x58fe
movq %r14, %rdi
callq 0x33b0
nop