// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x58(%rbp)
movq %rdi, %rax
movq %rax, -0x50(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rcx
movl 0x201c(%rcx), %edi
movq %rcx, %rsi
addq $0x18, %rsi
movslq 0x2018(%rcx), %rax
addq %rax, %rsi
movl $0x2000, %eax           # imm = 0x2000
subl 0x2018(%rcx), %eax
movslq %eax, %rdx
callq 0x3420
movl %eax, %ecx
movl %ecx, -0x14(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x59(%rbp)
je 0xcc23
movq -0x48(%rbp), %rdi
callq 0x73e0
cmpq $0x2, %rax
setb %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0xcc2f
jmp 0xccc6
cmpl $-0x1, -0x14(%rbp)
jne 0xcc37
jmp 0xcbd3
movq -0x48(%rbp), %rax
movl 0x2018(%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xcc8c
movq -0x48(%rbp), %rax
movslq -0x18(%rbp), %rcx
movsbl 0x18(%rax,%rcx), %eax
cmpl $0x7e, %eax
jne 0xcc72
movq -0x48(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0xcf90
jmp 0xcc74
movq -0x48(%rbp), %rdi
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
leaq -0x18(%rbp), %rsi
callq 0xcac0
jmp 0xcc4b
movq -0x48(%rbp), %rax
movl -0x14(%rbp), %ecx
addl 0x2018(%rax), %ecx
movl %ecx, 0x2018(%rax)
cmpl $0x2000, 0x2018(%rax)   # imm = 0x2000
jl 0xccc1
movq -0x48(%rbp), %rax
movl 0x2018(%rax), %ecx
subl $0x2000, %ecx           # imm = 0x2000
movl %ecx, 0x2018(%rax)
jmp 0xcbd3
movq -0x48(%rbp), %rdi
callq 0x73e0
cmpq $0x2, %rax
jae 0xcd06
movq 0x1f2f4(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
leaq 0x1162b(%rip), %rsi     # 0x1e311
movb $0x0, %al
callq 0x3410
movq 0x1f2dc(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
callq 0x3370
movl $0x1, %edi
callq 0x3380
movq -0x48(%rbp), %rdi
callq 0x73e0
cmpq $0x2, %rax
jne 0xcd17
jmp 0xcd36
leaq 0x11619(%rip), %rdi     # 0x1e337
leaq 0x11624(%rip), %rsi     # 0x1e349
movl $0x33, %edx
leaq 0x11623(%rip), %rcx     # 0x1e354
callq 0x31e0
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0xd000
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0xcac0
movq -0x58(%rbp), %rdi
movb $0x0, -0x1d(%rbp)
callq 0xd020
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0xd000
movq -0x48(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x60(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0xd000
movq %rax, %rcx
movl -0x60(%rbp), %eax
cmpl (%rcx), %eax
jle 0xcdb4
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0xd000
movq -0x48(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x68(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0xd000
movq %rax, %rcx
movl -0x68(%rbp), %eax
subl (%rcx), %eax
movl %eax, -0x64(%rbp)
jmp 0xcde7
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xd000
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl $0x2000, %eax           # imm = 0x2000
subl (%rcx), %eax
movl %eax, -0x6c(%rbp)
movl $0x1, %esi
callq 0xd000
movq %rax, %rcx
movl -0x6c(%rbp), %eax
addl (%rcx), %eax
movl %eax, -0x64(%rbp)
movq -0x58(%rbp), %rdi
movl -0x64(%rbp), %eax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rsi
callq 0xd040
jmp 0xcdfc
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xd000
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, -0x70(%rbp)
movl $0x1, %esi
callq 0xd000
movq %rax, %rcx
movl -0x70(%rbp), %eax
cmpl (%rcx), %eax
je 0xceb9
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsbl 0x18(%rax,%rcx), %eax
cmpl $0x7d, %eax
jne 0xce7c
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xcac0
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsbl 0x18(%rax,%rcx), %edx
xorl $0x20, %edx
movb %dl, 0x18(%rax,%rcx)
jmp 0xce7c
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xd190
jmp 0xceea
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movb 0x18(%rax,%rcx), %al
movb %al, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
callq 0xd120
jmp 0xce9a
movq -0x48(%rbp), %rax
movslq -0x38(%rbp), %rcx
movb $0x0, 0x18(%rax,%rcx)
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xcac0
jmp 0xce0e
movq -0x48(%rbp), %rdi
callq 0xd150
movq -0x58(%rbp), %rdi
callq 0xd170
jmp 0xcecd
movb $0x1, -0x1d(%rbp)
testb $0x1, -0x1d(%rbp)
jne 0xcee0
movq -0x58(%rbp), %rdi
callq 0xd190
movq -0x50(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)