// File: /workspace/github_cpp_makefile_star2_O1/carellano8[P]Proyecto-Redes-I-2017/hamming.cpp
// Function: hamming::codificadorHamming(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
binario hamming::codificadorHamming(binario bin){
    unsigned int i,j,k,m,n,p;
    int b[MAXMSJ],acum;
    binario s="";
    k=0;j=0;i=0;n=bin.size();//inicializacion
    
    while(k<=n){
        if(pow(2,j)==(i+1)){//si es potencia de 2 se omite el vector se rellena con 0
            j++;
            b[i]=0;
            }
        else{
            b[i]=(bin[k]=='1')?1:0;
            k++;
            }
        i++;
        }
    n+=j;
    j--;
    //~ cout<<j<<" "<<n<<endl;
    if(potencia2(n)){n--;}
    for(i=0;i<=j;i++){

        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){
            b[p-1]=1;
            }
    }
    acum=0;
    for (i = 0; i < n; i++){
        acum+=b[i];
        s+=(b[i]+'0');
    }
    //~ cout<<s<<" "<<j<<endl;
    if(acum%2==0){
        b[n]=0;
        s+='0';
    }
    else{
        b[n]=1;
        s+='1';
        }
    //~ n++;

    //~ cout<<"Tamano: "<<n<<" | Bits de paridad: "<<j<<endl<<"Binario: ";
    //~ for(i = 0; i <n ; i++){
        //~ cout<<b[i];
    //~ }
    //~ cout<<endl;

    return s;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f68, %rsp           # imm = 0x1F68
movq %rsi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, -0x38(%rbp)
movq %rax, (%rdi)
leaq 0x2402(%rip), %rdx      # 0x8159
movq %rdi, -0x40(%rbp)
movq %rdx, %rsi
callq 0x315a
movq %rbx, -0x48(%rbp)
movl 0x8(%rbx), %r12d
xorl %r15d, %r15d
movl $0x1, %ebx
movl $0x0, -0x2c(%rbp)
leal -0x1(%rbx), %r13d
movl -0x2c(%rbp), %r14d
movl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x2140
movl %ebx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
movl %r13d, %eax
jne 0x5dbb
jp 0x5dbb
incl %r14d
movl %r14d, -0x2c(%rbp)
movl $0x0, -0x1f90(%rbp,%rax,4)
jmp 0x5dd9
movl %r15d, %ecx
movq -0x48(%rbp), %rdx
movq (%rdx), %rdx
xorl %esi, %esi
cmpb $0x31, (%rdx,%rcx)
sete %sil
movl %esi, -0x1f90(%rbp,%rax,4)
incl %r15d
incl %ebx
cmpl %r12d, %r15d
jbe 0x5d7a
movl -0x2c(%rbp), %ebx
addl %ebx, %r12d
xorl %r14d, %r14d
movl $0x0, %eax
testl %r12d, %r12d
jle 0x5e39
xorl %ebx, %ebx
movl $0x1, %r15d
movl $0x1, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
callq 0x2140
cmpl %r12d, %r13d
movzbl %bl, %ebx
movl $0x1, %eax
cmovel %eax, %ebx
testb $0x1, %bl
jne 0x5e2e
cvttsd2si %xmm0, %r13d
incl %r15d
cmpl %r13d, %r12d
jge 0x5e01
movzbl %bl, %eax
andl $0x1, %eax
negl %eax
movl -0x2c(%rbp), %ebx
decl %ebx
addl %eax, %r12d
movl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rax
leal -0x1(%rax), %edx
cmpl %r12d, %edx
jae 0x5ebb
movl %edx, %ecx
xorl %esi, %esi
leal (%rdx,%rax), %edi
cmpl %r12d, %edi
cmovael %r12d, %edi
cmpl %edi, %edx
jae 0x5ea3
movl %edx, %edx
movl %edi, %r8d
movq %rcx, %rdi
subq %rdx, %rdi
leaq -0x1f90(,%rdx,4), %r9
addq %rbp, %r9
movq %rdx, %r10
subq %r8, %r10
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x5e95
addl (%r9,%r8,4), %esi
incq %r8
movq %r10, %r11
addq %r8, %r11
jne 0x5e8c
addl %r8d, %edx
addl %eax, %edx
cmpl %r12d, %edx
jb 0x5e5f
testb $0x1, %sil
je 0x5ebb
movl $0x1, -0x1f90(%rbp,%rcx,4)
incl %r14d
cmpl %ebx, %r14d
jbe 0x5e3e
movl %r12d, %ebx
testl %r12d, %r12d
movq -0x40(%rbp), %r13
je 0x5f06
xorl %r15d, %r15d
xorl %r14d, %r14d
movl -0x1f90(%rbp,%r15,4), %r12d
leal 0x30(%r12), %eax
movsbl %al, %esi
movq %r13, %rdi
callq 0x2120
addl %r12d, %r14d
incq %r15
cmpq %r15, %rbx
jne 0x5ed9
andl $0x1, %r14d
leal 0x30(%r14), %eax
jmp 0x5f0b
movb $0x30, %al
xorl %r14d, %r14d
movl %r14d, -0x1f90(%rbp,%rbx,4)
movzbl %al, %esi
movq %r13, %rdi
callq 0x2120
movq %r13, %rax
addq $0x1f68, %rsp           # imm = 0x1F68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5f37
jmp 0x5f37
movq %rax, %r14
movq -0x40(%rbp), %rax
movq (%rax), %rdi
cmpq -0x38(%rbp), %rdi
je 0x5f56
movq -0x38(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x2160
movq %r14, %rdi
callq 0x2240