// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f98, %rsp           # imm = 0x1F98
movq %rdx, -0x40(%rbp)
movq %rsi, %r14
leaq 0x120d(%rip), %rsi      # 0x6159
leaq -0x80(%rbp), %rdx
movq %rdi, -0x30(%rbp)
callq 0x2c1a
movq 0x8(%r14), %rsi
decq %rsi
movq %r14, %rdi
callq 0x2280
movsbl (%rax), %eax
movl %eax, -0x34(%rbp)
movq 0x8(%r14), %rcx
decq %rcx
leaq -0x80(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2160
leaq -0x80(%rbp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x21a0
movq %r12, %rdi
callq 0x20a0
movq 0x8(%r14), %r13
movl %r13d, %r15d
xorl %eax, %eax
cmpq %rax, %r15
je 0x4fc0
movq (%r14), %rcx
xorl %edx, %edx
cmpb $0x31, (%rcx,%rax)
sete %dl
movl %edx, -0x1fc0(%rbp,%rax,4)
incq %rax
jmp 0x4fa3
xorl %eax, %eax
leaq 0x11aa(%rip), %r12      # 0x6173
xorl %ebx, %ebx
cmpl %r13d, %eax
jae 0x503c
movl %ebx, %eax
cvtsi2sd %rax, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
movl %eax, %ecx
decl %eax
xorl %edx, %edx
cmpl %r13d, %eax
jae 0x500d
leal (%rax,%rcx), %esi
cmpl %r13d, %esi
cmovael %r13d, %esi
movl %eax, %eax
cmpq %rsi, %rax
jae 0x5009
addl -0x1fc0(%rbp,%rax,4), %edx
incq %rax
jmp 0x4ff8
addl %ecx, %eax
jmp 0x4fe7
testb $0x1, %dl
movq %r12, %rdx
leaq 0x112d(%rip), %rax      # 0x6147
cmoveq %rax, %rdx
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x2060
incl %ebx
cvtsi2sd %rbx, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
jmp 0x4fcb
xorl %eax, %eax
xorl %ebx, %ebx
cmpq %rax, %r15
je 0x5051
addl -0x1fc0(%rbp,%rax,4), %ebx
incq %rax
jmp 0x5040
leaq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x21c0
leaq -0x60(%rbp), %rdi
callq 0x514e
movl %eax, %r13d
andl $0x1, %ebx
orl $0x30, %ebx
leaq -0x60(%rbp), %rdi
callq 0x20a0
movl -0x34(%rbp), %edx
cmpl %edx, %ebx
setne %al
cmpl $0x2, %r13d
setae %cl
testb %cl, %al
je 0x50b7
decl %r13d
movq (%r14), %rax
cmpb $0x31, (%rax,%r13)
leaq 0x10a9(%rip), %rax      # 0x6147
cmoveq %rax, %r12
xorl %ebx, %ebx
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x20d0
jmp 0x50e7
cmpl $0x2, %r13d
setb %al
cmpl %edx, %ebx
setne %cl
orb %al, %cl
jne 0x50e4
movq -0x40(%rbp), %rax
orl $-0x1, (%rax)
movq -0x30(%rbp), %rax
addq $0x1f98, %rsp           # imm = 0x1F98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbx
movq -0x40(%rbp), %rax
movl %ebx, (%rax)
leaq 0x1065(%rip), %rsi      # 0x6159
movq -0x30(%rbp), %rdi
callq 0x2270
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x50ce
leal 0x1(%rbx), %edi
callq 0x5206
testb %al, %al
jne 0x5120
movq (%r14), %rax
movsbl (%rax,%rbx), %esi
movq -0x30(%rbp), %rdi
callq 0x2110
incq %rbx
jmp 0x50ff
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x20a0
jmp 0x513c
jmp 0x5139
jmp 0x5139
jmp 0x5139
movq %rax, %r14
movq -0x30(%rbp), %rdi
callq 0x20a0
movq %r14, %rdi
callq 0x2230
nop