// File: /workspace/github_cpp_makefile_star2_O1/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 0x3d2f
movl %edi, %r13d
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
movl $0x1, %r12d
movl $0x2, 0xc(%rsp)
movb $0x1, %al
movl %eax, 0x2c(%rsp)
movl $0xa, 0x4c(%rsp)
movl $0x0, 0x1c(%rsp)
leaq 0x588a(%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 0x3330
testl %eax, %eax
je 0x3b0b
movq %rbx, %rdi
leaq 0x57eb(%rip), %rsi      # 0x91a0
callq 0x3330
testl %eax, %eax
je 0x3b13
movq %rbx, %rdi
leaq 0x57dd(%rip), %rsi      # 0x91a9
callq 0x3330
testl %eax, %eax
je 0x3b1e
movq %rbx, %rdi
leaq 0x57cb(%rip), %rsi      # 0x91ae
callq 0x3470
testl %eax, %eax
je 0x3b29
movq %rbx, %rdi
leaq 0x57bb(%rip), %rsi      # 0x91b5
callq 0x3470
testl %eax, %eax
je 0x3b37
movq %rbx, %rdi
leaq 0x57ac(%rip), %rsi      # 0x91bd
callq 0x3470
testl %eax, %eax
je 0x3b45
movq %rbx, %rdi
leaq 0x579f(%rip), %rsi      # 0x91c7
callq 0x3470
testl %eax, %eax
je 0x3b53
movq %rbx, %rdi
leaq 0x5791(%rip), %rsi      # 0x91d0
callq 0x3470
testl %eax, %eax
je 0x3b61
movq %rbx, %rdi
leaq 0x5781(%rip), %rsi      # 0x91d7
callq 0x3470
testl %eax, %eax
je 0x3b6f
movq %rbx, %rdi
leaq 0x5778(%rip), %rsi      # 0x91e5
callq 0x3470
testl %eax, %eax
je 0x3b7d
movq %rbx, %rdi
leaq 0x5768(%rip), %rsi      # 0x91ec
callq 0x3470
testl %eax, %eax
je 0x3b8b
movq %rbx, %rdi
leaq 0x5760(%rip), %rsi      # 0x91fb
callq 0x3470
testl %eax, %eax
je 0x3b99
movq %rbx, %rdi
leaq 0x5750(%rip), %rsi      # 0x9202
callq 0x3470
testl %eax, %eax
je 0x3ba7
movq %rbx, %rdi
leaq 0x5743(%rip), %rsi      # 0x920c
callq 0x3470
testl %eax, %eax
je 0x3cfd
movq %rbx, %rdi
leaq 0x5735(%rip), %rsi      # 0x9215
callq 0x3470
testl %eax, %eax
je 0x3bb4
movl 0x14(%rsp), %eax
cmpq %rax, %r12
jne 0x3bc1
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x50(%rsp)
jmp 0x3cfd
xorl %r15d, %r15d
jmp 0x3cfd
movb $0x1, %al
movl %eax, 0x30(%rsp)
jmp 0x3cfd
movb $0x1, %al
movl %eax, 0x18(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x14(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x5c(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x58(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x48(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x44(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x40(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x3c(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x38(%rsp)
jmp 0x3cfd
leal 0x1(%r12), %eax
movl %eax, 0x34(%rsp)
jmp 0x3cfd
movl $0x0, 0x2c(%rsp)
jmp 0x3cfd
movl $0x0, 0x54(%rsp)
jmp 0x3cfd
movl 0x5c(%rsp), %eax
cmpq %rax, %r12
jne 0x3bdb
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x28(%rsp)
jmp 0x3cfd
movl 0x58(%rsp), %eax
cmpq %rax, %r12
jne 0x3bf5
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x24(%rsp)
jmp 0x3cfd
movl 0x40(%rsp), %eax
cmpq %rax, %r12
jne 0x3c0f
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x20(%rsp)
jmp 0x3cfd
movl 0x3c(%rsp), %eax
cmpq %rax, %r12
je 0x3cfd
movl 0x38(%rsp), %eax
cmpq %rax, %r12
jne 0x3c36
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x4c(%rsp)
jmp 0x3cfd
movl 0x34(%rsp), %eax
cmpq %rax, %r12
jne 0x3c6f
movq %rbx, %rdi
leaq 0x599c(%rip), %rsi      # 0x95e5
callq 0x3470
movl $0xffffffff, 0x1c(%rsp) # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x3cfd
movq %rbx, %rdi
callq 0x3540
movl %eax, 0x1c(%rsp)
jmp 0x3cfd
movl 0x48(%rsp), %eax
cmpq %rax, %r12
jne 0x3cb7
movq %rbx, %rdi
leaq 0x559d(%rip), %rsi      # 0x921f
callq 0x3470
testl %eax, %eax
movl $0x1, %eax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x5580(%rip), %rsi      # 0x9223
callq 0x3470
testl %eax, %eax
movl 0x10(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
jmp 0x3cfd
movl 0x44(%rsp), %eax
cmpq %rax, %r12
jne 0x3cfd
movq %rbx, %rdi
leaq 0x555d(%rip), %rsi      # 0x9227
callq 0x3470
testl %eax, %eax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
movq %rbx, %rdi
leaq 0x5547(%rip), %rsi      # 0x922d
callq 0x3470
testl %eax, %eax
movl $0x1, %eax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
incq %r12
cmpq %r12, %r13
jne 0x3993
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 0x3d8e
movl $0x2710, %ebx           # imm = 0x2710
movl $0x2, 0xc(%rsp)
movl $0x1, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x2c(%rsp)
movss 0x52b2(%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 0x5307(%rip), %rdi      # 0x909c
callq 0x33b0
movq %rax, %rdi
callq 0x3460
testq %rax, %rax
je 0x419f
movq %rax, %r14
leaq 0x530c(%rip), %rdi      # 0x90c1
callq 0x33b0
movq %rax, %r15
testb $0x1, 0x18(%rsp)
je 0x3df2
leaq 0x52d1(%rip), %rdi      # 0x909c
callq 0x33b0
leaq 0x52fe(%rip), %rdi      # 0x90d5
movq %rax, %rsi
xorl %eax, %eax
callq 0x3040
leaq 0x52fb(%rip), %rdi      # 0x90e3
movq %r15, %rsi
xorl %eax, %eax
callq 0x3040
testq %r15, %r15
je 0x3e08
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3480
movq %rax, %r15
jmp 0x3e0b
xorl %r15d, %r15d
testb %r13b, %r13b
je 0x3e31
testl %r15d, %r15d
je 0x3f13
movl 0xe0(%r14), %ebp
testb %r12b, %r12b
je 0x3f2a
xorl %r12d, %r12d
jmp 0x3f37
movslq 0xe0(%r14), %r13
movq 0xe8(%r14), %rax
movq %r13, %rcx
shlq $0x7, %rcx
movq 0x10(%rax,%rcx), %r15
testb %r12b, %r12b
je 0x3e55
xorl %r12d, %r12d
jmp 0x3e63
movq %r14, %rdi
movl %r13d, %esi
callq 0x383c
movq %rax, %r12
testq %r12, %r12
jne 0x3e7d
leaq 0x84d1(%rip), %rdx      # 0xc340
movq %r14, %rdi
movl %r13d, %esi
callq 0x34d0
movq %rax, %r12
testq %r12, %r12
je 0x41ad
movq (%r12), %rdx
xorl %r13d, %r13d
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 0x3530
addq $0x30, %rsp
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x3340
leaq 0x522c(%rip), %rdx      # 0x9132
movq %r14, %rdi
movq %r15, %rsi
callq 0x30a0
jmp 0x3f6f
movq (%rbp), %rsi
leaq 0x51d0(%rip), %rdi      # 0x90ee
xorl %eax, %eax
callq 0x3040
jmp 0x41c2
movq %r14, %rdi
movl %ebp, %esi
callq 0x383c
movq %rax, %r12
testq %r12, %r12
jne 0x3f50
leaq 0x83fd(%rip), %rdx      # 0xc340
movq %r14, %rdi
movl %ebp, %esi
callq 0x34d0
movq %rax, %r12
movslq %r15d, %r15
testq %r12, %r12
je 0x41dd
movl $0x28000, %edx          # imm = 0x28000
movq %r14, %rdi
movq %r15, %rsi
callq 0x31a0
xorl %r13d, %r13d
testq %r12, %r12
je 0x41c8
leaq 0x51c1(%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 0x3fad
callq 0x41f0
movl $0x1, %r13d
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x33c0
testq %rax, %rax
je 0x41a6
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3260
testl %eax, %eax
je 0x41bd
testb $0x1, 0x30(%rsp)
je 0x4016
movq 0x7fd3(%rip), %rax      # 0xbfc8
movq (%rax), %r13
movl $0x1f02, %edi           # imm = 0x1F02
callq 0x3190
leaq 0x517d(%rip), %rsi      # 0x9186
movq %r13, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x34b0
movl $0xb71, %edi            # imm = 0xB71
callq 0x32b0
leaq 0x68(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x35d0
movl 0x28(%rsp), %edi
callq 0x809e
movl 0x24(%rsp), %edi
callq 0x82b0
movl 0x10(%rsp), %edi
callq 0x820a
movl 0xc(%rsp), %edi
callq 0x812c
movl 0x20(%rsp), %edi
callq 0x7fc4
movl $0x19, %edi
callq 0x804e
movss 0x14(%rsp), %xmm0
callq 0x8198
movzbl 0x2c(%rsp), %edi
andl $0x1, %edi
callq 0x8162
xorl %edi, %edi
callq 0x83b4
movl 0x1c(%rsp), %ebp
testl %ebp, %ebp
js 0x409e
xorl %edi, %edi
callq 0x8320
movl %ebp, %edi
callq 0x8352
jmp 0x40a8
movl $0x1, %edi
callq 0x8320
callq 0x56dc
movl 0x70(%rsp), %edi
movl 0x74(%rsp), %esi
callq 0x4262
callq 0x42c7
leaq 0xf0(%rsp), %r13
movl $0x20000, %edx          # imm = 0x20000
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rcx
callq 0x31f0
testl %eax, %eax
jne 0x40f5
movq %r14, %rdi
movq %r15, %rsi
movl $0x21, %edx
movq %r13, %rcx
callq 0x3430
testl %eax, %eax
je 0x413e
movl 0xf0(%rsp), %eax
cmpl $0x21, %eax
je 0x412f
cmpl $0x16, %eax
jne 0x413e
movl 0x128(%rsp), %edi
movl 0x12c(%rsp), %esi
cmpl 0x70(%rsp), %edi
jne 0x4120
cmpl 0x74(%rsp), %esi
je 0x413e
movl %edi, 0x70(%rsp)
movl %esi, 0x74(%rsp)
callq 0x4262
jmp 0x40c7
movq 0x128(%rsp), %rax
cmpq 0x60(%rsp), %rax
je 0x415f
callq 0x547e
callq 0x43b1
movq %r14, %rdi
movq %r15, %rsi
callq 0x3600
movl %ebx, %edi
callq 0x34a0
jmp 0x40c7
callq 0x56d7
testb $0x1, 0x18(%rsp)
je 0x4170
callq 0x420d
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
movq %r14, %rdi
movq %r12, %rsi
callq 0x31d0
movq %r14, %rdi
movq %r15, %rsi
callq 0x3550
movq %r14, %rdi
callq 0x3630
jmp 0x41c8
callq 0x370e
jmp 0x41c2
callq 0x36ec
jmp 0x41c8
callq 0x3692
movl $0x1, %r13d
jmp 0x3f6f
callq 0x36ca
movl $0x1, %r13d
movl %r13d, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x36ae
movl $0x1, %r13d
jmp 0x3f6f
nopl (%rax)