// File: /workspace/github_cpp_makefile_star2_O2/intheswim[P]FractalTubes/main.cpp
// Function: main
int main (int argc, char *argv[]) {

    Display *dpy;
    Window  win;

    struct argvOptions params;

    parse_arguments (argc, argv, &params); 

    dpy = XOpenDisplay (getenv ("DISPLAY"));

    if (!dpy) 
    {
        fprintf(stderr, "Cannot connect to X server.\n");
        return EXIT_FAILURE;
    }

    char *xwin = getenv ("XSCREENSAVER_WINDOW");

    if (params.useLog)
    {
        printf ("Display : %s\n", getenv("DISPLAY"));

        printf ("xwin : %s\n", xwin);
    }

    int root_window_id = 0;

    if (xwin)
    {
        root_window_id = strtol (xwin, NULL, 0);
    }

    XVisualInfo *vi = NULL;

    if (params.standalone == false) 
    {
		// Get the root window
        if (root_window_id == 0)
        {
		    // root = DefaultRootWindow(dpy);
            printf ("usage as standalone app: %s -window\n", argv[0]);
            return EXIT_FAILURE;
        }
        else
    	{
      		win = root_window_id;

            int screen = DefaultScreen(dpy);  

            if (params.smooth)
            {
                vi = getMultispampleXVisual (dpy, screen);
            }

            if (vi == NULL)
            {
                vi = glXChooseVisual(dpy, screen, att);
            }

            if (vi == NULL) 
            {
	            fprintf(stderr, "\nNo appropriate visual found\n\n");
                return EXIT_FAILURE;
            }

            XSelectInput (dpy, win, ExposureMask | StructureNotifyMask);
    	}
	} 
    else 
	{
		/* Let's create our own window. */
		int screen = DefaultScreen(dpy);
        Window root = DefaultRootWindow(dpy);

        if (params.smooth)
        {
            vi = getMultispampleXVisual (dpy, screen);
        }

        if (vi == NULL)
        {
            vi = glXChooseVisual(dpy, screen, att);
        }

        if (vi == NULL) 
        {
	        fprintf(stderr, "\nNo appropriate visual found\n\n");
            return EXIT_FAILURE;
        } 

        unsigned int mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;

        int border_width = 0;

        Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);

        XSetWindowAttributes swa;

        swa.background_pixmap = None ;
        swa.background_pixel  = 0    ;
        swa.border_pixel      = 0    ;

        swa.colormap = cmap;
        swa.event_mask = ExposureMask | StructureNotifyMask;

		win = XCreateWindow(dpy, root, 0, 0, 860, 640, border_width,
            vi->depth, InputOutput, vi->visual, mask, &swa);

		XMapWindow(dpy, win);

		XStoreName(dpy, win, "Fractal Tubes");
	}

    // this is to terminate nicely:
	Atom wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(dpy, win, &wmDeleteMessage, 1);

    if (params.useLog) { createLog(); }

    GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);

    if (!glc)
    {
        fprintf(stderr, "\nglXCreateContext failed.\n\n");
        return EXIT_FAILURE;
    }

    if (!glXMakeCurrent(dpy, win, glc))
    {
        fprintf(stderr, "\nglXMakeCurren failed.\n\n");
        return EXIT_FAILURE;
    }

    if (params.verbose)
    {
        fprintf (stdout, "OpenGL Version: %s\n", glGetString(GL_VERSION));
    }
 
    glEnable (GL_DEPTH_TEST); 

    XWindowAttributes gwa;

    XGetWindowAttributes(dpy, win, &gwa);

    AppParams::setDetailLevel (params.detail);
    AppParams::setIntervalOption (params.interval);
    AppParams::setPalette (params.palette);
    AppParams::setLightSource (params.light_source);
    AppParams::setTransparencyLevel (params.transparency);
    AppParams::setRotationSpeed (params.rotation_speed);
    AppParams::setLightDistance (params.dist2light);
    AppParams::setLight2Sides (params.two_sided_light);
    AppParams::setShuffle (params.shuffle);

    if (params.gloss_level < 0) // random 
    {
        AppParams::setGlossRandom(true);
    }
    else 
    {
        AppParams::setGlossRandom(false);
        AppParams::setGlossLevel (params.gloss_level);
    }

    GetConfig();

    setFrameSizeForGL (gwa.width, gwa.height);
    
    prepareOpenGL();
 
    while (true) 
    {
        XEvent  event;

        long mask = StructureNotifyMask;

        if (XCheckWindowEvent(dpy, win, mask, &event) ||
		   XCheckTypedWindowEvent (dpy, win, ClientMessage, &event)) // needed to catch ClientMessage
        {
            if (event.type == ConfigureNotify) 
        	{
          		XConfigureEvent xce = event.xconfigure;

		        // This event type is generated for a variety of
          		// happenings, so check whether the window has been
          		// resized.

          		if (xce.width != gwa.width || xce.height != gwa.height) 
          		{
            		gwa.width = xce.width;
            		gwa.height = xce.height;

                    setFrameSizeForGL (gwa.width, gwa.height);
          
            		continue;
          		}
        	}

            else if (event.type == ClientMessage)
        	{
            	if ((Atom)event.xclient.data.l[0] == wmDeleteMessage)
            	{
                	break;
            	}
			}                
        }

        animateOneFrame();

        redrawMethod();
        glXSwapBuffers(dpy, win);

        // sleep for 10 ms
        usleep(1000 * params.delay_ms);
    } 

    CleanupAnimation();

    if (params.useLog) { closeLog (); }

    glXMakeCurrent(dpy, None, NULL);
 	glXDestroyContext(dpy, glc);
 	XDestroyWindow(dpy, win);
 	XCloseDisplay(dpy);

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %r13
movslq %edi, %r14
pushq $-0x1
popq %rdx
pushq $0x1
popq %r12
movb $0x1, %al
movl %eax, 0x48(%rsp)
pushq $0x2
popq %rcx
pushq $0x1
popq %rsi
pushq $0xa
popq %rax
movl $0x0, 0x18(%rsp)
leaq 0x5969(%rip), %rbx      # 0x919a
movl %esi, 0x38(%rsp)
movl %ecx, 0xc(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x0, 0x3c(%rsp)
movl %esi, 0x40(%rsp)
movl %eax, 0x34(%rsp)
movl %eax, 0x44(%rsp)
movl %esi, 0x1c(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x5c(%rsp)
movb $0x1, %bpl
movl %edx, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movl %edx, 0x4c(%rsp)
movl %edx, 0x50(%rsp)
movl %edx, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movl %edx, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movl %edx, 0x20(%rsp)
cmpq %r14, %r12
jge 0x3bff
movq (%r13,%r12,8), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3330
testl %eax, %eax
je 0x3a0d
movq %r15, %rdi
leaq 0x58e9(%rip), %rsi      # 0x91a0
callq 0x3330
testl %eax, %eax
je 0x3a14
movq %r15, %rdi
leaq 0x58db(%rip), %rsi      # 0x91a9
callq 0x3330
testl %eax, %eax
je 0x3a1f
movq %r15, %rdi
leaq 0x58c9(%rip), %rsi      # 0x91ae
callq 0x3480
testl %eax, %eax
je 0x3a2a
movq %r15, %rdi
leaq 0x58b9(%rip), %rsi      # 0x91b5
callq 0x3480
testl %eax, %eax
je 0x3a38
movq %r15, %rdi
leaq 0x58aa(%rip), %rsi      # 0x91bd
callq 0x3480
testl %eax, %eax
je 0x3a46
movq %r15, %rdi
leaq 0x589d(%rip), %rsi      # 0x91c7
callq 0x3480
testl %eax, %eax
je 0x3a54
movq %r15, %rdi
leaq 0x588f(%rip), %rsi      # 0x91d0
callq 0x3480
testl %eax, %eax
je 0x3a62
movq %r15, %rdi
leaq 0x587f(%rip), %rsi      # 0x91d7
callq 0x3480
testl %eax, %eax
je 0x3a70
movq %r15, %rdi
leaq 0x5876(%rip), %rsi      # 0x91e5
callq 0x3480
testl %eax, %eax
je 0x3a7e
movq %r15, %rdi
leaq 0x5866(%rip), %rsi      # 0x91ec
callq 0x3480
testl %eax, %eax
je 0x3a8c
movq %r15, %rdi
leaq 0x585e(%rip), %rsi      # 0x91fb
callq 0x3480
testl %eax, %eax
je 0x3a9a
movq %r15, %rdi
leaq 0x584e(%rip), %rsi      # 0x9202
callq 0x3480
testl %eax, %eax
je 0x3aa8
movq %r15, %rdi
leaq 0x5841(%rip), %rsi      # 0x920c
callq 0x3480
testl %eax, %eax
je 0x3bf7
movq %r15, %rdi
leaq 0x5833(%rip), %rsi      # 0x9215
callq 0x3480
testl %eax, %eax
je 0x3ab5
movl 0x58(%rsp), %eax
cmpq %rax, %r12
jne 0x3ac2
movq %r15, %rdi
callq 0x3550
movl %eax, 0x44(%rsp)
jmp 0x3bf7
xorl %ebp, %ebp
jmp 0x3bf7
movb $0x1, %al
movl %eax, 0x5c(%rsp)
jmp 0x3bf7
movb $0x1, %al
movl %eax, 0x14(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x58(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x54(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x50(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x4c(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x30(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x2c(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x28(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x24(%rsp)
jmp 0x3bf7
leal 0x1(%r12), %eax
movl %eax, 0x20(%rsp)
jmp 0x3bf7
movl $0x0, 0x48(%rsp)
jmp 0x3bf7
movl $0x0, 0x1c(%rsp)
jmp 0x3bf7
movl 0x54(%rsp), %eax
cmpq %rax, %r12
jne 0x3adc
movq %r15, %rdi
callq 0x3550
movl %eax, 0x40(%rsp)
jmp 0x3bf7
movl 0x50(%rsp), %eax
cmpq %rax, %r12
jne 0x3af6
movq %r15, %rdi
callq 0x3550
movl %eax, 0x3c(%rsp)
jmp 0x3bf7
movl 0x2c(%rsp), %eax
cmpq %rax, %r12
jne 0x3b10
movq %r15, %rdi
callq 0x3550
movl %eax, 0x38(%rsp)
jmp 0x3bf7
movl 0x28(%rsp), %eax
cmpq %rax, %r12
je 0x3bf7
movl 0x24(%rsp), %eax
cmpq %rax, %r12
jne 0x3b37
movq %r15, %rdi
callq 0x3550
movl %eax, 0x34(%rsp)
jmp 0x3bf7
movl 0x20(%rsp), %eax
cmpq %rax, %r12
jne 0x3b5f
movq %r15, %rdi
leaq 0x5af3(%rip), %rsi      # 0x963d
callq 0x3480
testl %eax, %eax
je 0x3beb
pushq $-0x1
popq %rax
jmp 0x3bf3
movl 0x4c(%rsp), %eax
cmpq %rax, %r12
jne 0x3ba5
movq %r15, %rdi
leaq 0x56ad(%rip), %rsi      # 0x921f
callq 0x3480
testl %eax, %eax
pushq $0x1
popq %rax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq %r15, %rdi
leaq 0x5692(%rip), %rsi      # 0x9223
callq 0x3480
testl %eax, %eax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x3bf7
movl 0x30(%rsp), %eax
cmpq %rax, %r12
jne 0x3bf7
movq %r15, %rdi
leaq 0x566f(%rip), %rsi      # 0x9227
callq 0x3480
testl %eax, %eax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
movq %r15, %rdi
leaq 0x5659(%rip), %rsi      # 0x922d
callq 0x3480
testl %eax, %eax
pushq $0x1
popq %rax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
jmp 0x3bf7
movq %r15, %rdi
callq 0x3550
movl %eax, 0x18(%rsp)
incq %r12
jmp 0x388c
leaq 0x5496(%rip), %rdi      # 0x909c
callq 0x33b0
movq %rax, %rdi
callq 0x3470
testq %rax, %rax
je 0x3feb
movq %rax, %rbx
leaq 0x549b(%rip), %rdi      # 0x90c1
callq 0x33b0
movq %rax, %r14
testb $0x1, 0x14(%rsp)
je 0x3c63
leaq 0x5460(%rip), %rdi      # 0x909c
callq 0x33b0
leaq 0x548d(%rip), %rdi      # 0x90d5
movq %rax, %rsi
xorl %eax, %eax
callq 0x3040
leaq 0x548a(%rip), %rdi      # 0x90e3
movq %r14, %rsi
xorl %eax, %eax
callq 0x3040
testq %r14, %r14
je 0x3cd6
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3490
testb %bpl, %bpl
jne 0x3cdf
movq %rax, %r14
testl %r14d, %r14d
je 0x3fd7
movl 0xe0(%rbx), %ebp
cmpl $0x0, 0x1c(%rsp)
je 0x3ca1
movq %rbx, %rdi
movl %ebp, %esi
callq 0x376c
testq %rax, %rax
jne 0x3cbb
leaq 0x96a8(%rip), %rdx      # 0xd350
movq %rbx, %rdi
movl %ebp, %esi
callq 0x34e0
testq %rax, %rax
je 0x4033
movq %rax, %r15
movslq %r14d, %r14
movl $0x28000, %edx          # imm = 0x28000
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31c0
jmp 0x3db0
testb %bpl, %bpl
je 0x3fd7
movslq 0xe0(%rbx), %r12
movq 0xe8(%rbx), %rax
movq %r12, %rcx
shlq $0x7, %rcx
movq 0x10(%rax,%rcx), %r14
cmpl $0x0, 0x1c(%rsp)
je 0x3d10
movq %rbx, %rdi
movl %r12d, %esi
callq 0x376c
testq %rax, %rax
jne 0x3d2b
leaq 0x9639(%rip), %rdx      # 0xd350
movq %rbx, %rdi
movl %r12d, %esi
callq 0x34e0
testq %rax, %rax
je 0x4033
movq %rax, %r15
movq (%rax), %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x31d0
leaq 0xf0(%rsp), %r10
andq $0x0, 0x18(%r10)
xorps %xmm0, %xmm0
movaps %xmm0, (%r10)
movq %rax, 0x60(%r10)
movq $0x28000, 0x48(%r10)    # imm = 0x28000
movl 0x14(%r15), %eax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x35c, %r8d            # imm = 0x35C
movl $0x280, %r9d            # imm = 0x280
pushq %r10
pushq $0x2809                 # imm = 0x2809
pushq (%r15)
pushq $0x1
pushq %rax
pushq $0x0
callq 0x3540
addq $0x30, %rsp
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3340
leaq 0x538d(%rip), %rdx      # 0x9132
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30a0
leaq 0x5389(%rip), %rsi      # 0x9140
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3240
leaq 0x60(%rsp), %rdx
movq %rax, (%rdx)
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31a0
testb $0x1, 0x14(%rsp)
je 0x3de3
callq 0x4068
pushq $0x1
popq %r12
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x33c0
testq %rax, %rax
je 0x4000
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x3270
testl %eax, %eax
je 0x4015
testb $0x1, 0x5c(%rsp)
je 0x3e48
movq 0x91a1(%rip), %rax      # 0xcfc8
movq (%rax), %r12
movl $0x1f02, %edi           # imm = 0x1F02
callq 0x31b0
leaq 0x534b(%rip), %rsi      # 0x9186
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x34c0
movl $0xb71, %edi            # imm = 0xB71
callq 0x32c0
leaq 0x68(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x35e0
movl 0x40(%rsp), %edi
callq 0x7872
movl 0x3c(%rsp), %edi
callq 0x7a2e
movl 0x10(%rsp), %edi
callq 0x79b2
movl 0xc(%rsp), %edi
callq 0x78da
movl 0x38(%rsp), %edi
callq 0x77c8
pushq $0x19
popq %rdi
callq 0x7828
cvtsi2ssl 0x34(%rsp), %xmm0
callq 0x7942
movzbl 0x48(%rsp), %edi
andl $0x1, %edi
callq 0x790e
xorl %edi, %edi
callq 0x7b1e
movl 0x18(%rsp), %ebp
testl %ebp, %ebp
js 0x3ece
xorl %edi, %edi
callq 0x7a8c
movl %ebp, %edi
callq 0x7abe
jmp 0x3ed6
pushq $0x1
popq %rdi
callq 0x7a8c
callq 0x5209
movl 0x70(%rsp), %edi
movl 0x74(%rsp), %esi
callq 0x40d7
callq 0x4139
imull $0x3e8, 0x44(%rsp), %ebp # imm = 0x3E8
leaq 0xf0(%rsp), %r12
pushq $0x21
popq %r13
movl $0x20000, %edx          # imm = 0x20000
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x3210
testl %eax, %eax
jne 0x3f2d
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
movq %r12, %rcx
callq 0x3440
testl %eax, %eax
je 0x3f76
movl 0xf0(%rsp), %eax
cmpl $0x21, %eax
je 0x3f67
cmpl $0x16, %eax
jne 0x3f76
movl 0x128(%rsp), %edi
movl 0x12c(%rsp), %esi
cmpl 0x70(%rsp), %edi
jne 0x3f58
cmpl 0x74(%rsp), %esi
je 0x3f76
movl %edi, 0x70(%rsp)
movl %esi, 0x74(%rsp)
callq 0x40d7
jmp 0x3f01
movq 0x128(%rsp), %rax
cmpq 0x60(%rsp), %rax
je 0x3f97
callq 0x4fe8
callq 0x4220
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3610
movl %ebp, %edi
callq 0x34b0
jmp 0x3f01
callq 0x5204
testb $0x1, 0x14(%rsp)
je 0x3fa8
callq 0x4085
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3270
movq %rbx, %rdi
movq %r15, %rsi
callq 0x31f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3560
movq %rbx, %rdi
callq 0x3640
jmp 0x4053
movq (%r13), %rsi
leaq 0x510c(%rip), %rdi      # 0x90ee
xorl %eax, %eax
callq 0x3040
jmp 0x404f
movq 0x8fee(%rip), %rax      # 0xcfe0
movq (%rax), %rcx
leaq 0x50a8(%rip), %rdi      # 0x90a4
pushq $0x1c
jmp 0x4046
movq 0x8fd9(%rip), %rax      # 0xcfe0
movq (%rax), %rcx
leaq 0x5140(%rip), %rdi      # 0x9151
pushq $0x1b
jmp 0x4028
movq 0x8fc4(%rip), %rax      # 0xcfe0
movq (%rax), %rcx
leaq 0x5147(%rip), %rdi      # 0x916d
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x3650
jmp 0x4053
movq 0x8fa6(%rip), %rax      # 0xcfe0
movq (%rax), %rcx
leaq 0x50cf(%rip), %rdi      # 0x9113
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x3650
pushq $0x1
popq %r12
movl %r12d, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq