// File: /workspace/github_c_makefile_star2_O0/ww9210[P]xnuca_final_ssp/main.c
// Function: swap_a_patch
int swap_a_patch(){
    struct patch_attr attr;
    unsigned int value_size;
    struct executor_queue * eq;
    unsigned int queue_type, idx_to_swap;
    unsigned char * value, * tmp_buffer;
    struct ppl_list_node * cur;
    int found_queue = 0;
    void *dst;
    if(!total_ppl){
        return -1;
    }
    read(0, &attr, sizeof(struct patch_attr));
    queue_type = attr.queue_type;
    idx_to_swap  = attr.idx_to_swap;
#ifdef DEBUG
    printf("type:%d\t idx to swap: %d\n", queue_type, idx_to_swap);
#endif
    
    cur = &head;
    for(cur=cur->fd; cur!=&head; cur=cur->fd)
    {
        eq = (struct executor_queue *)((unsigned long)cur - (unsigned long)&((struct executor_queue*)0)->list);
#ifdef DEBUG
        printf("found executor at: %p\n", eq);
#endif
        if(eq->queue_type == queue_type && eq->head > idx_to_swap && eq->tail<=idx_to_swap){
            found_queue = 1;
            break;
        }
    }
    if (!found_queue){
#ifdef DEBUG
        printf("executor not found for type %d\n", queue_type);
        printf("head: %d, tail: %d\n", eq->head, eq->tail);
#endif
        return -1;
    }
    value_size = eq->value_size;
    value = malloc(value_size);
#ifdef DEBUG
    puts("xD");
#endif
    // read new patch to buffer just allocated
    //read(0, value, value_size);
    safe_read(0, value, value_size);

    tmp_buffer = (char*)malloc(value_size);
    dst = &eq->elements[idx_to_swap * value_size];
    memcpy(tmp_buffer, dst, value_size);
    memcpy(dst, value, value_size);

    write(1, tmp_buffer, value_size);

    free(tmp_buffer);
    free(value);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4c(%rbp)
cmpl $0x0, 0x7c7a(%rip)      # 0xa210
jne 0x25a4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x26de
xorl %edi, %edi
leaq -0x1c(%rbp), %rsi
movl $0x18, %edx
callq 0x1120
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x30(%rbp)
leaq 0x7d01(%rip), %rax      # 0xa2c8
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
leaq 0x7ceb(%rip), %rax      # 0xa2c8
cmpq %rax, -0x48(%rbp)
je 0x2630
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
addq $0x28, %rcx
subq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0x2c(%rbp), %eax
jne 0x2621
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
cmpl -0x30(%rbp), %eax
jbe 0x2621
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %eax
cmpl -0x30(%rbp), %eax
ja 0x2621
movl $0x1, -0x4c(%rbp)
jmp 0x2630
jmp 0x2623
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x25d6
cmpl $0x0, -0x4c(%rbp)
jne 0x2642
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x26de
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %edi
callq 0x1170
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
movl -0x20(%rbp), %edx
xorl %edi, %edi
callq 0x18f0
movl -0x20(%rbp), %eax
movl %eax, %edi
callq 0x1170
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
addq $0x38, %rax
movl -0x30(%rbp), %ecx
imull -0x20(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x20(%rbp), %eax
movl %eax, %edx
callq 0x1160
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x20(%rbp), %eax
movl %eax, %edx
callq 0x1160
movq -0x40(%rbp), %rsi
movl -0x20(%rbp), %eax
movl %eax, %edx
movl $0x1, %edi
callq 0x10a0
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)