// File: /workspace/github_c_makefile_star2_O3/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
movl %edi, %r12d
xorl %eax, %eax
testl %esi, %esi
cmovgl %esi, %eax
incl %eax
leaq 0x767a(%rip), %r13      # 0xa1d8
movq (%r13), %r13
decl %eax
jne 0x2b5e
leaq 0x8(%r13), %r14
cmpb $0xa, %r12b
jne 0x2b8f
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
jne 0x2b85
movq %r14, %rdi
callq 0x24da
movq %r14, %rdi
movl $0xa, %esi
jmp 0x2be1
movsbl %r12b, %esi
movl 0x10(%r13), %ecx
leaq 0x75f6(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, %ecx
jne 0x2bda
movl %r12d, -0x2c(%rbp)
movl %esi, %r12d
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
jne 0x2bc3
movq %r14, %rdi
movl %r12d, %esi
jmp 0x2be1
movl 0x10(%r13), %ecx
leaq 0x75c6(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
movl %r12d, %esi
movl -0x2c(%rbp), %r12d
cmpl %eax, %ecx
jge 0x2bfb
movq %r14, %rdi
movl %ebx, %edx
callq 0x243d
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7586(%rip), %rcx      # 0xa188
cmpl %r15d, (%rcx)
jne 0x2c3d
movq %r14, %rdi
movl %ebx, %edx
movq %rcx, %rbx
callq 0x243d
incl (%rbx)
leaq 0x75bb(%rip), %rdi      # 0xa1d8
callq 0x25db
movq %r14, %rdi
callq 0x2580
cmpl $0x20, %eax
jne 0x2ce7
movq %r14, %rdi
callq 0x24da
jmp 0x2c22
cmpl %ebx, %eax
jle 0x2dc5
movq %r14, %rdi
movl %ebx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x24da
movq %r14, %rdi
callq 0x2580
cmpl $0x20, %eax
je 0x2c4f
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
je 0x2c4f
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2e5e
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2c74
movq (%r13), %rdi
addq $0x8, %rdi
xorl %ebx, %ebx
movl $0x20, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
jne 0x2e7f
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x24da
cmpl $0x20, %eax
je 0x2e7f
incl %ebx
movq (%r13), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
je 0x2cb2
jmp 0x2e7f
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2d3a
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2cea
movq %r14, %rdi
callq 0x2565
xorl %ebx, %ebx
testl %eax, %eax
jne 0x2d5b
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x24da
cmpl $0x20, %eax
je 0x2d5b
incl %ebx
movq (%r13), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
je 0x2d0c
jmp 0x2d5b
movq (%r13), %rbx
addq $0x8, %rbx
movq %r14, %rdi
callq 0x24da
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x243d
movl $0x1, %ebx
movq %r14, %rdi
movl $0xa, %esi
callq 0x2409
leaq 0x7425(%rip), %rax      # 0xa194
movl (%rax), %edx
addl $-0x3, %edx
leaq 0x743d(%rip), %r14      # 0xa1b8
leaq 0x4295(%rip), %rsi      # 0x7017
leaq 0x458a(%rip), %r13      # 0x7313
movq %r14, %rdi
movl %r15d, %ecx
movq %r13, %r8
movq %r13, %r9
callq 0x4ff8
leaq -0x2e(%rbp), %r8
movb $0xa, (%r8)
movb %r12b, 0x1(%r8)
incl %r15d
leaq 0x426c(%rip), %rsi      # 0x701c
movq %r14, %rdi
xorl %edx, %edx
movl %r15d, %ecx
movq %r13, %r9
callq 0x4ff8
jmp 0x2bea
movq (%r13), %rdi
addq $0x8, %rdi
movl %esi, %ebx
movl $0x20, %esi
xorl %edx, %edx
callq 0x243d
movq (%r13), %rdi
addq $0x8, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x24da
movq %r14, %rdi
callq 0x2580
cmpl $0x20, %eax
je 0x2dec
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
je 0x2dec
movq (%r14), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x2e5e
cmpb $0x20, 0x8(%rax)
movq %rcx, %rax
jne 0x2e11
movq %r14, %rdi
callq 0x2565
movl $0x1, %ebx
testl %eax, %eax
jne 0x2e7f
movq %r14, %rdi
callq 0x24da
cmpl $0x20, %eax
je 0x2e7f
incl %ebx
movq (%r13), %rdi
addq $0x8, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x2565
jmp 0x2e30
movq (%r13), %rbx
addq $0x8, %rbx
movq %r14, %rdi
callq 0x24da
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x243d
movl $0x1, %ebx
movq %r14, %rdi
movl $0xa, %esi
callq 0x2409
movq (%r13), %r12
cmpq $0x0, (%r12)
je 0x2fa4
leaq 0x72f2(%rip), %r13      # 0xa194
movl (%r13), %eax
addl $-0x2, %eax
cmpl %eax, 0x10(%r12)
jl 0x2bea
leaq 0x8(%r12), %r14
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
jne 0x2ed1
movq %r14, %rdi
callq 0x24da
movl %eax, %r15d
movq %r14, %rdi
callq 0x2580
cmpl $0x20, %eax
je 0x2ec6
movq %r14, %rdi
callq 0x2580
cmpl $0xa, %eax
je 0x2ec6
movq (%r12), %rdi
addq $0x8, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
jne 0x2f16
movq %r14, %rdi
callq 0x24da
movl %eax, %r15d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
jne 0x2f7f
cmpl $0x20, %r15d
je 0x2f7f
movq (%r12), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x24da
movl %eax, %r15d
cmpl $0x20, %eax
jne 0x2f16
movl (%r13), %eax
addl $-0x2, %eax
movl $0x20, %r15d
cmpl %eax, 0x10(%r12)
jle 0x2f16
movq %r14, %rdi
callq 0x24da
movl %eax, %r15d
movq (%r12), %rdi
addq $0x8, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x243d
jmp 0x2f16
movq %r14, %rdi
movl $0xa, %esi
callq 0x2409
movq (%r12), %r12
cmpq $0x0, (%r12)
leaq 0x71ec(%rip), %rcx      # 0xa188
jne 0x2ea2
jmp 0x2fb2
leaq 0x71e9(%rip), %r13      # 0xa194
leaq 0x71d6(%rip), %rcx      # 0xa188
movl (%r13), %eax
addl $-0x2, %eax
cmpl %eax, 0x10(%r12)
jl 0x2bea
leaq 0x8(%r12), %r14
incl (%rcx)
leaq 0x7206(%rip), %rdi      # 0xa1d8
callq 0x25db
movq %r14, %rdi
callq 0x2580
movq %r14, %rdi
cmpl $0x20, %eax
jne 0x2fee
callq 0x24da
jmp 0x2fd7
callq 0x24da
movl %eax, %r15d
movq %r14, %rdi
callq 0x2565
testl %eax, %eax
jne 0x3049
cmpl $0x9, %r15d
je 0x3049
cmpl $0x20, %r15d
je 0x3049
movq (%r12), %rdi
addq $0x8, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x243d
movq %r14, %rdi
callq 0x24da
movl %eax, %r15d
cmpl $0x20, %eax
jne 0x2ff6
movl (%r13), %eax
addl $-0x2, %eax
movl $0x20, %r15d
cmpl %eax, 0x10(%r12)
jl 0x2ff6
movq %r14, %rdi
jmp 0x2fee
movq %r14, %rdi
movl $0xa, %esi
callq 0x2409
jmp 0x2bea