// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0xf
popq %rbx
pushq $0x64
popq %rax
movl %eax, 0x8(%rsp)
movl $0x5b4, 0xc(%rsp)       # imm = 0x5B4
movq $0x0, 0x10(%rsp)
leaq 0x2b6d(%rip), %r13      # 0x5012
leaq 0x5d24(%rip), %rbp      # 0x81d0
movq 0x5b15(%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 0x24da
movq (%r15), %rdi
callq 0x22d0
movl %eax, %ebx
jmp 0x24b3
cmpl $-0x1, %eax
je 0x2517
cmpl $0x62, %eax
je 0x2500
cmpl $0x64, %eax
je 0x250e
cmpl $0x75, %eax
jne 0x2622
movq (%r15), %rdi
callq 0x22d0
movl %eax, 0xc(%rsp)
jmp 0x24b3
movq (%r15), %rdi
callq 0x22d0
movl %eax, 0x8(%rsp)
jmp 0x24b3
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x24b3
cmpl $0x2, %ebx
pushq $0x1
popq %rbp
cmovll %ebp, %ebx
movl 0xc(%rsp), %r12d
cmpl $0xffe4, %r12d          # imm = 0xFFE4
jae 0x2641
xorl %eax, %eax
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
cmovgl %ecx, %eax
cmpl $0x64, %eax
pushq $0x64
popq %r15
cmovll %eax, %r15d
pushq $0x20
popq %rdi
callq 0x21d0
movq %rax, %r14
movq %rax, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0x3ce0
movl $0x539, %edi            # imm = 0x539
callq 0x28f4
testb %al, %al
je 0x262d
movq 0x5a5e(%rip), %rax      # 0x7fd8
movq (%rax), %r15
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movl %eax, %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x18(%rax)
leaq 0x2ab0(%rip), %rsi      # 0x5048
movq %r15, %rdi
movl %ebp, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x2290
testb $0x1, 0x10(%rsp)
je 0x25be
xorl %edi, %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
jne 0x266b
movq (%r14), %rax
movq %r14, %rdi
callq *0x28(%rax)
leaq 0x18(%rsp), %rdi
callq 0x26e4
leaq 0x40(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x2b64
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0x2f12
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x29b5
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0x40(%rsp), %rdi
callq 0x2cc6
leaq 0x18(%rsp), %rdi
callq 0x21f0
xorl %ebp, %ebp
jmp 0x262d
movq (%r14), %rdi
callq 0x26b0
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5990(%rip), %rax      # 0x7fd8
movq (%rax), %rdi
leaq 0x29c8(%rip), %rsi      # 0x501a
movl %r12d, %edx
movl $0xffe3, %ecx           # imm = 0xFFE3
xorl %eax, %eax
callq 0x2290
movq (%r14), %rdi
callq 0x26b0
jmp 0x262d
leaq 0x2a08(%rip), %rdi      # 0x507a
callq 0x21a0
jmp 0x25be
movq %rax, %rbx
jmp 0x268e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2cc6
leaq 0x18(%rsp), %rdi
callq 0x21f0
jmp 0x26a8
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x2320