// File: /workspace/github_c_makefile_star2_O0/janet-lang[P]janet/src/core/filewatch.c
// Function: watcher_callback_read
static void watcher_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
    JanetStream *stream = fiber->ev_stream;
    JanetWatcher *watcher = *((JanetWatcher **) fiber->ev_state);
    char buf[1024];
    switch (event) {
        default:
            break;
        case JANET_ASYNC_EVENT_MARK:
            janet_mark(janet_wrap_abstract(watcher));
            break;
        case JANET_ASYNC_EVENT_CLOSE:
            janet_schedule(fiber, janet_wrap_nil());
            janet_async_end(fiber);
            break;
        case JANET_ASYNC_EVENT_ERR: {
            janet_schedule(fiber, janet_wrap_nil());
            janet_async_end(fiber);
            break;
        }
    read_more:
        case JANET_ASYNC_EVENT_HUP:
        case JANET_ASYNC_EVENT_INIT:
        case JANET_ASYNC_EVENT_READ: {
            Janet name = janet_wrap_nil();

            /* Assumption - read will never return partial events *
             * From documentation:
             *
             * The behavior when the buffer given to read(2) is too small to
             * return information about the next event depends on the kernel
             * version: before Linux 2.6.21, read(2) returns 0; since Linux
             * 2.6.21, read(2) fails with the error EINVAL.  Specifying a buffer
             * of size
             *
             *     sizeof(struct inotify_event) + NAME_MAX + 1
             *
             * will be sufficient to read at least one event. */
            ssize_t nread;
            do {
                nread = read(stream->handle, buf, sizeof(buf));
            } while (nread == -1 && errno == EINTR);

            /* Check for errors - special case errors that can just be waited on to fix */
            if (nread == -1) {
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    break;
                }
                janet_cancel(fiber, janet_ev_lasterr());
                fiber->ev_state = NULL;
                janet_async_end(fiber);
                break;
            }
            if (nread < (ssize_t) sizeof(struct inotify_event)) break;

            /* Iterate through all events read from the buffer */
            char *cursor = buf;
            while (cursor < buf + nread) {
                struct inotify_event inevent;
                memcpy(&inevent, cursor, sizeof(inevent));
                cursor += sizeof(inevent);
                /* Read path of inevent */
                if (inevent.len) {
                    name = janet_cstringv(cursor);
                    cursor += inevent.len;
                }

                /* Got an event */
                Janet path = janet_table_get(watcher->watch_descriptors, janet_wrap_integer(inevent.wd));
                JanetKV *event = janet_struct_begin(6);
                janet_struct_put(event, janet_ckeywordv("wd"), janet_wrap_integer(inevent.wd));
                janet_struct_put(event, janet_ckeywordv("wd-path"), path);
                if (janet_checktype(name, JANET_NIL)) {
                    /* We were watching a file directly, so path is the full path. Split into dirname / basename */
                    JanetString spath = janet_unwrap_string(path);
                    const uint8_t *cursor = spath + janet_string_length(spath);
                    const uint8_t *cursor_end = cursor;
                    while (cursor > spath && cursor[0] != '/') {
                        cursor--;
                    }
                    if (cursor == spath) {
                        janet_struct_put(event, janet_ckeywordv("dir-name"), path);
                        janet_struct_put(event, janet_ckeywordv("file-name"), name);
                    } else {
                        janet_struct_put(event, janet_ckeywordv("dir-name"), janet_wrap_string(janet_string(spath, (cursor - spath))));
                        janet_struct_put(event, janet_ckeywordv("file-name"), janet_wrap_string(janet_string(cursor + 1, (cursor_end - cursor - 1))));
                    }
                } else {
                    janet_struct_put(event, janet_ckeywordv("dir-name"), path);
                    janet_struct_put(event, janet_ckeywordv("file-name"), name);
                }
                janet_struct_put(event, janet_ckeywordv("cookie"), janet_wrap_integer(inevent.cookie));
                Janet etype = janet_ckeywordv("type");
                const JanetWatchFlagName *wfn_end = watcher_flags_linux + sizeof(watcher_flags_linux) / sizeof(watcher_flags_linux[0]);
                for (const JanetWatchFlagName *wfn = watcher_flags_linux; wfn < wfn_end; wfn++) {
                    if ((inevent.mask & wfn->flag) == wfn->flag) janet_struct_put(event, etype, janet_ckeywordv(wfn->name));
                }
                Janet eventv = janet_wrap_struct(janet_struct_end(event));

                janet_channel_give(watcher->channel, eventv);
            }

            /* Read some more if possible */
            goto read_more;
        }
        break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5c0, %rsp            # imm = 0x5C0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movl -0xc(%rbp), %eax
