// File: /workspace/github_c_makefile_star2_O2/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
leaq 0xf983(%rip), %rax      # 0x12fd4
andl $0x0, (%rax)
movq %rdi, %rbx
leaq 0xf332(%rip), %rax      # 0x12990
cmpl $0x0, (%rax)
je 0x377a
callq 0x32f0
movl %eax, %r14d
testl %eax, %eax
js 0x37bc
movl %r14d, %edi
movq %rbx, %rsi
movl $0x88, %edx
callq 0x3360
testl %eax, %eax
js 0x378f
leaq -0x30(%rbp), %r15
pushq $0x1
popq %r12
leaq 0xf2f2(%rip), %rax      # 0x12990
movl (%rax), %esi
movq %rbx, %rdi
callq 0x37cd
movl %eax, %edi
callq 0x8abb
movl 0xec5f(%rip), %edi      # 0x12314
callq 0x8d04
testl %eax, %eax
je 0x36cb
leaq 0xf90f(%rip), %rax      # 0x12fd4
movl $0x1, (%rax)
leaq 0xf75e(%rip), %rax      # 0x12e30
cmpb $0x0, (%rax)
je 0x36de
xorl %edi, %edi
callq 0x96a4
movl %r14d, -0x30(%rbp)
movl $0x1, -0x2c(%rbp)
movq %r15, %rdi
movq %r12, %rsi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x33d0
testl %eax, %eax
js 0x3722
je 0x3759
movq %rsp, %r13
leaq -0xd0(%rsp), %rsi
movq %rsi, %rsp
movl $0xc8, %edx
movl %r14d, %edi
callq 0x3210
movq %r13, %rsp
jmp 0x3697
leaq 0xf89f(%rip), %rax      # 0x12fc8
movq (%rax), %r13
callq 0x3070
movl (%rax), %edi
callq 0x34d0
movq %r13, %rdi
leaq 0x791d(%rip), %rsi      # 0xb05f
movq %rax, %rdx
xorl %eax, %eax
callq 0x32a0
pushq $0x1
popq %rdi
callq 0x34e0
jmp 0x3697
leaq 0xf868(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x7908(%rip), %rdi      # 0xb072
pushq $0x19
popq %rsi
movq %r12, %rdx
callq 0x34b0
jmp 0x3697
movq %rbx, %rdi
xorl %esi, %esi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x37cd
leaq 0xf832(%rip), %rax      # 0x12fc8
movq (%rax), %rcx
leaq 0x78ad(%rip), %rdi      # 0xb04d
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x34b0
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x787d(%rip), %rdi      # 0xb040
callq 0x3450
jmp 0x3677