// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
cmpl $0x2, %edi
jl 0x3d1d
movl %edi, %r13d
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
movl $0x1, %r12d
movb $0x1, %al
movl %eax, 0x2c(%rsp)
movl $0x2, 0xc(%rsp)
movl $0xa, 0x4c(%rsp)
movl $0x0, 0x1c(%rsp)
leaq 0x589c(%rip), %r14      # 0x919a
movl $0x1, 0x20(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x1, 0x28(%rsp)
movl $0xa, 0x50(%rsp)
movl $0x1, 0x54(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x30(%rsp)
movb $0x1, %r15b
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x40(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x58(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x48(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x5c(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movq (%rbp,%r12,8), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3320
testl %eax, %eax
je 0x3af9
movq %rbx, %rdi
leaq 0x57fd(%rip), %rsi      # 0x91a0
callq 0x3320
testl %eax, %eax
je 0x3b01
movq %rbx, %rdi
leaq 0x57ef(%rip), %rsi      # 0x91a9
callq 0x3320
testl %eax, %eax
je 0x3b0c
movq %rbx, %rdi
leaq 0x57dd(%rip), %rsi      # 0x91ae
callq 0x3460
testl %eax, %eax
je 0x3b17
movq %rbx, %rdi
leaq 0x57cd(%rip), %rsi      # 0x91b5
callq 0x3460
testl %eax, %eax
je 0x3b25
movq %rbx, %rdi
leaq 0x57be(%rip), %rsi      # 0x91bd
callq 0x3460
testl %eax, %eax
je 0x3b33
movq %rbx, %rdi
leaq 0x57b1(%rip), %rsi      # 0x91c7
callq 0x3460
testl %eax, %eax
je 0x3b41
movq %rbx, %rdi
leaq 0x57a3(%rip), %rsi      # 0x91d0
callq 0x3460
testl %eax, %eax
je 0x3b4f
movq %rbx, %rdi
leaq 0x5793(%rip), %rsi      # 0x91d7
callq 0x3460
testl %eax, %eax
je 0x3b5d
movq %rbx, %rdi
leaq 0x578a(%rip), %rsi      # 0x91e5
callq 0x3460
testl %eax, %eax
je 0x3b6b
movq %rbx, %rdi
leaq 0x577a(%rip), %rsi      # 0x91ec
callq 0x3460
testl %eax, %eax
je 0x3b79
movq %rbx, %rdi
leaq 0x5772(%rip), %rsi      # 0x91fb
callq 0x3460
testl %eax, %eax
je 0x3b87
movq %rbx, %rdi
leaq 0x5762(%rip), %rsi      # 0x9202
callq 0x3460
testl %eax, %eax
je 0x3b95
movq %rbx, %rdi
leaq 0x5755(%rip), %rsi      # 0x920c
callq 0x3460
testl %eax, %eax
je 0x3ceb
movq %rbx, %rdi
leaq 0x5747(%rip), %rsi      # 0x9215
callq 0x3460
testl %eax, %eax
je 0x3ba2
movl 0x14(%rsp), %eax
cmpq %rax, %r12
jne 0x3baf
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x50(%rsp)
jmp 0x3ceb
xorl %r15d, %r15d
jmp 0x3ceb
movb $0x1, %al
movl %eax, 0x30(%rsp)
jmp 0x3ceb
movb $0x1, %al
movl %eax, 0x18(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x14(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x5c(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x58(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x48(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x44(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x40(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x3c(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x38(%rsp)
jmp 0x3ceb
leal 0x1(%r12), %eax
movl %eax, 0x34(%rsp)
jmp 0x3ceb
movl $0x0, 0x2c(%rsp)
jmp 0x3ceb
movl $0x0, 0x54(%rsp)
jmp 0x3ceb
movl 0x5c(%rsp), %eax
cmpq %rax, %r12
jne 0x3bc9
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x28(%rsp)
jmp 0x3ceb
movl 0x58(%rsp), %eax
cmpq %rax, %r12
jne 0x3be3
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x24(%rsp)
jmp 0x3ceb
movl 0x40(%rsp), %eax
cmpq %rax, %r12
jne 0x3bfd
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x20(%rsp)
jmp 0x3ceb
movl 0x3c(%rsp), %eax
cmpq %rax, %r12
je 0x3ceb
movl 0x38(%rsp), %eax
cmpq %rax, %r12
jne 0x3c24
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x4c(%rsp)
jmp 0x3ceb
movl 0x34(%rsp), %eax
cmpq %rax, %r12
jne 0x3c5d
movq %rbx, %rdi
leaq 0x5a2e(%rip), %rsi      # 0x9665
callq 0x3460
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x3ceb
movq %rbx, %rdi
callq 0x3530
movl %eax, 0x1c(%rsp)
jmp 0x3ceb
movl 0x48(%rsp), %eax
cmpq %rax, %r12
jne 0x3ca5
movq %rbx, %rdi
leaq 0x55af(%rip), %rsi      # 0x921f
callq 0x3460
testl %eax, %eax
movl $0x1, %eax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x5592(%rip), %rsi      # 0x9223
callq 0x3460
testl %eax, %eax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x3ceb
movl 0x44(%rsp), %eax
cmpq %rax, %r12
jne 0x3ceb
movq %rbx, %rdi
leaq 0x556f(%rip), %rsi      # 0x9227
callq 0x3460
testl %eax, %eax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
movq %rbx, %rdi
leaq 0x5559(%rip), %rsi      # 0x922d
callq 0x3460
testl %eax, %eax
movl $0x1, %eax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
incq %r12
cmpq %r12, %r13
jne 0x3981
testb %r15b, %r15b
sete %r13b
cmpl $0x0, 0x54(%rsp)
sete %r12b
cvtsi2ssl 0x4c(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
imull $0x3e8, 0x50(%rsp), %ebx # imm = 0x3E8
jmp 0x3d7c
movl $0x2710, %ebx           # imm = 0x2710
movl $0x2, 0xc(%rsp)
movl $0x1, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x2c(%rsp)
movss 0x52c4(%rip), %xmm0     # 0x9004
movss %xmm0, 0x14(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x1, 0x28(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x18(%rsp)
movl $0x0, 0x30(%rsp)
xorl %r13d, %r13d
leaq 0x5319(%rip), %rdi      # 0x909c
callq 0x33a0
movq %rax, %rdi
callq 0x3450
testq %rax, %rax
je 0x417e
movq %rax, %r14
leaq 0x531e(%rip), %rdi      # 0x90c1
callq 0x33a0
movq %rax, %r15
testb $0x1, 0x18(%rsp)
je 0x3de0
leaq 0x52e3(%rip), %rdi      # 0x909c
callq 0x33a0
leaq 0x5310(%rip), %rdi      # 0x90d5
movq %rax, %rsi
xorl %eax, %eax
callq 0x3040
leaq 0x530d(%rip), %rdi      # 0x90e3
movq %r15, %rsi
xorl %eax, %eax
callq 0x3040
testq %r15, %r15
je 0x3e4e
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3470
testb %r13b, %r13b
je 0x3e70
movq %rax, %r15
testl %r15d, %r15d
je 0x3e53
movl 0xe0(%r14), %ebp
testb %r12b, %r12b
jne 0x3e19
movq %r14, %rdi
movl %ebp, %esi
callq 0x382c
testq %rax, %rax
jne 0x3e33
leaq 0x8520(%rip), %rdx      # 0xc340
movq %r14, %rdi
movl %ebp, %esi
callq 0x34c0
testq %rax, %rax
je 0x41a3
movq %rax, %r12
movslq %r15d, %r15
movl $0x28000, %edx          # imm = 0x28000
movq %r14, %rdi
movq %r15, %rsi
callq 0x31a0
jmp 0x3f44
testb %r13b, %r13b
je 0x3e70
movq (%rbp), %rsi
leaq 0x5290(%rip), %rdi      # 0x90ee
xorl %eax, %eax
callq 0x3040
movl $0x1, %r13d
jmp 0x4169
movslq 0xe0(%r14), %r13
movq 0xe8(%r14), %rax
movq %r13, %rcx
shlq $0x7, %rcx
movq 0x10(%rax,%rcx), %r15
testb %r12b, %r12b
jne 0x3e9f
movq %r14, %rdi
movl %r13d, %esi
callq 0x382c
testq %rax, %rax
jne 0x3eba
leaq 0x849a(%rip), %rdx      # 0xc340
movq %r14, %rdi
movl %r13d, %esi
callq 0x34c0
testq %rax, %rax
je 0x4199
movq %rax, %r12
movq (%rax), %rdx
movq %r14, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x31b0
leaq 0xf0(%rsp), %r10
movq $0x0, 0x18(%r10)
xorps %xmm0, %xmm0
movaps %xmm0, (%r10)
movq %rax, 0x60(%r10)
movq $0x28000, 0x48(%r10)    # imm = 0x28000
movl 0x14(%r12), %eax
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x35c, %r8d            # imm = 0x35C
movl $0x280, %r9d            # imm = 0x280
pushq %r10
pushq $0x2809                 # imm = 0x2809
pushq (%r12)
pushq $0x1
pushq %rax
pushq $0x0
callq 0x3520
addq $0x30, %rsp
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x3330
leaq 0x51f9(%rip), %rdx      # 0x9132
movq %r14, %rdi
movq %r15, %rsi
callq 0x30a0
leaq 0x51f5(%rip), %rsi      # 0x9140
movq %r14, %rdi
xorl %edx, %edx
callq 0x3220
leaq 0x60(%rsp), %rdx
movq %rax, (%rdx)
movq %r14, %rdi
movq %r15, %rsi
movl $0x1, %ecx
callq 0x3180
testb $0x1, 0x18(%rsp)
je 0x3f79
callq 0x41b0
movl $0x1, %r13d
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x33b0
testq %rax, %rax
je 0x4188
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3250
testl %eax, %eax
je 0x418f
testb $0x1, 0x30(%rsp)
je 0x3fe2
movq 0x8007(%rip), %rax      # 0xbfc8
movq (%rax), %r13
movl $0x1f02, %edi           # imm = 0x1F02
callq 0x3190
leaq 0x51b1(%rip), %rsi      # 0x9186
movq %r13, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x34a0
movl $0xb71, %edi            # imm = 0xB71
callq 0x32a0
leaq 0x68(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x35c0
movl 0x28(%rsp), %edi
callq 0x7efa
movl 0x24(%rsp), %edi
callq 0x810c
movl 0x10(%rsp), %edi
callq 0x8066
movl 0xc(%rsp), %edi
callq 0x7f88
movl 0x20(%rsp), %edi
callq 0x7e20
movl $0x19, %edi
callq 0x7eaa
movss 0x14(%rsp), %xmm0
callq 0x7ff4
movzbl 0x2c(%rsp), %edi
andl $0x1, %edi
callq 0x7fbe
xorl %edi, %edi
callq 0x8210
movl 0x1c(%rsp), %ebp
testl %ebp, %ebp
js 0x406a
xorl %edi, %edi
callq 0x817c
movl %ebp, %edi
callq 0x81ae
jmp 0x4074
movl $0x1, %edi
callq 0x817c
callq 0x5604
movl 0x70(%rsp), %edi
movl 0x74(%rsp), %esi
callq 0x4222
callq 0x4287
leaq 0xf0(%rsp), %r13
movl $0x20000, %edx          # imm = 0x20000
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rcx
callq 0x31f0
testl %eax, %eax
jne 0x40c1
movq %r14, %rdi
movq %r15, %rsi
movl $0x21, %edx
movq %r13, %rcx
callq 0x3420
testl %eax, %eax
je 0x410a
movl 0xf0(%rsp), %eax
cmpl $0x21, %eax
je 0x40fb
cmpl $0x16, %eax
jne 0x410a
movl 0x128(%rsp), %edi
movl 0x12c(%rsp), %esi
cmpl 0x70(%rsp), %edi
jne 0x40ec
cmpl 0x74(%rsp), %esi
je 0x410a
movl %edi, 0x70(%rsp)
movl %esi, 0x74(%rsp)
callq 0x4222
jmp 0x4093
movq 0x128(%rsp), %rax
cmpq 0x60(%rsp), %rax
je 0x412b
callq 0x5391
callq 0x4371
movq %r14, %rdi
movq %r15, %rsi
callq 0x35f0
movl %ebx, %edi
callq 0x3490
jmp 0x4093
callq 0x55ff
testb $0x1, 0x18(%rsp)
je 0x413c
callq 0x41cd
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3250
movq %r14, %rdi
movq %r12, %rsi
callq 0x31d0
movq %r14, %rdi
movq %r15, %rsi
callq 0x3540
movq %r14, %rdi
callq 0x3620
movl %r13d, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x36fe
jmp 0x3e65
callq 0x36c0
jmp 0x4169
callq 0x369e
jmp 0x3e65
callq 0x36e2
jmp 0x3e65
callq 0x3682
jmp 0x3e65
nopl (%rax)