// File: /workspace/github_cpp_makefile_star2_O1/hzeller[P]FlaschenTaschen-PixelPusher-bridge/main.cc
// Function: main
int main(int argc, char *argv[]) {
    int layer_timeout = 15;
    bool as_daemon = false;
    int udp_packet_size = kDefaultPacketSize;
    int brightness = 100;

    enum LongOptionsOnly {
        OPT_LAYER_TIMEOUT = 1002,
    };

    static struct option long_options[] = {
        { "daemon",             no_argument,       NULL, 'd'},
        { "layer-timeout",      required_argument, NULL,  OPT_LAYER_TIMEOUT },
        { 0,                    0,                 0,    0  },
    };

    int opt;
    while ((opt = getopt_long(argc, argv, "I:du:b:", long_options, NULL)) != -1) {
        switch (opt) {
        case 'd':
            as_daemon = true;
            break;
        case OPT_LAYER_TIMEOUT:
            layer_timeout = atoi(optarg);
            break;
        case 'u':
            udp_packet_size = atoi(optarg);
            break;
        case 'b':
            brightness = atoi(optarg);
            break;
        default:
            return usage(argv[0]);
        }
    }

    if (layer_timeout < 1) {
        layer_timeout = 1;
    }
    if (brightness < 0) brightness = 0;
    if (brightness > 100) brightness = 100;

    if (udp_packet_size < 0 || udp_packet_size > kMaxUDPPacketSize) {
        fprintf(stderr, "-u %d is outside usable packet size of 0..%d\n",
                udp_packet_size, kMaxUDPPacketSize);
        return usage(argv[0]);
    }

    FlaschenTaschen *display = new BJKPixelPusher(udp_packet_size, brightness);

    // Start all the services and report problems (such as sockets already
    // bound to) before we become a daemon
    if (!udp_server_init(1337)) {
        return 1;
    }

    fprintf(stderr, "Sending result to %dx%d PixelPusher installation\n",
            display->width(), display->height());

    // Commandline parsed, immediate errors reported. Time to become daemon.
    if (as_daemon && daemon(0, 0) != 0) {  // Become daemon.
        perror("Failed to become daemon");
    }

    display->Send();  // Clear screen.

    ft::Mutex mutex;

    // The display we expose to the user provides composite layering which can
    // be used by the UDP server.
    CompositeFlaschenTaschen layered_display(display, 16);
    layered_display.StartLayerGarbageCollection(&mutex, layer_timeout);

    udp_server_run_blocking(&layered_display, &mutex);  // last server blocks.
    delete display;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movl %edi, %r12d
movl $0xf, %ebx
movl $0x64, 0x8(%rsp)
movl $0x5b4, 0xc(%rsp)       # imm = 0x5B4
movq $0x0, 0x10(%rsp)
leaq 0x2a0a(%rip), %r13      # 0x5012
leaq 0x5bc1(%rip), %rbp      # 0x81d0
movq 0x59b2(%rip), %r15      # 0x7fc8
movl %r12d, %edi
movq %r14, %rsi
movq %r13, %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
callq 0x22f0
cmpl $0x3ea, %eax            # imm = 0x3EA
jne 0x263d
movq (%r15), %rdi
callq 0x22d0
movl %eax, %ebx
jmp 0x2616
cmpl $0x63, %eax
jg 0x265e
cmpl $-0x1, %eax
je 0x2683
cmpl $0x62, %eax
jne 0x26f3
movq (%r15), %rdi
callq 0x22d0
movl %eax, 0x8(%rsp)
jmp 0x2616
cmpl $0x64, %eax
je 0x267a
cmpl $0x75, %eax
jne 0x26f3
movq (%r15), %rdi
callq 0x22d0
movl %eax, 0xc(%rsp)
jmp 0x2616
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x2616
cmpl $0x2, %ebx
movl $0x1, %ebp
cmovll %ebp, %ebx
movl 0xc(%rsp), %r15d
cmpl $0xffe4, %r15d          # imm = 0xFFE4
jae 0x2702
xorl %eax, %eax
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
cmpl $0x64, %eax
movl $0x64, %ebp
cmovll %eax, %ebp
movl $0x20, %edi
callq 0x21d0
movq %rax, %r14
movq %rax, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x3dc8
movl $0x539, %edi            # imm = 0x539
callq 0x29a8
movl $0x1, %ebp
testb %al, %al
jne 0x270f
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2446
movl $0x1, %ebp
jmp 0x26df
movl %r15d, %edi
movq %r14, %rsi
callq 0x23ef
jmp 0x26df
movzbl 0x10(%rsp), %esi
movq %r14, %rdi
callq 0x2380
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x22b0
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2c58
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0x30ca
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2a78
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0x40(%rsp), %rdi
callq 0x2e6a
leaq 0x18(%rsp), %rdi
callq 0x21f0
xorl %ebp, %ebp
jmp 0x26df
movq %rax, %rbx
jmp 0x2799
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2e6a
leaq 0x18(%rsp), %rdi
callq 0x21f0
jmp 0x27b5
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x2320
nopl (%rax)