// File: /workspace/github_c_makefile_star2_O1/Ziddykins[P]Dawn/src/status.c
// Function: add_event
void add_event(enum Events event, int e_data, unsigned int offset, int flags) { //flags -> enum event_mode (status.h)
    if(elist == 0)
        return;
    struct event_list * celist = (struct event_list *)elist;

    time_t newtime = time(0) + offset;
    struct event_node * tmp = celist->head, * prev = 0;
    if(flags & UNIQUE) {
        while(tmp != 0 && tmp->event_time < newtime) { //go to the place where we need to insert the new event
            if(tmp->elem->event == event && tmp->elem->data == e_data) {
                if(flags & KEEP) {
                    return;
                } else {
                    tmp = prev; //move to previous event
                    remove_event(prev); //remove next event
                }
            }
            prev = tmp;
            if(tmp != 0)
                tmp = tmp->next;
        }
        struct event_node * scanner = tmp, * prev_scanner = prev;
        while(scanner != 0) {
            if(tmp->elem->event == event && tmp->elem->data == e_data) {
                if(flags & KEEP) {
                    return;
                } else {
                    scanner = prev_scanner;
                    remove_event(prev_scanner);
                }
            }
            prev_scanner = scanner;
            if(scanner != 0)
                scanner = scanner->next;
        }
    } else {
        while(tmp != 0 && tmp->event_time < newtime) { //go to the place where we need to insert the new event
            prev = tmp;
            tmp = tmp->next;
        }
    }

    if(prev == 0) { //create a new node from scratch
        struct event_node * prev_head = celist->head;
        CALLEXIT(!(celist->head = calloc(1, sizeof *celist->head)))
        celist->head->next = prev_head;
        tmp = celist->head;
    } else { //or insert it where it belongs
        struct event_node * prevnext = prev->next;
        CALLEXIT(!(prev->next = calloc(1, sizeof *prev)))
        tmp = prev->next;
        tmp->next = prevnext;
    }
    CALLEXIT(!(tmp->elem = malloc(sizeof *tmp->elem)))
    tmp->elem->event = event;
    tmp->elem->data = e_data;
    tmp->event_time = newtime;

    celist->len++;
    update_alarm(); //head may have been replaced so we reset the alarm to the next event in the queue
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, 0xc(%rsp)
movq 0x124c0(%rip), %rax     # 0x21a90
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0xf6f5
movl %ecx, %r14d
movl %edx, %r15d
movl %edi, %ebp
xorl %edi, %edi
callq 0x21b0
movl %r15d, %r13d
addq %rax, %r13
movq 0x10(%rsp), %rax
movq (%rax), %r12
testb $0x1, %r14b
jne 0xf629
testq %r12, %r12
je 0xf67e
cmpq %r13, 0x10(%r12)
jge 0xf67e
movq %r12, %rbx
movq (%r12), %r12
testq %r12, %r12
je 0xf6ce
cmpq %r13, 0x10(%r12)
jl 0xf60d
jmp 0xf6ce
testq %r12, %r12
je 0xf682
xorl %r15d, %r15d
cmpq %r13, 0x10(%r12)
jge 0xf686
movq 0x8(%r12), %rax
cmpl %ebp, (%rax)
jne 0xf664
movq %r12, %rbx
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4(%rax)
jne 0xf667
testb $0x2, %r14b
jne 0xf6f5
movq %r15, %rdi
callq 0xf843
movq %r15, %rbx
jmp 0xf667
movq %r12, %rbx
testq %rbx, %rbx
je 0xf671
movq (%rbx), %r12
jmp 0xf674
xorl %r12d, %r12d
movq %rbx, %r15
testq %r12, %r12
jne 0xf631
jmp 0xf689
xorl %ebx, %ebx
jmp 0xf6ce
xorl %ebx, %ebx
jmp 0xf689
movq %r15, %rbx
testq %r12, %r12
je 0xf6ce
movq %rbx, %r15
movq %r12, %rax
movq 0x8(%r12), %rcx
cmpl %ebp, (%rcx)
jne 0xf6b7
movl 0xc(%rsp), %edx
cmpl %edx, 0x4(%rcx)
jne 0xf6b7
testb $0x2, %r14b
jne 0xf6f5
movq %r15, %rdi
callq 0xf843
movq %r15, %rax
testq %rax, %rax
je 0xf6c1
movq (%rax), %rcx
jmp 0xf6c3
xorl %ecx, %ecx
movq %rax, %r15
movq %rcx, %rax
testq %rcx, %rcx
jne 0xf694
testq %rbx, %rbx
je 0xf704
movq (%rbx), %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x2110
movq %rax, (%rbx)
testq %rax, %rax
je 0xf76a
movq %rax, %r14
movq %r15, (%rax)
jmp 0xf729
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r15
movq (%r15), %rbx
movl $0x1, %edi
movl $0x18, %esi
callq 0x2110
movq %rax, (%r15)
testq %rax, %rax
je 0xf76f
movq %rax, %r14
movq %rbx, (%rax)
movl $0x8, %edi
callq 0x22b0
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0xf765
movl %ebp, (%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%rax)
movq %r13, 0x10(%r14)
movq 0x10(%rsp), %rax
incq 0x8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf800
callq 0x2fa9
callq 0x2f94
callq 0x2fbe