// File: /workspace/github_cpp_makefile_star2_O2/fgheng[P]keysound/src/main.cc
// Function: main
int main(int argc, char *argv[])
{
    process_command_line_arguments(argc, argv);
    // 结束进程		end process
    if (args.kill) {
        kill_exists_process();
        exit(EXIT_SUCCESS);
    }

    signal_handling();
    std::string str;

    switch (args.flag) {
        case 'd':
            str = args.dir;
            break;
        case 'f':
            str = args.wav_file;
            break;
        case 'j':
            str = args.json;
            break;
        default:
            exit(EXIT_FAILURE);
    }

    if (args.daemon) {
        if(daemon(1, 0) == -1) {
            std::cout << "daemon create error" << std::endl;
            exit(EXIT_FAILURE);
        }
    }

    Audio audio(str, args.flag);
    Mixer mixer(audio.get_max_len());

    // 杀掉已经存在的线程	Kill existing threads
    kill_exists_process();
    // 创建当前进程pid的文件	Create a file for the current process pid
    create_pid_file();

    std::thread th1(device_detect, &audio, &mixer);
    std::thread th2(play, &mixer, 2048, audio.get_channels(),
        audio.get_sample_rate(), audio.get_bits_per_sample());

    // 等待子线程结束，避免主线程提前释放资源后子线程访问失败
    // Wait for the end of the sub-thread to avoid the failure of the sub-thread access after the main thread releases resources in advance
    th1.join();
    th2.join();

    // 移除临时文件，这样不可，新进程创建的速度要快于通知旧进程结束的信号的速度
    // 所以如果启动一个新进程，那么旧进程会将新进程的文件删除，那怎么办呢？
    // Remove temporary files. This is not possible. The speed of the new process creation is faster than the speed of the signal notifying the end of the old process
    // So if a new process is started, the old process will delete the files of the new process, what should we do?
    // remove(PID_FILE);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x12e8, %rsp           # imm = 0x12E8
cmpl $0x1, %edi
jle 0xa8d0
movq %rsi, %r12
movl %edi, %ebp
leaq 0x749a(%rip), %rsi      # 0x11be0
leaq 0xd0(%rsp), %r13
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
callq 0x3280
leaq 0x2262(%rip), %r14      # 0xc9c4
leaq 0x2213(%rip), %rbx      # 0xc97c
leaq 0x80(%rsp), %r15
movl %ebp, %edi
movq %r12, %rsi
movq %r14, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x3520
leal -0x64(%rax), %ecx
cmpl $0x8, %ecx
ja 0xa7dc
movslq (%rbx,%rcx,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0x7804(%rip), %rax      # 0x11fa0
movq (%rax), %rsi
movq %r15, %rdi
leaq 0x30(%rsp), %rdx
callq 0x6efa
leaq 0xa485(%rip), %rdi      # 0x14c38
movq %r15, %rsi
callq 0x33c0
movq %r15, %rdi
callq 0x3618
cmpl $0x0, 0xa426(%rip)      # 0x14bf0
jne 0xa8d0
movl $0x64, 0xa416(%rip)     # 0x14bf0
jmp 0xa771
cmpl $0x44, %eax
jne 0xa8b9
movb $0x1, 0xa408(%rip)      # 0x14bf4
jmp 0xa771
movq 0x77ab(%rip), %rax      # 0x11fa0
movq (%rax), %rsi
movq %r15, %rdi
leaq 0x30(%rsp), %rdx
callq 0x6efa
leaq 0xa44c(%rip), %rdi      # 0x14c58
movq %r15, %rsi
callq 0x33c0
movq %r15, %rdi
callq 0x3618
jmp 0xa771
movq 0x7778(%rip), %rax      # 0x11fa0
movq (%rax), %rsi
movq %r15, %rdi
leaq 0x30(%rsp), %rdx
callq 0x6efa
leaq 0xa3d9(%rip), %rdi      # 0x14c18
movq %r15, %rsi
callq 0x33c0
movq %r15, %rdi
callq 0x3618
cmpl $0x0, 0xa39a(%rip)      # 0x14bf0
jne 0xa8d0
movl $0x66, 0xa38e(%rip)     # 0x14bf0
jmp 0xa771
movb $0x1, 0xa387(%rip)      # 0x14bf5
jmp 0xa771
movq 0x7726(%rip), %rax      # 0x11fa0
movq (%rax), %rsi
movq %r15, %rdi
leaq 0x30(%rsp), %rdx
callq 0x6efa
leaq 0xa367(%rip), %rdi      # 0x14bf8
movq %r15, %rsi
callq 0x33c0
movq %r15, %rdi
callq 0x3618
cmpl $0x0, 0xa348(%rip)      # 0x14bf0
jne 0xa8d0
movl $0x6a, 0xa33c(%rip)     # 0x14bf0
jmp 0xa771
cmpl $-0x1, %eax
jne 0xa8d0
cmpl $0x0, 0xa32b(%rip)      # 0x14bf0
jne 0xa90c
cmpb $0x0, 0xa327(%rip)      # 0x14bf5
jne 0xa90c
callq 0xae6d
pushq $0x1
popq %rdi
callq 0x33b0
movq 0x76d4(%rip), %rdi      # 0x11fb8
leaq 0x20e6(%rip), %rsi      # 0xc9d1
callq 0x3310
movslq (%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, (%rbx)
movq (%r12,%rcx,8), %rsi
movq %rax, %rdi
callq 0x3310
movq %rax, %rdi
callq 0x3180
movq 0x76b5(%rip), %rbx      # 0x11fc8
cmpl %ebp, (%rbx)
jl 0xa8dd
cmpb $0x1, 0xa2d7(%rip)      # 0x14bf5
je 0xabad
leaq 0xd8(%rsp), %rbx
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31b0
leaq 0x5c7(%rip), %rax       # 0xaf09
movq %rax, -0x8(%rbx)
pushq $0x1
popq %rdi
leaq 0xd0(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x33d0
pushq $0x2
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x33d0
pushq $0xf
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x33d0
pushq $0x3
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x33d0
movq $0x1, (%rbx)
movl %ebp, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x33d0
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0xa242(%rip), %eax      # 0x14bf0
cmpl $0x6a, %eax
je 0xa9d1
cmpl $0x66, %eax
leaq 0xa25b(%rip), %rsi      # 0x14c18
je 0xa9d8
cmpl $0x64, %eax
jne 0xa8d5
leaq 0xa269(%rip), %rsi      # 0x14c38
jmp 0xa9d8
leaq 0xa220(%rip), %rsi      # 0x14bf8
leaq 0x30(%rsp), %rdi
callq 0x31a0
cmpb $0x1, 0xa20b(%rip)      # 0x14bf4
jne 0xa9fe
pushq $0x1
popq %rdi
xorl %esi, %esi
callq 0x33a0
cmpl $-0x1, %eax
je 0xabec
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x3410
movl 0xa1dd(%rip), %edx      # 0x14bf0
leaq 0xd0(%rsp), %rbx
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x61cc
leaq 0x60(%rsp), %rdi
callq 0x3618
movl 0x12d4(%rsp), %esi
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0x81bc
callq 0xac64
leaq 0x2157(%rip), %rdi      # 0xcbac
movl $0xc1, %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x3230
movl %eax, %ebp
cmpl $-0x1, %eax
je 0xabb9
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
callq 0x3480
leaq 0x1ece(%rip), %rsi      # 0xc957
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x3080
movq %r15, %rdi
callq 0x3150
movl %ebp, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3120
movq %rax, %r15
movl %ebp, %edi
callq 0x35f0
cmpq $-0x1, %r15
jne 0xaae1
leaq 0x20eb(%rip), %rdi      # 0xcbac
callq 0x3590
movq 0x74eb(%rip), %rdi      # 0x11fb8
leaq 0x20ea(%rip), %rsi      # 0xcbbe
callq 0x3310
movq %rax, %rdi
callq 0x3180
leaq 0x10(%rsp), %rdx
movq %rbx, (%rdx)
leaq 0x50(%rsp), %rcx
movq %r14, (%rcx)
leaq -0x35e4(%rip), %rsi     # 0x7514
leaq 0x20(%rsp), %rdi
callq 0xafa6
leaq 0x50(%rsp), %rdx
movq %r14, (%rdx)
leaq 0x5c(%rsp), %rcx
movl $0x800, (%rcx)          # imm = 0x800
movzwl 0x12d8(%rsp), %eax
leaq 0x1e(%rsp), %r8
movw %ax, (%r8)
movl 0x12dc(%rsp), %eax
leaq 0x58(%rsp), %r9
movl %eax, (%r9)
movzwl 0x12e0(%rsp), %eax
leaq 0x1c(%rsp), %rsi
movw %ax, (%rsi)
movq %rsi, (%rsp)
leaq -0x2510(%rip), %rsi     # 0x8640
leaq 0x10(%rsp), %rdi
callq 0xb028
leaq 0x20(%rsp), %rdi
callq 0x3550
leaq 0x10(%rsp), %rdi
callq 0x3550
leaq 0x10(%rsp), %rdi
callq 0x7bba
leaq 0x20(%rsp), %rdi
callq 0x7bba
leaq 0xd0(%rsp), %rdi
callq 0x6bec
leaq 0x30(%rsp), %rdi
callq 0x3618
xorl %eax, %eax
addq $0x12e8, %rsp           # imm = 0x12E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xac64
xorl %edi, %edi
callq 0x33b0
callq 0x3050
cmpl $0x11, (%rax)
leaq 0x200b(%rip), %rax      # 0xcbd3
leaq 0x201f(%rip), %rsi      # 0xcbee
cmoveq %rax, %rsi
movq 0x73de(%rip), %rdi      # 0x11fb8
callq 0x3310
movq %rax, %rdi
callq 0x3180
jmp 0xa8d5
movq 0x73c5(%rip), %rdi      # 0x11fb8
leaq 0x1da6(%rip), %rsi      # 0xc9a0
callq 0x3310
movq %rax, %rdi
callq 0x3180
jmp 0xa8d5
movq %rax, %rbx
jmp 0xac36
jmp 0xac42
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3618
jmp 0xac52
jmp 0xac24
movq %rax, %rbx
jmp 0xac52
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7bba
leaq 0x20(%rsp), %rdi
callq 0x7bba
jmp 0xac45
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x6bec
leaq 0x30(%rsp), %rdi
callq 0x3618
movq %rbx, %rdi
callq 0x3570