// File: 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
movq %rsp, %rbp
subq $0x1330, %rsp           # imm = 0x1330
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x127c0
testb $0x1, 0xf801(%rip)      # 0x21c7d
je 0x1248a
callq 0x12be0
xorl %edi, %edi
callq 0x4480
callq 0x12dd0
leaq -0x30(%rbp), %rdi
callq 0x44f0
movl 0xf7da(%rip), %eax      # 0x21c78
movl %eax, -0x1304(%rbp)
subl $0x64, %eax
je 0x124c5
jmp 0x124ab
movl -0x1304(%rbp), %eax
subl $0x66, %eax
je 0x124ea
jmp 0x124b8
movl -0x1304(%rbp), %eax
subl $0x6a, %eax
je 0x124fe
jmp 0x12512
leaq 0xf7f4(%rip), %rsi      # 0x21cc0
leaq -0x30(%rbp), %rdi
callq 0x4050
jmp 0x124d7
jmp 0x1251c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1279f
leaq 0xf7af(%rip), %rsi      # 0x21ca0
leaq -0x30(%rbp), %rdi
callq 0x4050
jmp 0x124fc
jmp 0x1251c
leaq 0xf77b(%rip), %rsi      # 0x21c80
leaq -0x30(%rbp), %rdi
callq 0x4050
jmp 0x12510
jmp 0x1251c
movl $0x1, %edi
callq 0x4480
testb $0x1, 0xf759(%rip)      # 0x21c7c
je 0x12573
movl $0x1, %edi
xorl %esi, %esi
callq 0x4470
cmpl $-0x1, %eax
jne 0x12571
movq 0xca7b(%rip), %rdi      # 0x1efb8
leaq 0x4568(%rip), %rsi      # 0x16aac
callq 0x43a0
movq %rax, -0x1310(%rbp)
jmp 0x12552
movq -0x1310(%rbp), %rdi
movq 0xca28(%rip), %rsi      # 0x1ef88
callq 0x4400
jmp 0x12567
movl $0x1, %edi
callq 0x4480
jmp 0x12573
leaq -0x1278(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4180
jmp 0x12585
movl 0xf6ed(%rip), %edx      # 0x21c78
leaq -0x1258(%rbp), %rdi
leaq -0x1278(%rbp), %rsi
callq 0x7600
jmp 0x125a0
leaq -0x1278(%rbp), %rdi
callq 0x4728
leaq -0x1258(%rbp), %rdi
callq 0x13280
movl %eax, -0x1314(%rbp)
jmp 0x125c0
movl -0x1314(%rbp), %esi
leaq -0x12c8(%rbp), %rdi
callq 0xd8b0
jmp 0x125d4
callq 0x12be0
jmp 0x125db
callq 0x12e80
jmp 0x125e2
leaq -0x1258(%rbp), %rax
movq %rax, -0x12d8(%rbp)
leaq -0x12c8(%rbp), %rax
movq %rax, -0x12e0(%rbp)
leaq -0x76a5(%rip), %rsi     # 0xaf60
leaq -0x12d0(%rbp), %rdi
leaq -0x12d8(%rbp), %rdx
leaq -0x12e0(%rbp), %rcx
callq 0x132a0
jmp 0x12621
leaq -0x12c8(%rbp), %rax
movq %rax, -0x12f0(%rbp)
movl $0x800, -0x12f4(%rbp)   # imm = 0x800
leaq -0x1258(%rbp), %rdi
callq 0x13370
movw %ax, -0x1316(%rbp)
jmp 0x1264e
movw -0x1316(%rbp), %ax
movw %ax, -0x12f6(%rbp)
leaq -0x1258(%rbp), %rdi
callq 0x13390
movl %eax, -0x131c(%rbp)
jmp 0x12670
movl -0x131c(%rbp), %eax
movl %eax, -0x12fc(%rbp)
leaq -0x1258(%rbp), %rdi
callq 0x133b0
movw %ax, -0x131e(%rbp)
jmp 0x12691
movw -0x131e(%rbp), %ax
movw %ax, -0x12fe(%rbp)
movq %rsp, %rax
leaq -0x12fe(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x42c3(%rip), %rsi     # 0xe3f0
leaq -0x12e8(%rbp), %rdi
leaq -0x12f0(%rbp), %rdx
leaq -0x12f4(%rbp), %rcx
leaq -0x12f6(%rbp), %r8
leaq -0x12fc(%rbp), %r9
callq 0x133d0
jmp 0x126dd
leaq -0x12d0(%rbp), %rdi
callq 0x4650
jmp 0x126eb
leaq -0x12e8(%rbp), %rdi
callq 0x4650
jmp 0x126f9
movl $0x0, -0x4(%rbp)
leaq -0x12e8(%rbp), %rdi
callq 0xbc30
leaq -0x12d0(%rbp), %rdi
callq 0xbc30
leaq -0x1258(%rbp), %rdi
callq 0x8990
leaq -0x30(%rbp), %rdi
callq 0x4728
movl -0x4(%rbp), %eax
addq $0x1330, %rsp           # imm = 0x1330
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1278(%rbp), %rdi
callq 0x4728
jmp 0x1279f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x12793
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x12787
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x12e8(%rbp), %rdi
callq 0xbc30
leaq -0x12d0(%rbp), %rdi
callq 0xbc30
leaq -0x1258(%rbp), %rdi
callq 0x8990
leaq -0x30(%rbp), %rdi
callq 0x4728
movq -0x38(%rbp), %rdi
callq 0x4670
nopw %cs:(%rax,%rax)