// File: /workspace/github_cpp_makefile_star2_O2/carellano8[P]Proyecto-Redes-I-2017/crc16.cpp
// Function: crc16::recibir(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void crc16::recibir(binario bin){
    size_t i,li,ls,j,a,b;
    bool error=false;
    binario aux="";
    binario bufferB[MAXMSJ/MAXT*8];
    binario buffer2[MAXMSJ/MAXT*8];
    string bufferMsj;
    ofstream archivo("salidacrc.txt");
    
   
     i=0;j=0;
    while(i<bin.size()){

        li=bin.find("01111110",i);
        ls=bin.find("01111110",i+8);
        bufferB[j]=bin.substr(li,(ls+8)-li);

        //~ cout<<"Inferior "<<li<<" Superior "<<ls<<" Distancia "<<(ls+8)-li<<endl;
        //~ cout<<bufferB[j]<<endl;
        j++;
        i=ls+8;;
    }
    if(j>16){error=true;}// maximo 16 tramas 

    i=0;
    while(!error&&i<j){// Verfificar que no hay error en las banderas 
        if(bufferB[i].size()<30){
            error=true;
        }
        if(!error){
            bufferB[i]=bufferB[i].substr(8,bufferB[i].size()-16);//quita las banderas 
        }
        i++;
    }
   	if(!error){// Quitar relleno de bits
        for (i = 0; i < j; i++)
        {
        	b=0;aux="";a=0;
        	while(a < bufferB[i].size())
        	{
        		if(bufferB[i].at(a)=='1'){
                    b++;
                }else{
                    b=0;
                    }
                aux+=bufferB[i].at(a);		
                if(b==5){
                a++;b=0;
                	}
            a++;
        	}
        	
        	bufferB[i]=aux;

       	}
    }
            
    
    if(!error){// CRC16
        i=0;
        while(!error&&i<j){// Verfificar CRC16
        	// cout<<bufferB[i]<<endl;
        	// cout<<crc(bufferB[i])<<endl;
            if(crc(bufferB[i])!="0000000000000000"){
                error=true;
            }else{
                bufferB[i]=bufferB[i].substr(0,bufferB[i].size()-16);
            }
            i++;
        }
    }
   
       
    if(!error){// Cabecera, Que no hay desorden en las tramas y que hay trama final
        i=0;
        while(!error&&i<j){// 
            a=BitStringToInt(bufferB[i].substr(1,4));
            b=BitStringToInt(bufferB[i].substr(5,1));
            buffer2[a]=bufferB[i].substr(6);
            //cout<<a<<" "<<b<<" "<<buffer2[a]<<endl;
            i++;
        }
        //~ b=BitStringToInt(buffer2[i-1].substr(5,1));
        if(!b){
            error=true;
        }
    }
    if(!error){
        bufferMsj="";
        for (i = 0; i <j ; i++)
        {
            for (a = 0; a <buffer2[i].size() ; a+=8)
            {
                bufferMsj+=(char)(BitStringToInt(buffer2[i].substr(a,8)));
            }   
        }

        cout<<bufferMsj<<endl;
        if(archivo.is_open()){
            archivo<<bufferMsj;
            archivo.close();
        }
    }
    
    
    
    
    if(error){
        cout<<"Se ha detectado algun un error"<<endl;
        if(archivo.is_open()){
            archivo<<"Se ha detectado algun un error"<<endl;
            archivo.close();
        }else{
            cout<<"Se ha detectado algun un error"<<endl;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x22f8, %rsp           # imm = 0x22F8
movq %rdi, %rbx
leaq 0x2adf(%rip), %rsi      # 0x6159
leaq -0xa0(%rbp), %rdi
leaq -0x1320(%rbp), %r14
movq %r14, %rdx
callq 0x2c1a
pushq $0x10
popq %rax
leaq (%rax,%rbp), %rcx
addq $-0x1320, %rcx          # imm = 0xECE0
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x1010, %rax           # imm = 0x1010
jne 0x3693
pushq $0x10
popq %rax
leaq (%rax,%rbp), %rcx
addq $-0x2320, %rcx          # imm = 0xDCE0
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb $0x0, (%rcx)
addq $0x20, %rax
cmpq $0x1010, %rax           # imm = 0x1010
jne 0x36b9
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x29bb(%rip), %rsi      # 0x60ae
leaq -0x320(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x2180
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
cmpq 0x8(%rbx), %r13
jae 0x3779
movq %rbx, %rdi
leaq 0x2a3d(%rip), %r12      # 0x615a
movq %r12, %rsi
movq %r13, %rdx
callq 0x22a0
movq %rax, %r15
addq $0x8, %r13
movq %rbx, %rdi
movq %r12, %rsi
leaq -0x80(%rbp), %r12
movq %r13, %rdx
callq 0x22a0
movq %rax, %r13
addq $0x8, %r13
movq %r13, %rcx
subq %r15, %rcx
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2160
movq %r14, %rdi
movq %r12, %rsi
callq 0x21a0
movq %r12, %rdi
callq 0x20a0
incq -0x38(%rbp)
addq $0x20, %r14
jmp 0x370d
cmpq $0x11, -0x38(%rbp)
setae %al
movl %eax, -0x30(%rbp)
leaq -0x1320(%rbp), %rbx
xorl %r12d, %r12d
leaq -0x80(%rbp), %r14
pushq $0x8
popq %r15
testb $0x1, -0x30(%rbp)
jne 0x37eb
cmpq -0x38(%rbp), %r12
jae 0x37eb
movq 0x8(%rbx), %rcx
cmpq $0x1e, %rcx
movl -0x30(%rbp), %eax
movzbl %al, %eax
pushq $0x1
popq %rdx
cmovbl %edx, %eax
movl %eax, -0x30(%rbp)
testb $0x1, %al
jne 0x37e2
addq $-0x10, %rcx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2160
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21a0
movq %r14, %rdi
callq 0x20a0
incq %r12
addq $0x20, %rbx
jmp 0x3796
testb $0x1, -0x30(%rbp)
je 0x3803
testb $0x1, -0x30(%rbp)
je 0x389a
movl -0x30(%rbp), %eax
jmp 0x396b
xorl %r13d, %r13d
leaq -0xa0(%rbp), %r14
cmpq %r13, -0x38(%rbp)
je 0x3a41
movq %r14, %rdi
leaq 0x2938(%rip), %rsi      # 0x6159
callq 0x2270
movq %r13, %rax
shlq $0x5, %rax
leaq (%rax,%rbp), %r15
addq $-0x1320, %r15          # imm = 0xECE0
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq 0x8(%r15), %r12
jae 0x3887
movq %r15, %rdi
movq %r12, %rsi
callq 0x2280
cmpb $0x31, (%rax)
je 0x3857
xorl %ebx, %ebx
jmp 0x385a
incq %rbx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2280
movsbl (%rax), %esi
movq %r14, %rdi
callq 0x2110
xorl %eax, %eax
cmpq $0x5, %rbx
sete %cl
jne 0x387d
xorl %ebx, %ebx
movb %cl, %al
addq %rax, %r12
incq %r12
jmp 0x383d
movq %r15, %rdi
movq %r14, %rsi
callq 0x20c0
incq %r13
jmp 0x380d
leaq -0x1320(%rbp), %rsi
leaq -0xe0(%rbp), %r14
movq -0x38(%rbp), %r15
subq $0x1, %r15
jb 0x395c
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
movq %rsi, %r12
pushq $0x1
popq %rdx
pushq $0x4
popq %rcx
callq 0x2160
movq %rbx, %rdi
callq 0x3c8e
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20a0
movq %r14, %rdi
movq %r12, %rsi
pushq $0x5
popq %rdx
pushq $0x1
popq %rcx
callq 0x2160
movl %r13d, %ebx
movq %r15, -0x40(%rbp)
movq %r14, %rdi
callq 0x3c8e
movq %r14, %rdi
movl %eax, %r14d
movq %rdi, %r13
callq 0x20a0
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
pushq $0x6
popq %rdx
pushq $-0x1
popq %rcx
callq 0x2160
movslq %ebx, %rax
movslq %r14d, %rbx
shlq $0x5, %rax
leaq (%rax,%rbp), %rdi
addq $-0x2320, %rdi          # imm = 0xDCE0
movq %r15, %rsi
callq 0x21a0
movq %r15, %rdi
callq 0x20a0
addq $0x20, %r12
movq %r13, %r14
movq %r12, %rsi
movq -0x40(%rbp), %r15
jmp 0x38ac
testq %rbx, %rbx
movl -0x30(%rbp), %eax
movzbl %al, %eax
pushq $0x1
popq %rcx
cmovel %ecx, %eax
testb $0x1, %al
je 0x39be
movq 0x565a(%rip), %rdi      # 0x8fd0
leaq 0x2712(%rip), %rsi      # 0x608f
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x2b0(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x3b3b
leaq 0x26ea(%rip), %rsi      # 0x608f
leaq -0x320(%rbp), %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20b0
jmp 0x3b2d
leaq 0x2794(%rip), %rsi      # 0x6159
leaq -0x60(%rbp), %rdi
callq 0x2270
xorl %eax, %eax
leaq -0xc0(%rbp), %rbx
pushq $0x8
popq %r14
leaq -0x60(%rbp), %r15
cmpq %rax, -0x38(%rbp)
je 0x3af5
movq %rax, -0x30(%rbp)
shlq $0x5, %rax
leaq (%rax,%rbp), %r12
addq $-0x2320, %r12          # imm = 0xDCE0
xorl %r13d, %r13d
cmpq 0x8(%r12), %r13
jae 0x3a38
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2160
movq %rbx, %rdi
callq 0x3c8e
movsbl %al, %esi
movq %r15, %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x20a0
addq $0x8, %r13
jmp 0x39ff
movq -0x30(%rbp), %rax
incq %rax
jmp 0x39df
leaq -0x1320(%rbp), %r12
movq $0x0, -0x40(%rbp)
leaq -0x120(%rbp), %r13
movl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
testb $0x1, %al
jne 0x37f1
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x37f1
movq %r13, %rdi
movq %r12, %rsi
callq 0x21c0
leaq -0x80(%rbp), %r14
movq %r14, %rdi
movq %r13, %r15
movq %r13, %rsi
callq 0x32ac
movq %r14, %rdi
leaq 0x269e(%rip), %rsi      # 0x6138
callq 0x3dac
movl %eax, %r13d
movq %r14, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x20a0
movb $0x1, %al
testb %r13b, %r13b
jne 0x3ae5
movq 0x8(%r12), %rcx
addq $-0x10, %rcx
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2160
movq %r12, %rdi
movq %r14, %rsi
callq 0x21a0
movq %r14, %rdi
callq 0x20a0
movl -0x30(%rbp), %eax
incq -0x40(%rbp)
addq $0x20, %r12
movq %r15, %r13
jmp 0x3a5a
movq 0x54d4(%rip), %rdi      # 0x8fd0
leaq -0x60(%rbp), %rsi
callq 0x2130
movq %rax, %rdi
callq 0x20b0
leaq -0x2b0(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x3b56
leaq -0x320(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2130
leaq -0x320(%rbp), %rdi
callq 0x21d0
jmp 0x3b56
movq 0x548e(%rip), %rdi      # 0x8fd0
leaq 0x2546(%rip), %rsi      # 0x608f
callq 0x2150
movq %rax, %rdi
callq 0x20b0
leaq -0x320(%rbp), %rdi
callq 0x21e0
leaq -0x60(%rbp), %rdi
callq 0x20a0
movl $0xfe0, %ebx            # imm = 0xFE0
leaq (%rbx,%rbp), %rdi
addq $-0x2320, %rdi          # imm = 0xDCE0
callq 0x20a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x3b70
movl $0xfe0, %ebx            # imm = 0xFE0
leaq (%rbx,%rbp), %rdi
addq $-0x1320, %rdi          # imm = 0xECE0
callq 0x20a0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x3b8f
leaq -0xa0(%rbp), %rdi
callq 0x20a0
addq $0x22f8, %rsp           # imm = 0x22F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3c30
jmp 0x3c21
jmp 0x3c21
jmp 0x3c21
movq %rax, %rbx
jmp 0x3be3
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x20a0
leaq -0x120(%rbp), %rdi
jmp 0x3c1a
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
jmp 0x3c1a
jmp 0x3c21
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
jmp 0x3c1a
jmp 0x3c21
jmp 0x3c21
jmp 0x3c21
jmp 0x3c21
jmp 0x3c21
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x20a0
jmp 0x3c24
movq %rax, %rbx
leaq -0x320(%rbp), %rdi
callq 0x21e0
leaq -0x60(%rbp), %rdi
callq 0x20a0
movl $0xfe0, %r14d           # imm = 0xFE0
leaq (%r14,%rbp), %rdi
addq $-0x2320, %rdi          # imm = 0xDCE0
callq 0x20a0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x3c3f
movl $0xfe0, %r14d           # imm = 0xFE0
leaq (%r14,%rbp), %rdi
addq $-0x1320, %rdi          # imm = 0xECE0
callq 0x20a0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x3c5f
leaq -0xa0(%rbp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2230
nop