// File: /workspace/github_c_makefile_star2_O2/vasusharma7[P]Text-Editor/stdedit.c
// Function: add_char
int add_char(char ch,int line,int loc){
        int i = 0,y,x,j = 0;
        point *temp = data.front;
        for(i = 0; i < line; i++){
          temp = temp->tail;
        }
        //printw("%p\n",temp);
        //refresh();

        if( ch == '\n'){
            if(ipeeps(&(temp->element)) == '\n'){
                ipops(&(temp->element));
                inserts(&(temp->element),ch,loc);
            }
            else{
                inserts(&(temp->element),ch,loc);
            }
        }
        else if( (temp->element.width == (bx-3)) && (ipeeps(&(temp->element)) == '\n')){
            inserts(&(temp->element),ch,loc);
            return 0;
        }
        else if(temp->element.width >= (bx - 3)){
            if(line == lines){
                
                inserts(&(temp->element),ch,loc);
                lines++;
                //add_line(lines);
                enqueue(&data);
                while((ipeeps(&(temp->element)) == ' ') )
                    ipops(&(temp->element));
                
                if(!search_char(' ',0,temp->element)){
                    inserts(&(temp->tail->element),ipops(&(temp->element)),0);
                    ipushs(&(temp->element),'\n');
                    j = 1;
                }
                else{
                    while(  (!isEmptys(&(temp->element))) && ((i = ipops(&(temp->element))) != ' ') && ++j){
                        inserts(&(temp->tail->element),i,0);
                    }
                    
                    //inserts(&(temp->tail->element),' ',j+1);
                    ipushs(&(temp->element),'\n');
                }
                add_event(&states, "next",bx - 3,line,"","");
                char snd[2];
                snd[0] = '\n';
                snd[1] = ch;
                add_event(&states, "add",0,line+1,snd,"");
                return j;
            }
            else{
                if(loc >= (bx - 3)){
                    inserts(&(temp->tail->element),' ',0);
                    inserts(&(temp->tail->element),ch,0);
                        j++;
                    ipops(&(temp->element));
                    
                    while((ipeeps(&(temp->element)) == ' ') ||  (ipeeps(&(temp->element)) == '\n')){
                        ipops(&(temp->element));
                    }
                    //inserts(&(temp->tail->element),' ',0);
                    // ++j;
                    if(!search_char(' ',0,temp->element)){
                        inserts(&(temp->tail->element),ipops(&(temp->element)),0);
                        ipushs(&(temp->element),'\n');
                        j = 1;
                    }
                    else{
                        while( !isEmptys(&(temp->element)) && ((i = ipops(&(temp->element))) != ' ') && ++j)
                            inserts(&(temp->tail->element),i,0);
                        
                        ipushs(&(temp->element),'\n');
                    }
                }
                else{
                    inserts(&(temp->element),ch,loc);
                    i = ipops(&(temp->element));

                    while((ipeeps(&(temp->element)) == ' ') || (ipeeps(&(temp->element)) == '\n'))
                        ipops(&(temp->element));

                    //++j;            //check this well
                    //inserts(&(temp->tail->element),' ',0);
                    if(!search_char(' ',0,temp->element)){
                        inserts(&(temp->tail->element),ipops(&(temp->element)),0);
                        ipushs(&(temp->element),'\n');
                        j = 1;

                    }else{
                         inserts(&(temp->tail->element),' ',0);
                        while( !isEmptys(&(temp->element)) && ((i = ipops(&(temp->element))) != ' ') && ++j){
                            inserts(&(temp->tail->element),i,0);    
                        }
                        ipushs(&(temp->element),'\n');
                    }
                }
                
            }
            temp = temp->tail;
            int k;
            while(temp->tail && (temp->element.width >= (bx-2))){
                if(ipeeps(&(temp->element)) == '\n')
                    k = ipops(&(temp->element));

                while((ipeeps(&(temp->element)) == ' ') || (ipeeps(&(temp->element)) == '\n'))
                    k = ipops(&(temp->element));

                inserts(&(temp->tail->element),' ',0);
                if(!isEmptys(&(temp->element)))
                    k = ipops(&( temp->element));

                while( (!isEmptys(&(temp->element))) &&  (k != ' ')){
                    inserts(&(temp->tail->element),k,0);
                    k = ipops(&( temp->element));
                    if( (k == ' ') && temp->element.width > (bx - 2)){
                        k = ipops(&( temp->element));
                        inserts(&(temp->tail->element),' ',0);
                    }
                }

                ipushs(&(temp->element),'\n');
                temp = temp->tail;
            }
            if((temp->tail == NULL) && (temp->element.width >= (bx - 2))){
                lines++;
                enqueue(&data);
                while((ipeeps(&(temp->element)) == ' '))
                    k = ipops(&(temp->element));

                //inserts(&(temp->tail->element),' ',0);
                k = ipops(&( temp->element));
                while( (!isEmptys(&(temp->element))) &&  (k != ' ') && (k != '\t')){
                    inserts(&(temp->tail->element),k,0);
                    k = ipops(&( temp->element));
                    if( (k == ' ') && temp->element.width >= (bx - 2)){
                        k = ipops(&( temp->element));
                        //inserts(&(temp->tail->element),' ',0);
                    }
                }
                ipushs(&(temp->element),'\n');
            }
            
        }

        else{
            inserts(&(temp->element),ch,loc);
        }

        return j;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r13d
movl %esi, %r15d
movl %edi, %r12d
xorl %eax, %eax
testl %esi, %esi
cmovgl %esi, %eax
incl %eax
leaq 0x7705(%rip), %r14      # 0xa1d8
movq (%r14), %r14
decl %eax
jne 0x2ad3
leaq 0x8(%r14), %rbx
cmpb $0xa, %r12b
jne 0x2afe
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
jne 0x2af9
movq %rbx, %rdi
callq 0x24bf
pushq $0xa
popq %rsi
jmp 0x2b45
movsbl %r12b, %esi
movl 0x10(%r14), %ecx
leaq 0x7687(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, %ecx
jne 0x2b41
movl %esi, -0x2c(%rbp)
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
jne 0x2b2e
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
jmp 0x2b48
movl 0x10(%r14), %ecx
leaq 0x765b(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
movl -0x2c(%rbp), %esi
cmpl %eax, %ecx
jge 0x2b65
movq %rbx, %rdi
movl %r13d, %edx
callq 0x2432
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x761c(%rip), %rcx      # 0xa188
cmpl %r15d, (%rcx)
jne 0x2baa
movq %rbx, %rdi
movl %r13d, %edx
movq %rcx, %r13
callq 0x2432
incl (%r13)
leaq 0x764e(%rip), %rdi      # 0xa1d8
callq 0x25bc
movq %rbx, %rdi
callq 0x2567
cmpl $0x20, %eax
jne 0x2c48
movq %rbx, %rdi
callq 0x24bf
jmp 0x2b8f
cmpl %r13d, %eax
jle 0x2ca3
movq %rbx, %rdi
movl %r13d, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x24bf
movq %rbx, %rdi
callq 0x2567
cmpl $0x20, %eax
je 0x2bbe
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
je 0x2bbe
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
pushq $0x20
popq %rdi
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x2a46
movq (%r14), %r15
addq $0x8, %r15
testl %eax, %eax
je 0x2de3
pushq $0x20
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2dd0
movq %rbx, %rdi
callq 0x24bf
cmpl $0x20, %eax
je 0x2dd0
incl %r12d
movq (%r14), %rdi
addq $0x8, %rdi
movl %eax, %esi
jmp 0x2c12
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
pushq $0x20
popq %rdi
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x2a46
testl %eax, %eax
je 0x2d46
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2c9b
movq %rbx, %rdi
callq 0x24bf
cmpl $0x20, %eax
je 0x2c9b
incl %r13d
movq (%r14), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x2432
jmp 0x2c6d
movl %r13d, %r14d
jmp 0x2d65
movq (%r14), %rdi
addq $0x8, %rdi
movl %esi, %r15d
pushq $0x20
popq %rsi
xorl %edx, %edx
callq 0x2432
movq (%r14), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x24bf
movq %rbx, %rdi
callq 0x2567
cmpl $0x20, %eax
je 0x2cc8
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
je 0x2cc8
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
pushq $0x20
popq %rdi
xorl %esi, %esi
callq 0x2a46
testl %eax, %eax
je 0x2ddc
pushq $0x1
popq %r15
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2dd6
movq %rbx, %rdi
callq 0x24bf
cmpl $0x20, %eax
je 0x2dd6
incl %r15d
movq (%r14), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x2432
jmp 0x2d10
movq (%r14), %r13
addq $0x8, %r13
movq %rbx, %rdi
callq 0x24bf
movq %r13, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x2432
pushq $0x1
popq %r14
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x2402
leaq 0x741d(%rip), %rax      # 0xa194
movl (%rax), %edx
addl $-0x3, %edx
leaq 0x7435(%rip), %rbx      # 0xa1b8
leaq 0x428d(%rip), %rsi      # 0x7017
leaq 0x4582(%rip), %r13      # 0x7313
movq %rbx, %rdi
movl %r15d, %ecx
movq %r13, %r8
movq %r13, %r9
callq 0x4cfc
leaq -0x2e(%rbp), %r8
movb $0xa, (%r8)
movb %r12b, 0x1(%r8)
incl %r15d
leaq 0x4264(%rip), %rsi      # 0x701c
movq %rbx, %rdi
xorl %edx, %edx
movl %r15d, %ecx
movq %r13, %r9
callq 0x4cfc
movl %r14d, %r13d
jmp 0x2b53
movl %r12d, -0x2c(%rbp)
jmp 0x2dfd
movl %r15d, -0x2c(%rbp)
jmp 0x2dfd
movq (%r14), %r15
addq $0x8, %r15
movq %rbx, %rdi
callq 0x24bf
movq %r15, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x2432
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
pushq $0x20
popq %r15
pushq $0xa
popq %r12
movq %rbx, %rdi
movl %r12d, %esi
callq 0x2402
movq (%r14), %r14
leaq 0x8(%r14), %rbx
leaq 0x7376(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
cmpq $0x0, (%r14)
movl 0x10(%r14), %ecx
je 0x2f01
cmpl %eax, %ecx
jl 0x2f9d
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
jne 0x2e51
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2567
cmpl $0x20, %eax
je 0x2e46
movq %rbx, %rdi
callq 0x2567
cmpl $0xa, %eax
je 0x2e46
movq (%r14), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2e93
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r13d
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2e05
cmpl $0x20, %r13d
je 0x2e05
movq (%r14), %rdi
addq $0x8, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r13d
cmpl $0x20, %eax
jne 0x2e93
leaq 0x72bf(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
movl %r15d, %r13d
cmpl %eax, 0x10(%r14)
jle 0x2e93
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r13d
movq (%r14), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x2432
jmp 0x2e93
cmpl %eax, %ecx
movl -0x2c(%rbp), %r13d
jl 0x2b53
leaq 0x7274(%rip), %rax      # 0xa188
incl (%rax)
leaq 0x72bb(%rip), %rdi      # 0xa1d8
callq 0x25bc
movq %rbx, %rdi
callq 0x2567
movl %eax, %r12d
movq %rbx, %rdi
callq 0x24bf
cmpl $0x20, %r12d
je 0x2f22
movl %eax, %r15d
pushq $0x20
popq %r12
movq %rbx, %rdi
callq 0x254c
testl %eax, %eax
jne 0x2fa6
cmpl $0x9, %r15d
je 0x2fa6
cmpl $0x20, %r15d
je 0x2fa6
movq (%r14), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x2432
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r15d
cmpl $0x20, %eax
jne 0x2f42
leaq 0x7212(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
movl %r12d, %r15d
cmpl %eax, 0x10(%r14)
jl 0x2f42
movq %rbx, %rdi
callq 0x24bf
movl %eax, %r15d
jmp 0x2f42
movl -0x2c(%rbp), %r13d
jmp 0x2b53
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x2402
jmp 0x2b53