movq %rax, -0x538(%rbp)
subq $0x6, %rax
ja 0x38334
movq -0x538(%rbp), %rax
leaq 0x51d05(%rip), %rcx     # 0x8a030
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x38b3a
movq -0x20(%rbp), %rdi
movabsq $-0x1000000000000, %rsi # imm = 0xFFFF000000000000
callq 0x7b610
movq %rax, -0x428(%rbp)
movq -0x428(%rbp), %rdi
callq 0x38b90
jmp 0x38b3a
movq -0x8(%rbp), %rax
movq %rax, -0x540(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x7b460
movq -0x540(%rbp), %rdi
movq %rax, -0x430(%rbp)
movq -0x430(%rbp), %rsi
callq 0x2b160
movq -0x8(%rbp), %rdi
callq 0x2a730
jmp 0x38b3a
movq -0x8(%rbp), %rax
movq %rax, -0x548(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x7b460
movq -0x548(%rbp), %rdi
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rsi
callq 0x2b160
movq -0x8(%rbp), %rdi
callq 0x2a730
jmp 0x38b3a
jmp 0x383ea
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x7b460
movq %rax, -0x440(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %edi
leaq -0x420(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x12520
movq %rax, -0x448(%rbp)
xorl %eax, %eax
cmpq $-0x1, -0x448(%rbp)
movb %al, -0x549(%rbp)
jne 0x38441
callq 0x12120
cmpl $0x4, (%rax)
sete %al
movb %al, -0x549(%rbp)
movb -0x549(%rbp), %al
testb $0x1, %al
jne 0x38400
cmpq $-0x1, -0x448(%rbp)
jne 0x384b2
callq 0x12120
cmpl $0xb, (%rax)
je 0x38469
callq 0x12120
cmpl $0xb, (%rax)
jne 0x3846e
jmp 0x38b3a
movq -0x8(%rbp), %rax
movq %rax, -0x558(%rbp)
callq 0x2f7c0
movq -0x558(%rbp), %rdi
movq %rax, -0x450(%rbp)
movq -0x450(%rbp), %rsi
callq 0x2b130
movq -0x8(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x8(%rbp), %rdi
callq 0x2a730
jmp 0x38b3a
cmpq $0x10, -0x448(%rbp)
jge 0x384c1
jmp 0x38b3a
leaq -0x420(%rbp), %rax
movq %rax, -0x458(%rbp)
movq -0x458(%rbp), %rax
leaq -0x420(%rbp), %rcx
addq -0x448(%rbp), %rcx
cmpq %rcx, %rax
jae 0x38b35
movq -0x458(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x468(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x460(%rbp)
movq -0x458(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x458(%rbp)
cmpl $0x0, -0x45c(%rbp)
je 0x38570
movq -0x458(%rbp), %rdi
callq 0x64a70
movq %rax, %rdi
movabsq $-0x6000000000000, %rsi # imm = 0xFFFA000000000000
callq 0x7b550
movq %rax, -0x470(%rbp)
movq -0x470(%rbp), %rax
movq %rax, -0x440(%rbp)
movl -0x45c(%rbp), %ecx
movq -0x458(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x458(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x570(%rbp)
cvtsi2sdl -0x468(%rbp), %xmm0
callq 0x7b820
movq -0x570(%rbp), %rdi
movq %rax, -0x480(%rbp)
movq -0x480(%rbp), %rsi
callq 0x69420
movq %rax, -0x478(%rbp)
movl $0x6, %edi
callq 0x67720
movq %rax, -0x488(%rbp)
movq -0x488(%rbp), %rax
movq %rax, -0x568(%rbp)
leaq 0x5ad79(%rip), %rdi     # 0x9334c
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq %rax, -0x490(%rbp)
cvtsi2sdl -0x468(%rbp), %xmm0
callq 0x7b820
movq -0x568(%rbp), %rdi
movq %rax, -0x498(%rbp)
movq -0x490(%rbp), %rsi
movq -0x498(%rbp), %rdx
callq 0x67d00
movq -0x488(%rbp), %rax
movq %rax, -0x560(%rbp)
leaq 0x52659(%rip), %rdi     # 0x8ac8d
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x560(%rbp), %rdi
movq %rax, -0x4a0(%rbp)
movq -0x4a0(%rbp), %rsi
movq -0x478(%rbp), %rdx
callq 0x67d00
xorl %eax, %eax
testb $0x1, %al
jne 0x38674
jmp 0x386a3
movsd -0x440(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x38689
jmp 0x386c7
movq -0x440(%rbp), %rax
shrq $0x2f, %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x386c7
jmp 0x3891c
movabsq $-0x800000000000, %rax  # imm = 0xFFFF800000000000
andq -0x440(%rbp), %rax
movabsq $-0x7800000000000, %rcx # imm = 0xFFF8800000000000
cmpq %rcx, %rax
jne 0x3891c
movq -0x478(%rbp), %rdi
callq 0x7b260
movq %rax, -0x4a8(%rbp)
movq -0x4a8(%rbp), %rax
movq -0x4a8(%rbp), %rcx
movslq -0x8(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x4b0(%rbp)
movq -0x4b0(%rbp), %rax
movq %rax, -0x4b8(%rbp)
movq -0x4b0(%rbp), %rcx
xorl %eax, %eax
cmpq -0x4a8(%rbp), %rcx
movb %al, -0x571(%rbp)
jbe 0x38732
movq -0x4b0(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0x571(%rbp)
movb -0x571(%rbp), %al
testb $0x1, %al
jne 0x3873e
jmp 0x38752
movq -0x4b0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x4b0(%rbp)
jmp 0x38704
movq -0x4b0(%rbp), %rax
cmpq -0x4a8(%rbp), %rax
jne 0x38805
movq -0x488(%rbp), %rax
movq %rax, -0x588(%rbp)
leaq 0x5251a(%rip), %rdi     # 0x8ac95
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x588(%rbp), %rdi
movq %rax, -0x4c0(%rbp)
movq -0x4c0(%rbp), %rsi
movq -0x478(%rbp), %rdx
callq 0x67d00
movq -0x488(%rbp), %rax
movq %rax, -0x580(%rbp)
leaq 0x524d6(%rip), %rdi     # 0x8ac9e
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x580(%rbp), %rdi
movq %rax, -0x4c8(%rbp)
movq -0x4c8(%rbp), %rsi
movq -0x440(%rbp), %rdx
callq 0x67d00
jmp 0x38917
movq -0x488(%rbp), %rax
movq %rax, -0x598(%rbp)
leaq 0x5247b(%rip), %rdi     # 0x8ac95
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq %rax, -0x4d0(%rbp)
movq -0x4a8(%rbp), %rdi
movq -0x4b0(%rbp), %rax
movq -0x4a8(%rbp), %rcx
subq %rcx, %rax
movl %eax, %esi
callq 0x64880
movq %rax, %rdi
movabsq $-0x6000000000000, %rsi # imm = 0xFFFA000000000000
callq 0x7b550
movq -0x598(%rbp), %rdi
movq %rax, -0x4d8(%rbp)
movq -0x4d0(%rbp), %rsi
movq -0x4d8(%rbp), %rdx
callq 0x67d00
movq -0x488(%rbp), %rax
movq %rax, -0x590(%rbp)
leaq 0x523ff(%rip), %rdi     # 0x8ac9e
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq %rax, -0x4e0(%rbp)
movq -0x4b0(%rbp), %rdi
addq $0x1, %rdi
movq -0x4b8(%rbp), %rax
movq -0x4b0(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %esi
callq 0x64880
movq %rax, %rdi
movabsq $-0x6000000000000, %rsi # imm = 0xFFFA000000000000
callq 0x7b550
movq -0x590(%rbp), %rdi
movq %rax, -0x4e8(%rbp)
movq -0x4e0(%rbp), %rsi
movq -0x4e8(%rbp), %rdx
callq 0x67d00
jmp 0x389b6
movq -0x488(%rbp), %rax
movq %rax, -0x5a8(%rbp)
leaq 0x52364(%rip), %rdi     # 0x8ac95
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x5a8(%rbp), %rdi
movq %rax, -0x4f0(%rbp)
movq -0x4f0(%rbp), %rsi
movq -0x478(%rbp), %rdx
callq 0x67d00
movq -0x488(%rbp), %rax
movq %rax, -0x5a0(%rbp)
leaq 0x52320(%rip), %rdi     # 0x8ac9e
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x5a0(%rbp), %rdi
movq %rax, -0x4f8(%rbp)
movq -0x4f8(%rbp), %rsi
movq -0x440(%rbp), %rdx
callq 0x67d00
movq -0x488(%rbp), %rax
movq %rax, -0x5b0(%rbp)
leaq 0x522dd(%rip), %rdi     # 0x8aca8
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq %rax, -0x500(%rbp)
cvtsi2sdl -0x460(%rbp), %xmm0
callq 0x7b820
movq -0x5b0(%rbp), %rdi
movq %rax, -0x508(%rbp)
movq -0x500(%rbp), %rsi
movq -0x508(%rbp), %rdx
callq 0x67d00
leaq 0x56cf3(%rip), %rdi     # 0x8f711
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq %rax, -0x510(%rbp)
leaq 0x7411d(%rip), %rax     # 0xacb60
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x518(%rbp)
leaq 0x74109(%rip), %rax     # 0xacb60
movq %rax, -0x520(%rbp)
movq -0x520(%rbp), %rax
cmpq -0x518(%rbp), %rax
jae 0x38af7
movl -0x464(%rbp), %eax
movq -0x520(%rbp), %rcx
andl 0x8(%rcx), %eax
movq -0x520(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x38ade
movq -0x488(%rbp), %rax
movq %rax, -0x5b8(%rbp)
movq -0x520(%rbp), %rax
movq (%rax), %rdi
callq 0x68d90
movq %rax, %rdi
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
callq 0x7b550
movq -0x5b8(%rbp), %rdi
movq %rax, -0x528(%rbp)
movq -0x510(%rbp), %rsi
movq -0x528(%rbp), %rdx
callq 0x67d00
jmp 0x38ae0
movq -0x520(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x520(%rbp)
jmp 0x38a5e
movq -0x488(%rbp), %rdi
callq 0x67d30
movq %rax, %rdi
movabsq $-0x3000000000000, %rsi # imm = 0xFFFD000000000000
callq 0x7b550
movq %rax, -0x530(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x530(%rbp), %rsi
callq 0x2c130
jmp 0x384cf
jmp 0x383e8
addq $0x5c0, %rsp            # imm = 0x5C0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)