// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movl %edx, %ebx
movl %esi, %r15d
xorl %eax, %eax
testl %esi, %esi
cmovgl %esi, %eax
incl %eax
leaq 0x7654(%rip), %r12      # 0xa1d8
movq (%r12), %r12
decl %eax
jne 0x2b84
leaq 0x8(%r12), %r14
cmpb $0xa, %dil
jne 0x2bb6
movq %r14, %rdi
callq 0x2592
cmpl $0xa, %eax
jne 0x2bac
movq %r14, %rdi
callq 0x24e8
movq %r14, %rdi
movl $0xa, %esi
jmp 0x2bf6
movl %edi, -0x2c(%rbp)
movsbl %dil, %r13d
leaq 0x75d0(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, 0x10(%r12)
jne 0x2bdd
movq %r14, %rdi
callq 0x2592
cmpl $0xa, %eax
je 0x2bf0
leaq 0x75b0(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, 0x10(%r12)
jge 0x2c10
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0x244e
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7571(%rip), %rcx      # 0xa188
cmpl %r15d, (%rcx)
jne 0x2c55
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
movq %rcx, %rbx
callq 0x244e
incl (%rbx)
leaq 0x75a3(%rip), %rdi      # 0xa1d8
callq 0x25ef
movq %r14, %rdi
callq 0x2592
cmpl $0x20, %eax
jne 0x2d0b
movq %r14, %rdi
callq 0x24e8
jmp 0x2c3a
cmpl %ebx, %eax
jle 0x2df1
movq %r14, %rdi
movl %r13d, %esi
movl %ebx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x24e8
movq %r14, %rdi
callq 0x2592
cmpl $0x20, %eax
je 0x2c6a
movq %r14, %rdi
callq 0x2592
cmpl $0xa, %eax
je 0x2c6a
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2e8e
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2c8f
testq %rcx, %rcx
je 0x2e8e
movq (%r12), %rdi
addq $0x8, %rdi
xorl %ebx, %ebx
movl $0x20, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x2577
testl %eax, %eax
jne 0x2eaf
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x24e8
cmpl $0x20, %eax
je 0x2eaf
incl %ebx
movq (%r12), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x2577
testl %eax, %eax
je 0x2cd6
jmp 0x2eaf
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2d63
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2d0e
testq %rcx, %rcx
je 0x2d63
movq %r14, %rdi
callq 0x2577
xorl %ebx, %ebx
testl %eax, %eax
jne 0x2d84
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x24e8
cmpl $0x20, %eax
je 0x2d84
incl %ebx
movq (%r12), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x2577
testl %eax, %eax
je 0x2d35
jmp 0x2d84
movq (%r12), %rbx
addq $0x8, %rbx
movq %r14, %rdi
callq 0x24e8
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x244e
movl $0x1, %ebx
movq %r14, %rdi
movl $0xa, %esi
callq 0x2412
leaq 0x73fc(%rip), %rax      # 0xa194
movl (%rax), %edx
addl $-0x3, %edx
leaq 0x7414(%rip), %r14      # 0xa1b8
leaq 0x426c(%rip), %rsi      # 0x7017
leaq 0x4561(%rip), %r13      # 0x7313
movq %r14, %rdi
movl %r15d, %ecx
movq %r13, %r8
movq %r13, %r9
callq 0x508c
leaq -0x2e(%rbp), %r8
movb $0xa, (%r8)
movl -0x2c(%rbp), %eax
movb %al, 0x1(%r8)
incl %r15d
leaq 0x4240(%rip), %rsi      # 0x701c
movq %r14, %rdi
xorl %edx, %edx
movl %r15d, %ecx
movq %r13, %r9
callq 0x508c
jmp 0x2bff
movq (%r12), %rdi
addq $0x8, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x244e
movq (%r12), %rdi
addq $0x8, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x24e8
movq %r14, %rdi
callq 0x2592
cmpl $0x20, %eax
je 0x2e17
movq %r14, %rdi
callq 0x2592
cmpl $0xa, %eax
je 0x2e17
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2e8e
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2e3c
testq %rcx, %rcx
je 0x2e8e
movq %r14, %rdi
callq 0x2577
movl $0x1, %ebx
testl %eax, %eax
jne 0x2eaf
movq %r14, %rdi
callq 0x24e8
cmpl $0x20, %eax
je 0x2eaf
incl %ebx
movq (%r12), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x2577
jmp 0x2e60
movq (%r12), %rbx
addq $0x8, %rbx
movq %r14, %rdi
callq 0x24e8
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x244e
movl $0x1, %ebx
movq %r14, %rdi
movl $0xa, %esi
callq 0x2412
movq (%r12), %r12
cmpq $0x0, (%r12)
sete %al
je 0x2ff8
leaq 0x72bf(%rip), %rcx      # 0xa194
movl (%rcx), %ecx
addl $-0x2, %ecx
cmpl %ecx, 0x10(%r12)
jl 0x2ff0
leaq 0x10(%r12), %r13
leaq 0x8(%r12), %r15
movq %r15, %rdi
callq 0x2592
cmpl $0xa, %eax
jne 0x2f07
movq %r15, %rdi
callq 0x24e8
movl %eax, %r14d
movq %r15, %rdi
callq 0x2592
cmpl $0x20, %eax
je 0x2efc
movq %r15, %rdi
callq 0x2592
cmpl $0xa, %eax
je 0x2efc
movq (%r12), %rdi
addq $0x8, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x244e
movq %r15, %rdi
callq 0x2577
testl %eax, %eax
jne 0x2f4c
movq %r15, %rdi
callq 0x24e8
movl %eax, %r14d
movq %r15, %rdi
callq 0x2577
testl %eax, %eax
jne 0x2fb9
cmpl $0x20, %r14d
je 0x2fb9
movq (%r12), %rdi
addq $0x8, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x244e
movq %r15, %rdi
callq 0x24e8
movl %eax, %r14d
cmpl $0x20, %eax
jne 0x2f4c
leaq 0x720d(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
movl $0x20, %r14d
cmpl %eax, (%r13)
jle 0x2f4c
movq %r15, %rdi
callq 0x24e8
movl %eax, %r14d
movq (%r12), %rdi
addq $0x8, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x244e
jmp 0x2f4c
movq %r15, %rdi
movl $0xa, %esi
callq 0x2412
movq (%r12), %r12
cmpq $0x0, (%r12)
sete %al
je 0x2ff8
leaq 0x10(%r12), %r13
leaq 0x71b4(%rip), %rcx      # 0xa194
movl (%rcx), %ecx
addl $-0x2, %ecx
cmpl %ecx, 0x10(%r12)
jge 0x2eea
testb %al, %al
je 0x2bff
leaq 0x7195(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
cmpl %eax, 0x10(%r12)
jl 0x2bff
leaq 0x8(%r12), %r14
leaq 0x716d(%rip), %rax      # 0xa188
incl (%rax)
leaq 0x71b4(%rip), %rdi      # 0xa1d8
callq 0x25ef
movq %r14, %rdi
callq 0x2592
movq %r14, %rdi
cmpl $0x20, %eax
jne 0x3040
callq 0x24e8
jmp 0x3029
callq 0x24e8
movl %eax, %r15d
movq %r14, %rdi
callq 0x2577
testl %eax, %eax
jne 0x30a0
cmpl $0x9, %r15d
je 0x30a0
cmpl $0x20, %r15d
je 0x30a0
movq (%r12), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x244e
movq %r14, %rdi
callq 0x24e8
movl %eax, %r15d
cmpl $0x20, %eax
jne 0x3048
leaq 0x710b(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x2, %eax
movl $0x20, %r15d
cmpl %eax, 0x10(%r12)
jl 0x3048
movq %r14, %rdi
jmp 0x3040
movq %r14, %rdi
movl $0xa, %esi
callq 0x2412
jmp 0x2bff