// File: /workspace/github_c_makefile_star2_O3/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 0x121f0(%rip), %rax     # 0x21a90
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0xf9ae
movl %ecx, %r15d
movl %edx, %r14d
movl %edi, %ebp
xorl %edi, %edi
callq 0x21b0
movl %r14d, %ebx
addq %rax, %rbx
movq 0x10(%rsp), %rax
movq (%rax), %r14
testb $0x1, %r15b
jne 0xf8fa
testq %r14, %r14
je 0xf9bd
cmpq %rbx, 0x10(%r14)
jge 0xf948
movq %r14, %r12
movq (%r14), %r14
testq %r14, %r14
je 0xf98d
cmpq %rbx, 0x10(%r14)
jl 0xf8e0
jmp 0xf988
testq %r14, %r14
je 0xf9bd
xorl %r12d, %r12d
cmpq %rbx, 0x10(%r14)
jge 0xf94d
movq 0x8(%r14), %rax
cmpl %ebp, (%rax)
jne 0xf93b
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4(%rax)
jne 0xf93b
testb $0x2, %r15b
jne 0xf9ae
movq %r12, %rdi
callq 0xfaf9
movq %r12, %r14
testq %r12, %r12
je 0xf9bd
movq %r14, %r12
movq (%r14), %r14
testq %r14, %r14
jne 0xf906
jmp 0xf98d
xorl %r12d, %r12d
jmp 0xf988
movq %r12, %r13
movq %r14, %rax
movq 0x8(%r14), %rcx
cmpl %ebp, (%rcx)
jne 0xf97a
movl 0xc(%rsp), %edx
cmpl %edx, 0x4(%rcx)
jne 0xf97a
testb $0x2, %r15b
jne 0xf9ae
movq %r13, %rdi
callq 0xfaf9
movq %r13, %rax
testq %r13, %r13
je 0xf988
movq %rax, %r13
movq (%rax), %rcx
movq %rcx, %rax
testq %rcx, %rcx
jne 0xf953
testq %r12, %r12
je 0xf9bd
movq (%r12), %r14
movl $0x1, %edi
movl $0x18, %esi
callq 0x2110
movq %rax, (%r12)
testq %rax, %rax
jne 0xf9de
callq 0x2f94
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %r12
movq (%r12), %r14
movl $0x1, %edi
movl $0x18, %esi
callq 0x2110
movq %rax, (%r12)
testq %rax, %rax
je 0xfa25
movq %rax, %r15
movq %r14, (%rax)
movl $0x8, %edi
callq 0x22b0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0xfa20
movl %ebp, (%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%rax)
movq %rbx, 0x10(%r15)
movq 0x10(%rsp), %rax
incq 0x8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfab6
callq 0x2fa9
callq 0x2fbe