// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1f58, %rsp           # imm = 0x1F58
movq %rsi, %r15
leaq 0x1ad5(%rip), %rsi      # 0x6159
leaq -0x29(%rbp), %rdx
movq %rdi, -0x38(%rbp)
callq 0x2c1a
movl 0x8(%r15), %r14d
xorl %r13d, %r13d
movl %r14d, -0x30(%rbp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movl %r13d, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x40(%rbp)
cmpl %r14d, %r12d
ja 0x46ff
movsd -0x40(%rbp), %xmm0
callq 0x2140
leal 0x1(%rbx), %eax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
movl %ebx, %ecx
jne 0x46cf
jnp 0x46ed
movl %r12d, %edx
movq (%r15), %rsi
xorl %edi, %edi
cmpb $0x31, (%rsi,%rdx)
sete %dil
movl %edi, -0x1f80(%rbp,%rcx,4)
incl %r12d
movl %eax, %ebx
jmp 0x46ae
andl $0x0, -0x1f80(%rbp,%rcx,4)
incl %r13d
incl -0x30(%rbp)
incl %ebx
jmp 0x46a1
addl %r13d, %r14d
movl %r14d, %edi
callq 0x5206
decl %r13d
movzbl %al, %r15d
subl %r15d, %r14d
negl %r15d
xorl %r12d, %r12d
cmpl %r13d, %r12d
ja 0x477a
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
callq 0x2140
cvttsd2si %xmm0, %rcx
leal -0x1(%rcx), %eax
movl %ecx, %edx
xorl %ecx, %ecx
movl %eax, %esi
cmpl %r14d, %esi
jae 0x4765
leal (%rsi,%rdx), %edi
cmpl %r14d, %edi
cmovael %r14d, %edi
movl %esi, %esi
cmpq %rdi, %rsi
jae 0x4761
cmpq %rsi, %rax
je 0x475c
addl -0x1f80(%rbp,%rsi,4), %ecx
incq %rsi
jmp 0x474b
addl %edx, %esi
jmp 0x473a
testb $0x1, %cl
je 0x4775
movl $0x1, -0x1f80(%rbp,%rax,4)
incl %r12d
jmp 0x471a
addl -0x30(%rbp), %r15d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x38(%rbp), %r13
cmpq %rbx, %r15
je 0x47af
movl -0x1f80(%rbp,%rbx,4), %r12d
leal 0x30(%r12), %eax
movsbl %al, %esi
movq %r13, %rdi
callq 0x2110
addl %r14d, %r12d
incq %rbx
movl %r12d, %r14d
jmp 0x4787
andl $0x1, %r14d
orl $0x30, %r14d
movq %r13, %rdi
movl %r14d, %esi
callq 0x2110
movq -0x38(%rbp), %rax
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47da
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x20a0
movq %r14, %rdi
callq 0x2230