// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1fb8, %rsp           # imm = 0x1FB8
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r13
leaq 0x10(%rdi), %rbx
movq %rbx, (%rdi)
leaq 0x14de(%rip), %rdx      # 0x8159
movq %rdx, %rsi
callq 0x3140
movq 0x8(%r14), %rcx
subq $0x1, %rcx
movq %rbx, -0x78(%rbp)
jb 0x6fec
movq (%r14), %rax
movsbl (%rax,%rcx), %eax
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2170
movq %r15, -0x50(%rbp)
leaq -0x98(%rbp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%r12), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x6ce5
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2160
movq %r14, %rax
movq 0x8(%r14), %r15
movl %r15d, %esi
movl $0x30, %r14d
testl %r15d, %r15d
movq %rax, -0x40(%rbp)
movq %rsi, -0x30(%rbp)
je 0x6dea
movq (%rax), %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x31, (%rax,%rcx)
sete %dl
movl %edx, -0x1fe0(%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %rsi
jne 0x6d0b
testl %r15d, %r15d
je 0x6dea
xorl %r12d, %r12d
leaq 0x1411(%rip), %rbx      # 0x8147
movl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
leal -0x1(%rax), %esi
movq %rbx, %rcx
cmpl %r15d, %esi
jae 0x6d94
xorl %ecx, %ecx
leal (%rsi,%rax), %edi
cmpl %r15d, %edi
movl %r15d, %edx
cmovbl %edi, %edx
cmpl %edx, %esi
jae 0x6d7f
movl %esi, %esi
movl %edx, %edi
addl -0x1fe0(%rbp,%rsi,4), %ecx
incq %rsi
cmpq %rsi, %rdi
jne 0x6d6c
addl %eax, %edx
movl %edx, %edi
movl %edi, %esi
cmpl %r15d, %edi
jb 0x6d58
testb $0x1, %cl
leaq 0x13e3(%rip), %rcx      # 0x8173
cmoveq %rbx, %rcx
movl $0x1, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2190
incl %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
cmpl %r15d, %eax
jb 0x6d36
testl %r15d, %r15d
movq -0x30(%rbp), %rcx
je 0x6dea
xorl %eax, %eax
xorl %r14d, %r14d
addl -0x1fe0(%rbp,%rax,4), %r14d
incq %rax
cmpq %rax, %rcx
jne 0x6dd2
andl $0x1, %r14d
orl $0x30, %r14d
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x30a2
movq -0x68(%rbp), %rbx
testq %rbx, %rbx
je 0x6ec0
movq %r13, -0x38(%rbp)
decq %rbx
xorl %r12d, %r12d
movq 0x12eb(%rip), %xmm0     # 0x8110
movapd 0x12f3(%rip), %xmm2     # 0x8120
xorl %r13d, %r13d
movq -0x70(%rbp), %rax
cmpb $0x31, (%rax,%r13)
jne 0x6ead
movq %rbx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
subpd %xmm2, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x2140
movq %r12, %xmm1
movq 0x12aa(%rip), %xmm3     # 0x8110
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x12ae(%rip), %xmm4     # 0x8120
subpd %xmm4, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x1296(%rip), %xmm0     # 0x8130
cvttsd2si %xmm0, %r12
andq %rcx, %r12
orq %rax, %r12
movdqa %xmm3, %xmm0
movapd %xmm4, %xmm2
incq %r13
addq $-0x1, %rbx
jb 0x6e30
movq -0x38(%rbp), %r13
jmp 0x6ec3
xorl %r12d, %r12d
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x6edc
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
movl -0x44(%rbp), %eax
cmpl %eax, %r14d
je 0x6f26
cmpl $0x1, %r12d
jbe 0x6f26
decl %r12d
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
cmpb $0x31, (%rax,%r12)
leaq 0x1247(%rip), %rax      # 0x8147
leaq 0x126c(%rip), %rcx      # 0x8173
cmoveq %rax, %rcx
xorl %ebx, %ebx
movl $0x1, %edx
movl $0x1, %r8d
movq %r12, %rsi
callq 0x2190
movq -0x30(%rbp), %r12
jmp 0x6f4d
movl $0x1, %ebx
cmpl %eax, %r14d
jne 0x6f49
cmpl $0x2, %r12d
movq -0x30(%rbp), %r12
jb 0x6f4d
movq -0x50(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x6fd7
movq -0x30(%rbp), %r12
movq -0x50(%rbp), %rax
movl %ebx, (%rax)
movq 0x8(%r13), %rdx
leaq 0x11fb(%rip), %rcx      # 0x8159
movq %r13, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x2210
testl %r15d, %r15d
movq -0x40(%rbp), %r15
je 0x6fd7
xorl %r14d, %r14d
leaq 0x1(%r14), %rbx
cmpq $0x7ffffffe, %r14       # imm = 0x7FFFFFFE
ja 0x6fbf
movl $0x1, %r15d
movl $0x1, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
callq 0x2140
movl %r12d, %eax
cmpq %rax, %rbx
je 0x6fb2
incl %r15d
cvttsd2si %xmm0, %r12d
cmpl %r12d, %ebx
jge 0x6f90
cmpq %rax, %rbx
movq -0x40(%rbp), %r15
movq -0x30(%rbp), %r12
je 0x6fcf
movq (%r15), %rax
movsbl (%rax,%r14), %esi
movq %r13, %rdi
callq 0x2120
movq %rbx, %r14
cmpq %r12, %rbx
jne 0x6f77
movq %r13, %rax
addq $0x1fb8, %rsp           # imm = 0x1FB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11a7(%rip), %rdi      # 0x819a
movq %rcx, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x21f0
jmp 0x7029
jmp 0x7029
movq %rax, %r14
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x701f
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2160
movq -0x38(%rbp), %r13
jmp 0x702c
jmp 0x7029
jmp 0x7029
movq %rax, %r14
movq (%r13), %rdi
movq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x7044
movq (%rax), %rsi
incq %rsi
callq 0x2160
movq %r14, %rdi
callq 0x2240