// File: /workspace/github_c_makefile_star2_O3/vasusharma7[P]Text-Editor/stdedit.c
// Function: backspace
int backspace(int y, int x){
    int i;
    point *temp = data.front;
    for(i = 0; i < y; i++){
        temp = temp->tail;  
    }
    char str[2] = {'\0'};
    //printw("%d",x);
    //refresh();  
    
    if(x == 0){
        if(temp->element.width >= (bx - 2))
            return (bx - 3);
        istack *temp2 = &(temp->tail->element);
        char *str = (char *)calloc(2,1);
        str[0] = '\0';
        char ch[2] = {'\0'};
        i = 0;
        while(!isEmptys(temp2)){

            ch[0] = ipops(temp2);
            i++;
            strcat(str,ch);
            str = realloc(str,strlen(str) +2);

        }
        int t = temp->element.width; 
        if(!isEmptys(&(temp->element))){
                while(ipops(&(temp->element)) != '\n');
            }   
        if(strlen(str)){
            
            while(i--){
                if(temp->element.width >= (bx - 3)){
                        ipushs(&(temp->element),'\n');
                     break;
                 }
                ipushs(&(temp->element),str[i]);
                
            }
            i++;
            if(i >=1 ){
                while(i--)
                    ipushs(&(temp->tail->element),str[i]);
            }
        }
        if(temp->tail->element.width == 0){
            dequeueloc(&data,y+1);
            lines--;
        }
        add_event(&states,"back",temp->element.width - strlen(str),y,"\n","");

        return (temp->element.width - strlen(str));   //check this well
        //return (temp->element.width - 1);
    }
    else if(!isEmptys(&(temp->element))){
        if(x == temp->element.width){
            str[0] = ipops(&(temp->element));
            add_event(&states,"back",x,y,str,"");
        }
        else if(!isEmptys(&(temp->element))){
            str[0] = ipoploc(&(temp->element),x-1);
            add_event(&states,"back",x,y,str,"");

        }
        
    }
    
    int n_x = temp->element.width;
    return n_x;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r14d
movl %edi, %r13d
xorl %eax, %eax
testl %edi, %edi
cmovgl %edi, %eax
incl %eax
leaq 0x710e(%rip), %rbx      # 0xa1d8
movq (%rbx), %rbx
decl %eax
jne 0x30ca
movw $0x0, -0x2c(%rbp)
leaq 0x8(%rbx), %r15
testl %r14d, %r14d
je 0x3107
movq %r15, %rdi
callq 0x2565
testl %eax, %eax
jne 0x3230
cmpl %r14d, 0x10(%rbx)
jne 0x31f1
movq %r15, %rdi
callq 0x24da
jmp 0x3209
leaq 0x7086(%rip), %rax      # 0xa194
movl (%rax), %r12d
leal -0x2(%r12), %eax
cmpl %eax, 0x10(%rbx)
jge 0x31e8
movq %r13, -0x38(%rbp)
movq (%rbx), %r12
addq $0x8, %r12
movl $0x2, %edi
movl $0x1, %esi
callq 0x2180
movq %rax, %r14
movw $0x0, -0x2a(%rbp)
movq %r12, %rdi
callq 0x2565
xorl %r13d, %r13d
testl %eax, %eax
jne 0x3191
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x24da
movb %al, -0x2a(%rbp)
incl %r13d
movq %r14, %rdi
leaq -0x2a(%rbp), %rsi
callq 0x2260
movq %r14, %rdi
callq 0x20c0
leaq 0x2(%rax), %rsi
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
movq %r12, %rdi
callq 0x2565
testl %eax, %eax
je 0x3154
movq %r15, %rdi
callq 0x2565
testl %eax, %eax
jne 0x31aa
movq %r15, %rdi
callq 0x24da
cmpl $0xa, %eax
jne 0x319d
cmpb $0x0, (%r14)
sete %al
testl %r13d, %r13d
sete %cl
orb %al, %cl
jne 0x325d
movslq %r13d, %r12
leaq 0x6fcb(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, 0x10(%rbx)
jge 0x3239
movsbl -0x1(%r14,%r12), %esi
movq %r15, %rdi
callq 0x2409
decq %r12
jne 0x31c2
jmp 0x325d
addl $-0x3, %r12d
jmp 0x32c9
movq %r15, %rdi
callq 0x2565
testl %eax, %eax
jne 0x3230
leal -0x1(%r14), %esi
movq %r15, %rdi
callq 0x250b
leaq -0x2c(%rbp), %r8
movb %al, (%r8)
leaq 0x6fa1(%rip), %rdi      # 0xa1b8
leaq 0x3e02(%rip), %rsi      # 0x7020
leaq 0x40ee(%rip), %r9       # 0x7313
movl %r14d, %edx
movl %r13d, %ecx
callq 0x4ff8
movl 0x10(%rbx), %r12d
jmp 0x32c9
movq %r15, %rdi
movl $0xa, %esi
callq 0x2409
movq (%rbx), %rdi
addq $0x8, %rdi
movsbl -0x1(%r14,%r12), %esi
callq 0x2409
decq %r12
jne 0x3246
movq (%rbx), %rax
cmpl $0x0, 0x10(%rax)
movq -0x38(%rbp), %r12
jne 0x3284
leal 0x1(%r12), %esi
leaq 0x6f62(%rip), %rdi      # 0xa1d8
callq 0x269b
leaq 0x6f06(%rip), %rax      # 0xa188
decl (%rax)
movl 0x10(%rbx), %r15d
movq %r14, %rdi
callq 0x20c0
subl %eax, %r15d
leaq 0x6f1e(%rip), %rdi      # 0xa1b8
leaq 0x3d7f(%rip), %rsi      # 0x7020
leaq 0x406a(%rip), %r8       # 0x7312
leaq 0x4064(%rip), %r9       # 0x7313
movl %r15d, %edx
movl %r12d, %ecx
callq 0x4ff8
movl 0x10(%rbx), %r12d
movq %r14, %rdi
callq 0x20c0
subl %eax, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq