// File: /workspace/github_cpp_makefile_star2_O1/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 0x14a2(%rip), %rdx      # 0x8159
movq %rdx, %rsi
callq 0x315a
movq 0x8(%r14), %rdx
movq %rdx, %rcx
subq $0x1, %rcx
movq %r13, -0x38(%rbp)
movq %rbx, -0x70(%rbp)
jb 0x7040
movq (%r14), %rax
movsbl (%rax,%rcx), %eax
movl %eax, -0x3c(%rbp)
leaq -0x98(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2170
movq %r15, -0x78(%rbp)
leaq -0x98(%rbp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x21c0
movq (%r12), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x6d28
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2160
movq 0x8(%r14), %r15
movl %r15d, %esi
testl %r15d, %r15d
movq %rsi, -0x30(%rbp)
movq %r14, -0x48(%rbp)
je 0x6e26
movq (%r14), %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 0x6d45
xorl %r14d, %r14d
leaq 0x13e0(%rip), %r12      # 0x8147
movl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
leal -0x1(%rax), %edx
movq %r12, %rcx
cmpl %r15d, %edx
jae 0x6dd3
xorl %ecx, %ecx
leal (%rdx,%rax), %esi
cmpl %r15d, %esi
cmovael %r15d, %esi
cmpl %esi, %edx
jae 0x6dbe
movl %edx, %edx
movl %esi, %esi
leaq -0x1fe0(,%rdx,4), %rdi
addq %rbp, %rdi
movq %rdx, %r8
subq %rsi, %r8
xorl %esi, %esi
addl (%rdi,%rsi,4), %ecx
incq %rsi
movq %r8, %r9
addq %rsi, %r9
jne 0x6dae
addl %esi, %edx
addl %eax, %edx
cmpl %r15d, %edx
jb 0x6d89
testb $0x1, %cl
leaq 0x13a4(%rip), %rcx      # 0x8173
cmoveq %r12, %rcx
movl $0x1, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2190
incl %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
cmpl %r15d, %eax
jb 0x6d67
xorl %eax, %eax
xorl %r14d, %r14d
movq -0x30(%rbp), %rcx
addl -0x1fe0(%rbp,%rax,4), %r14d
incq %rax
cmpq %rax, %rcx
jne 0x6e0c
andl $0x1, %r14d
orl $0x30, %r14d
jmp 0x6e2c
movl $0x30, %r14d
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x30c0
movq -0x60(%rbp), %rbx
testq %rbx, %rbx
je 0x6efe
decq %rbx
xorl %r12d, %r12d
movq 0x12ad(%rip), %xmm0     # 0x8110
movapd 0x12b5(%rip), %xmm2     # 0x8120
xorl %r13d, %r13d
movq -0x68(%rbp), %rax
cmpb $0x31, (%rax,%r13)
jne 0x6eeb
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 0x126c(%rip), %xmm3     # 0x8110
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x1270(%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 0x1258(%rip), %xmm0     # 0x8130
cvttsd2si %xmm0, %r12
andq %rcx, %r12
orq %rax, %r12
movdqa %xmm3, %xmm0
movapd %xmm4, %xmm2
incq %r13
addq $-0x1, %rbx
jb 0x6e6e
movq -0x38(%rbp), %r13
jmp 0x6f01
xorl %r12d, %r12d
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x6f1a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2160
movl -0x3c(%rbp), %ecx
cmpl %ecx, %r14d
je 0x6f69
cmpl $0x1, %r12d
jbe 0x6f69
xorl %ebx, %ebx
subl $0x1, %r12d
cmovbl %ebx, %r12d
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
cmpb $0x31, (%rax,%r12)
leaq 0x1202(%rip), %rax      # 0x8147
leaq 0x1227(%rip), %rcx      # 0x8173
cmoveq %rax, %rcx
movl $0x1, %edx
movl $0x1, %r8d
movq %r12, %rsi
callq 0x2190
movq -0x30(%rbp), %r12
jmp 0x6f84
cmpl $0x2, %r12d
setae %al
cmpl %ecx, %r14d
sete %cl
andb %al, %cl
movzbl %cl, %ebx
negl %ebx
orl $0x1, %ebx
movq -0x30(%rbp), %r12
movq -0x78(%rbp), %rax
movl %ebx, (%rax)
cmpl $-0x1, %ebx
je 0x702b
movq 0x8(%r13), %rdx
leaq 0x11bb(%rip), %rcx      # 0x8159
movq %r13, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x2210
testl %r15d, %r15d
je 0x702b
xorl %r14d, %r14d
leaq 0x1(%r14), %rbx
cmpq $0x7ffffffe, %r14       # imm = 0x7FFFFFFE
ja 0x700f
movl $0x1, %r15d
xorl %r12d, %r12d
movl $0x1, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
callq 0x2140
movl %r13d, %ecx
movl $0x1, %eax
cmpq %rcx, %rbx
je 0x6fed
movzbl %r12b, %eax
movl %eax, %r12d
testb $0x1, %al
jne 0x7001
cvttsd2si %xmm0, %r13d
incl %r15d
cmpl %r13d, %ebx
jge 0x6fcf
testb $0x1, %r12b
movq -0x38(%rbp), %r13
movq -0x30(%rbp), %r12
jne 0x7023
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsbl (%rax,%r14), %esi
movq %r13, %rdi
callq 0x2120
movq %rbx, %r14
cmpq %r12, %rbx
jne 0x6fb3
movq %r13, %rax
addq $0x1fb8, %rsp           # imm = 0x1FB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1153(%rip), %rdi      # 0x819a
movq %rcx, %rsi
xorl %eax, %eax
callq 0x21f0
jmp 0x7077
jmp 0x7077
jmp 0x7077
movq %rax, %r14
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x707a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2160
jmp 0x707a
jmp 0x7077
movq %rax, %r14
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x7095
movq (%rax), %rsi
incq %rsi
callq 0x2160
movq %r14, %rdi
callq 0x2240
nop