// File: /workspace/github_c_makefile_star2_O3/Matthias-Wandel[P]imgcomp/src/main.c
// Function: DoDirectory
int DoDirectory(char * Directory)
{
    int a;
    Raspistill_restarted = 0;

    if (!FollowDir){
        // Offline mode - just a one shot, no polling.
        return DoDirectoryFunc(Directory, FollowDir);
    }

    int wd, fd;
    fd = inotify_init();
    if (fd < 0) perror("inotify_init");

    // raspistill first writes under a temporary file name, ending with "~" then renames.
    // IN_MOVED_TO triggers when the file is renamed to the final file name.
    // But when using ffmpeg for the RTSP camera hack, ffmpeg writes the files under
    // the original name, so IN_CLOSE_WRITE is needed for that.
    wd = inotify_add_watch( fd, Directory, IN_CLOSE_WRITE | IN_MOVED_TO);
    if (wd < 0){
        fprintf(Log, "add watch failed\n");
        return 0;
    }

    for (;;){
        a = DoDirectoryFunc(Directory, FollowDir);
        DoMotionRun(a);

        int b = manage_camera_prog(NumProcessed);
        if (b) Raspistill_restarted = 1;
        if (LogToFile[0] != '\0') LogFileMaintain(0);

        // Wait for more files to appear.
        struct pollfd pfd = { fd, POLLIN, 0 };
        int ret = poll(&pfd, 1, 2000);
        if (ret < 0) {
            fprintf(Log, "select failed: %s\n", strerror(errno));
            sleep(1);
            continue;
        }
        if (ret == 0){
            // Timeout waiting for a new file.
            fprintf(Log, "wait file poll() timeout\n");
            continue;
        }

        // Read and ignore the event to clear it out.
        const int EVENT_BUF_LEN = 200;
        char buffer[EVENT_BUF_LEN];
        read( fd, buffer, EVENT_BUF_LEN );
    }

    return a;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0xf540(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
leaq 0xeeef(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x3bb3
callq 0x32f0
movl %eax, %r14d
testl %eax, %eax
js 0x3bf9
movl %r14d, %edi
movq %rbx, %rsi
movl $0x88, %edx
callq 0x3360
testl %eax, %eax
js 0x3bc8
leaq 0xf357(%rip), %r12      # 0x12e20
leaq -0x30(%rbp), %r15
leaq 0xeeac(%rip), %rax      # 0x12980
movl (%rax), %esi
movq %rbx, %rdi
callq 0x3c0a
movl %eax, %edi
callq 0x9399
movl 0xe819(%rip), %edi      # 0x12304
callq 0x960f
testl %eax, %eax
je 0x3b01
leaq 0xf4c9(%rip), %rax      # 0x12fc4
movl $0x1, (%rax)
cmpb $0x0, (%r12)
je 0x3b0f
xorl %edi, %edi
callq 0xa03e
movl %r14d, -0x30(%rbp)
movl $0x1, -0x2c(%rbp)
movl $0x1, %esi
movq %r15, %rdi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x33c0
testl %eax, %eax
js 0x3b55
je 0x3b8e
movq %rsp, %r13
leaq -0xd0(%rsp), %rsi
movq %rsi, %rsp
movl $0xc8, %edx
movl %r14d, %edi
callq 0x3210
movq %r13, %rsp
jmp 0x3acd
leaq 0xf45c(%rip), %rax      # 0x12fb8
movq (%rax), %r13
callq 0x3070
movl (%rax), %edi
callq 0x34c0
movq %r13, %rdi
leaq 0x74ea(%rip), %rsi      # 0xb05f
movq %rax, %rdx
xorl %eax, %eax
callq 0x32a0
movl $0x1, %edi
callq 0x34d0
jmp 0x3acd
leaq 0xf423(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x19, %esi
movl $0x1, %edx
leaq 0x74c9(%rip), %rdi      # 0xb072
callq 0x34a0
jmp 0x3acd
movq %rbx, %rdi
xorl %esi, %esi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3c0a
leaq 0xf3e9(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x7474(%rip), %rdi      # 0xb04d
movl $0x11, %esi
movl $0x1, %edx
callq 0x34a0
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7440(%rip), %rdi      # 0xb040
callq 0x3440
jmp 0x3aaa