// File: /workspace/github_c_makefile_star2_O2/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, %r15d
xorl %eax, %eax
testl %edi, %edi
cmovgl %edi, %eax
incl %eax
leaq 0x71b6(%rip), %rbx      # 0xa1d8
movq (%rbx), %rbx
decl %eax
jne 0x3022
andw $0x0, -0x2c(%rbp)
leaq 0x8(%rbx), %r13
testl %r14d, %r14d
je 0x305e
movq %r13, %rdi
callq 0x254c
testl %eax, %eax
jne 0x317f
cmpl %r14d, 0x10(%rbx)
jne 0x3140
movq %r13, %rdi
callq 0x24bf
jmp 0x3158
leaq 0x712f(%rip), %rax      # 0xa194
movl (%rax), %r12d
leal -0x2(%r12), %eax
cmpl %eax, 0x10(%rbx)
jge 0x30da
movq %r13, -0x38(%rbp)
movq %r15, -0x40(%rbp)
movq (%rbx), %r12
addq $0x8, %r12
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
callq 0x2180
movq %rax, %r14
leaq -0x2a(%rbp), %r13
andw $0x0, (%r13)
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x254c
testl %eax, %eax
jne 0x30e3
movq %r12, %rdi
callq 0x24bf
movb %al, -0x2a(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x2260
movq %r14, %rdi
callq 0x20c0
leaq 0x2(%rax), %rsi
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
incq %r15
jmp 0x309c
addl $-0x3, %r12d
jmp 0x321b
movq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x254c
testl %eax, %eax
jne 0x3100
movq %r12, %rdi
callq 0x24bf
cmpl $0xa, %eax
jne 0x30f3
cmpb $0x0, (%r14)
movq -0x40(%rbp), %r13
je 0x31b4
movq %r15, %r12
subq $0x1, %r12
jb 0x31b4
leaq 0x7072(%rip), %rax      # 0xa194
movl (%rax), %eax
addl $-0x3, %eax
cmpl %eax, 0x10(%rbx)
jge 0x3188
movsbl -0x1(%r14,%r15), %esi
movq -0x38(%rbp), %rdi
callq 0x2402
movq %r12, %r15
jmp 0x3111
movq %r13, %rdi
callq 0x254c
testl %eax, %eax
jne 0x317f
leal -0x1(%r14), %esi
movq %r13, %rdi
callq 0x24ed
leaq -0x2c(%rbp), %r8
movb %al, (%r8)
leaq 0x7052(%rip), %rdi      # 0xa1b8
leaq 0x3eb3(%rip), %rsi      # 0x7020
leaq 0x419f(%rip), %r9       # 0x7313
movl %r14d, %edx
movl %r15d, %ecx
callq 0x4cfc
movl 0x10(%rbx), %r12d
jmp 0x321b
pushq $0xa
popq %rsi
movq -0x38(%rbp), %rdi
callq 0x2402
movq %r15, %r12
subq $0x1, %r12
jb 0x31b4
movq (%rbx), %rdi
addq $0x8, %rdi
movsbl -0x1(%r14,%r15), %esi
callq 0x2402
movq %r12, %r15
jmp 0x3197
movq (%rbx), %rax
cmpl $0x0, 0x10(%rax)
jne 0x31d6
leal 0x1(%r13), %esi
leaq 0x7010(%rip), %rdi      # 0xa1d8
callq 0x2671
leaq 0x6fb4(%rip), %rax      # 0xa188
decl (%rax)
movl 0x10(%rbx), %r15d
movq %r14, %rdi
callq 0x20c0
subl %eax, %r15d
leaq 0x6fcc(%rip), %rdi      # 0xa1b8
leaq 0x3e2d(%rip), %rsi      # 0x7020
leaq 0x4118(%rip), %r8       # 0x7312
leaq 0x4112(%rip), %r9       # 0x7313
movl %r15d, %edx
movl %r13d, %ecx
callq 0x4cfc
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