// File: carellano8[P]Proyecto-Redes-I-2017/hamming.cpp
// Function: hamming::decodificadorHamming(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int*)
binario hamming::decodificadorHamming(binario bin,int *r){
    unsigned int i,j,k,m,n,p,x;
    int b[MAXMSJ],acum;
    binario s="";
    char bp;
    k=0;i=0;//inicializacion

    bp=bin.at(bin.size()-1);
   	bin=bin.substr(0,bin.size()-1);
    n=bin.size();
    
    for (i = 0; i < n; ++i)
    {
    	b[i]=(bin[i]=='1')?1:0;
    }
    j=0;i=0;
    while(j<n){

        p=pow(2,i);
        k=p-1;acum=0;
        while(k<n){

                for(m=k;m<p+k&&m<n;m++){
                    // if(m!=p-1){
                        acum+=b[m];
                    // }
                    //~ cout<<m<<" ";
                }
                //~ cout<<endl;
                k=p+m;
            }
            //~ cout<<endl;
        if(acum%2==0){
            s.insert(0,"0");
            }
        else{
        	s.insert(0,"1");
        }
     i++;j=pow(2,i);
    }
    
    acum=0;
    for (i = 0; i < n; i++){
        acum+=b[i];
    }
    if(acum%2==0){
        j=0;
        }
    else{
    	j=1;
    }
    //termina la decodificacion
	// cout<<s<<" "<<j<<endl;
    x=BitStringToInt(s);
    if(x!=0){x--;}
  
    if((((int)j+'0')!=bp)&&x!=0){//paridad mala y hamming malo
    	
    	if(bin[x]=='1'){
    		bin.replace(x,1,"0");
    	}else{
    		bin.replace(x,1,"1");
    	}

    	*r=0;//Se repara un error
    }else{
    	if((((int)j+'0')==bp)&&x!=0){
    		*r=-1;//error doble
    	}
    	else{
    		*r=1;//sin error
    	}
    }
    if(*r!=-1){
    	s="";
    	for (i = 0; i < n; ++i)
    	{
    		if(!potencia2(i+1)){
    			s+=bin[i];
    		}
    	}
    }
    
    return s;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2040, %rsp           # imm = 0x2040
movq %rsi, -0x1ff8(%rbp)
movq %rdi, -0x1fe8(%rbp)
movq %rdi, %rax
movq %rax, -0x1ff0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x1f85(%rbp)
leaq -0x1f86(%rbp), %rdi
movq %rdi, -0x1fe0(%rbp)
callq 0x33a0
movq -0x1fe8(%rbp), %rdi
movq -0x1fe0(%rbp), %rdx
leaq 0x150b(%rip), %rsi      # 0xa159
callq 0x4340
jmp 0x8c55
leaq -0x1f86(%rbp), %rdi
callq 0x3240
movq -0x1ff8(%rbp), %rdi
movl $0x0, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
callq 0x3170
movq -0x1ff8(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x33f0
movq %rax, -0x2000(%rbp)
jmp 0x8c96
movq -0x1ff8(%rbp), %rdi
movq -0x2000(%rbp), %rax
movb (%rax), %al
movb %al, -0x1f95(%rbp)
callq 0x3170
movq -0x1ff8(%rbp), %rsi
movq %rax, %rcx
decq %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x1fb8(%rbp), %rdi
callq 0x3200
jmp 0x8cd0
movq -0x1ff8(%rbp), %rdi
leaq -0x1fb8(%rbp), %rsi
callq 0x3290
leaq -0x1fb8(%rbp), %rdi
callq 0x3140
movq -0x1ff8(%rbp), %rdi
callq 0x3170
movl %eax, -0x2c(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x8d91
movq -0x1ff8(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x2008(%rbp)
jmp 0x8d2b
movq -0x2008(%rbp), %rax
movsbl (%rax), %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x31, %edx
cmovel %eax, %ecx
movl -0x1c(%rbp), %eax
movl %ecx, -0x1f80(%rbp,%rax,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x8d05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f90(%rbp)
movl %eax, -0x1f94(%rbp)
leaq -0x1f86(%rbp), %rdi
callq 0x3240
jmp 0x9122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f90(%rbp)
movl %eax, -0x1f94(%rbp)
jmp 0x9116
movl $0x0, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x8ecc
movl -0x1c(%rbp), %esi
movl $0x2, %edi
callq 0x9290
movsd %xmm0, -0x2010(%rbp)
jmp 0x8dc2
movsd -0x2010(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x1f84(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x8e4f
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %ecx
movl -0x30(%rbp), %edx
addl -0x24(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x2011(%rbp)
jae 0x8e17
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
setb %al
movb %al, -0x2011(%rbp)
movb -0x2011(%rbp), %al
testb $0x1, %al
jne 0x8e23
jmp 0x8e44
movl -0x28(%rbp), %eax
movl -0x1f80(%rbp,%rax,4), %eax
addl -0x1f84(%rbp), %eax
movl %eax, -0x1f84(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x8df3
movl -0x30(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x8de5
movl -0x1f84(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x8e7d
movq -0x1fe8(%rbp), %rdi
leaq 0x12d7(%rip), %rdx      # 0xa147
xorl %eax, %eax
movl %eax, %esi
callq 0x3080
jmp 0x8e7b
jmp 0x8e98
movq -0x1fe8(%rbp), %rdi
leaq 0x12e8(%rip), %rdx      # 0xa173
xorl %eax, %eax
movl %eax, %esi
callq 0x3080
jmp 0x8e96
jmp 0x8e98
movl -0x1c(%rbp), %eax
incl %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %esi
movl $0x2, %edi
callq 0x9290
movsd %xmm0, -0x2020(%rbp)
jmp 0x8eb7
movsd -0x2020(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x20(%rbp)
jmp 0x8d9f
movl $0x0, -0x1f84(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x8f06
movl -0x1c(%rbp), %eax
movl -0x1f80(%rbp,%rax,4), %eax
addl -0x1f84(%rbp), %eax
movl %eax, -0x1f84(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x8edd
movl -0x1f84(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x8f22
movl $0x0, -0x20(%rbp)
jmp 0x8f29
movl $0x1, -0x20(%rbp)
movq -0x1fe8(%rbp), %rsi
leaq -0x1fd8(%rbp), %rdi
callq 0x30e0
jmp 0x8f3e
leaq -0x1fd8(%rbp), %rdi
callq 0x9130
movl %eax, -0x2024(%rbp)
jmp 0x8f52
movl -0x2024(%rbp), %eax
movl %eax, -0x34(%rbp)
leaq -0x1fd8(%rbp), %rdi
callq 0x3140
cmpl $0x0, -0x34(%rbp)
je 0x8f9b
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x8f9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f90(%rbp)
movl %eax, -0x1f94(%rbp)
leaq -0x1fd8(%rbp), %rdi
callq 0x3140
jmp 0x9116
movl -0x20(%rbp), %eax
addl $0x30, %eax
movsbl -0x1f95(%rbp), %ecx
cmpl %ecx, %eax
je 0x9029
cmpl $0x0, -0x34(%rbp)
je 0x9029
movq -0x1ff8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x2030(%rbp)
jmp 0x8fcc
movq -0x2030(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x31, %eax
jne 0x8ffc
movq -0x1ff8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
leaq 0x1159(%rip), %rcx      # 0xa147
movl $0x1, %edx
callq 0x3120
jmp 0x8ffa
jmp 0x901d
movq -0x1ff8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
leaq 0x1164(%rip), %rcx      # 0xa173
movl $0x1, %edx
callq 0x3120
jmp 0x901b
jmp 0x901d
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
jmp 0x9058
movl -0x20(%rbp), %eax
addl $0x30, %eax
movsbl -0x1f95(%rbp), %ecx
cmpl %ecx, %eax
jne 0x904c
cmpl $0x0, -0x34(%rbp)
je 0x904c
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x9056
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x9058
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x90ea
movq -0x1fe8(%rbp), %rdi
leaq 0x10e6(%rip), %rsi      # 0xa159
callq 0x32c0
jmp 0x907a
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x90e8
movl -0x1c(%rbp), %edi
incl %edi
callq 0x9210
movb %al, -0x2031(%rbp)
jmp 0x909b
movb -0x2031(%rbp), %al
testb $0x1, %al
jne 0x90db
jmp 0x90a7
movq -0x1ff8(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0x3430
movq %rax, -0x2040(%rbp)
jmp 0x90c1
movq -0x1fe8(%rbp), %rdi
movq -0x2040(%rbp), %rax
movsbl (%rax), %esi
callq 0x3150
jmp 0x90d9
jmp 0x90db
jmp 0x90dd
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9081
jmp 0x90ea
movb $0x1, -0x1f85(%rbp)
testb $0x1, -0x1f85(%rbp)
jne 0x9106
movq -0x1fe8(%rbp), %rdi
callq 0x3140
movq -0x1ff0(%rbp), %rax
addq $0x2040, %rsp           # imm = 0x2040
popq %rbp
retq
movq -0x1fe8(%rbp), %rdi
callq 0x3140
movq -0x1f90(%rbp), %rdi
callq 0x3390
nop