// File: /workspace/github_c_makefile_star2_O1/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 0xf550(%rip), %rax      # 0x12fc4
movl $0x0, (%rax)
leaq 0xeeff(%rip), %rax      # 0x12980
cmpl $0x0, (%rax)
je 0x3ba3
callq 0x32e0
movl %eax, %r14d
testl %eax, %eax
js 0x3be9
movl %r14d, %edi
movq %rbx, %rsi
movl $0x88, %edx
callq 0x3350
testl %eax, %eax
js 0x3bb8
leaq 0xf367(%rip), %r12      # 0x12e20
leaq -0x30(%rbp), %r15
leaq 0xeebc(%rip), %rax      # 0x12980
movl (%rax), %esi
movq %rbx, %rdi
callq 0x3bfa
movl %eax, %edi
callq 0x93a5
movl 0xe829(%rip), %edi      # 0x12304
callq 0x9620
testl %eax, %eax
je 0x3af1
leaq 0xf4d9(%rip), %rax      # 0x12fc4
movl $0x1, (%rax)
cmpb $0x0, (%r12)
je 0x3aff
xorl %edi, %edi
callq 0xa043
movl %r14d, -0x30(%rbp)
movl $0x1, -0x2c(%rbp)
movl $0x1, %esi
movq %r15, %rdi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x33b0
testl %eax, %eax
js 0x3b45
je 0x3b7e
movq %rsp, %r13
leaq -0xd0(%rsp), %rsi
movq %rsi, %rsp
movl $0xc8, %edx
movl %r14d, %edi
callq 0x3210
movq %r13, %rsp
jmp 0x3abd
leaq 0xf46c(%rip), %rax      # 0x12fb8
movq (%rax), %r13
callq 0x3070
movl (%rax), %edi
callq 0x34b0
movq %r13, %rdi
leaq 0x74ea(%rip), %rsi      # 0xb04f
movq %rax, %rdx
xorl %eax, %eax
callq 0x3290
movl $0x1, %edi
callq 0x34c0
jmp 0x3abd
leaq 0xf433(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
movl $0x19, %esi
movl $0x1, %edx
leaq 0x74c9(%rip), %rdi      # 0xb062
callq 0x3490
jmp 0x3abd
movq %rbx, %rdi
xorl %esi, %esi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3bfa
leaq 0xf3f9(%rip), %rax      # 0x12fb8
movq (%rax), %rcx
leaq 0x7474(%rip), %rdi      # 0xb03d
movl $0x11, %esi
movl $0x1, %edx
callq 0x3490
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7440(%rip), %rdi      # 0xb030
callq 0x3430
jmp 0x3a9a