// File: /workspace/github_c_makefile_star2_O1/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 0x70b7(%rip), %rbx      # 0xa1d8
movq (%rbx), %rbx
decl %eax
jne 0x3121
movw $0x0, -0x2c(%rbp)
leaq 0x8(%rbx), %r15
testl %r14d, %r14d
je 0x315e
movq %r15, %rdi
callq 0x2577
testl %eax, %eax
jne 0x328d
cmpl %r14d, 0x10(%rbx)
jne 0x324e
movq %r15, %rdi
callq 0x24e8
jmp 0x3266
leaq 0x702f(%rip), %rax      # 0xa194
movl (%rax), %r12d
leal -0x2(%r12), %eax
cmpl %eax, 0x10(%rbx)
jge 0x3245
movq %r13, -0x38(%rbp)
movq (%rbx), %r12
addq $0x8, %r12
movl $0x2, %edi
movl $0x1, %esi
callq 0x2180
movq %rax, %r14
movb $0x0, (%rax)
movw $0x0, -0x2a(%rbp)
movq %r12, %rdi
callq 0x2577
xorl %r13d, %r13d
testl %eax, %eax
jne 0x31eb
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x24e8
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 0x2577
testl %eax, %eax
je 0x31ae
movq %r15, %rdi
callq 0x2577
testl %eax, %eax
jne 0x3204
movq %r15, %rdi
callq 0x24e8
cmpl $0xa, %eax
jne 0x31f7
cmpb $0x0, (%r14)
leaq 0x6f85(%rip), %r12      # 0xa194
je 0x32c2
testl %r13d, %r13d
je 0x32a3
movslq %r13d, %r13
movl (%r12), %eax
addl $-0x3, %eax
cmpl %eax, 0x10(%rbx)
jge 0x3296
movsbl -0x1(%r14,%r13), %esi
movq %r15, %rdi
callq 0x2412
decq %r13
jne 0x3221
xorl %r13d, %r13d
jmp 0x32a3
addl $-0x3, %r12d
jmp 0x332e
movq %r15, %rdi
callq 0x2577
testl %eax, %eax
jne 0x328d
leal -0x1(%r14), %esi
movq %r15, %rdi
callq 0x2519
leaq -0x2c(%rbp), %r8
movb %al, (%r8)
leaq 0x6f44(%rip), %rdi      # 0xa1b8
leaq 0x3da5(%rip), %rsi      # 0x7020
leaq 0x4091(%rip), %r9       # 0x7313
movl %r14d, %edx
movl %r13d, %ecx
callq 0x508c
movl 0x10(%rbx), %r12d
jmp 0x332e
movq %r15, %rdi
movl $0xa, %esi
callq 0x2412
testl %r13d, %r13d
jle 0x32c2
movl %r13d, %r15d
movq (%rbx), %rdi
addq $0x8, %rdi
movsbl -0x1(%r14,%r15), %esi
callq 0x2412
decq %r15
jne 0x32ab
movq (%rbx), %rax
cmpl $0x0, 0x10(%rax)
movq -0x38(%rbp), %r12
jne 0x32e9
leal 0x1(%r12), %esi
leaq 0x6efd(%rip), %rdi      # 0xa1d8
callq 0x26b6
leaq 0x6ea1(%rip), %rax      # 0xa188
decl (%rax)
movl 0x10(%rbx), %r15d
movq %r14, %rdi
callq 0x20c0
subl %eax, %r15d
leaq 0x6eb9(%rip), %rdi      # 0xa1b8
leaq 0x3d1a(%rip), %rsi      # 0x7020
leaq 0x4005(%rip), %r8       # 0x7312
leaq 0x3fff(%rip), %r9       # 0x7313
movl %r15d, %edx
movl %r12d, %ecx
callq 0x508c
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