// File: /workspace/github_c_makefile_star2_O3/baines[P]MiniGBS/x11.c
// Function: x11_init
int x11_init(void){
	xlib = dlopen("libX11.so", RTLD_NOW | RTLD_LOCAL);
	if(!xlib || dlerror()){
		xlib = NULL;
		return -1;
	}

	int i = 0;
	for(void** fn = (void**)&x11; fn != (void**)(&x11+1); ++fn, ++i){
		*fn = dlsym(xlib, x11_func_names[i]);

		const char* msg = dlerror();
		if(!*fn || msg){
			goto fail;
		}
	}

	if(!(x11_dpy = x11.open_dpy(NULL)))
		goto fail;

	x11_win = x11.simple_win(x11_dpy, RootWindow(x11_dpy, 0), 0, 0, X11_WIN_W, X11_WIN_H, 0, 0, BlackPixel(x11_dpy, 0));

	int op, ev, err;
	if(!x11.query_ext(x11_dpy, "Present", &op, &ev, &err))
		goto fail;

	XExtCodes* xec = x11.init_ext(x11_dpy, "Present");
	if(!xec)
		goto fail;

	x11_present_op = xec->major_opcode;
	x11.flush(x11_dpy);

	if(!(x11_pix = x11.create_pix(x11_dpy, x11_win, X11_WIN_W, X11_WIN_H, 24)))
		goto fail;

	XGCValues gcv = {
		.foreground = WhitePixel(x11_dpy, 0),
		.background = BlackPixel(x11_dpy, 0),
		.line_width = 2,
	};

	x11_gc = x11.create_gc(x11_dpy, x11_pix, GCForeground | GCBackground | GCLineWidth, &gcv);
	gcv.foreground = BlackPixel(x11_dpy, 0);
	gcv.background = WhitePixel(x11_dpy, 0);
	x11_gc2 = x11.create_gc(x11_dpy, x11_pix, GCForeground | GCBackground | GCLineWidth, &gcv);

	if(!x11_gc || !x11_gc2)
		goto fail;

	x11.store_name(x11_dpy, x11_win, "MiniGBS Oscilloscope");
	x11.sel_input(x11_dpy, x11_win, KeyPressMask);

    x11_atom_wm_delete = x11.intern_atom(x11_dpy, "WM_DELETE_WINDOW", 0);
    x11.set_protos(x11_dpy, x11_win, &x11_atom_wm_delete, 1);

	return ConnectionNumber(x11_dpy);

fail:
	dlclose(xlib);
	x11_dpy = xlib = NULL;
	x11_win = 0;
	return -1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x98, %rsp
leaq 0x2423(%rip), %rdi      # 0xd838
movl $0x2, %esi
callq 0x5370
movq %rax, 0xd742(%rip)      # 0x18b68
testq %rax, %rax
je 0xb435
callq 0x5060
testq %rax, %rax
je 0xb445
movq $0x0, 0xd728(%rip) # 0x18b68
jmp 0xb6cb
leaq 0x23a0(%rip), %rbx      # 0xd7ec
xorl %r14d, %r14d
leaq 0xd71a(%rip), %r15      # 0x18b70
movq %rbx, %r12
movq 0xd708(%rip), %rdi      # 0x18b68
movslq (%r12), %rsi
addq %rbx, %rsi
callq 0x54f0
movq %rax, (%r14,%r15)
callq 0x5060
cmpq $0x0, (%r14,%r15)
je 0xb6a8
testq %rax, %rax
jne 0xb6a8
addq $0x8, %r14
addq $0x4, %r12
cmpq $0x98, %r14
jne 0xb459
xorl %edi, %edi
callq *0xd6ce(%rip)           # 0x18b70
movq %rax, 0xd75f(%rip)      # 0x18c08
testq %rax, %rax
je 0xb6a8
movq 0xe8(%rax), %r10
movq 0x10(%r10), %rsi
subq $0x8, %rsp
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x356, %r8d            # imm = 0x356
movl $0x1e0, %r9d            # imm = 0x1E0
pushq 0x60(%r10)
pushq $0x0
pushq $0x0
callq *0xd696(%rip)           # 0x18b78
addq $0x20, %rsp
movq %rax, 0xd723(%rip)      # 0x18c10
movq 0xd714(%rip), %rdi      # 0x18c08
leaq 0x2347(%rip), %rsi      # 0xd842
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x4(%rsp), %r8
callq *0xd688(%rip)           # 0x18b98
testl %eax, %eax
je 0xb6a8
movq 0xd6e9(%rip), %rdi      # 0x18c08
leaq 0x231c(%rip), %rsi      # 0xd842
callq *0xd674(%rip)           # 0x18ba0
testq %rax, %rax
je 0xb6a8
movl 0x4(%rax), %eax
movl %eax, 0xd6da(%rip)      # 0x18c18
movq 0xd6c3(%rip), %rdi      # 0x18c08
callq *0xd67d(%rip)           # 0x18bc8
movq 0xd6b6(%rip), %rdi      # 0x18c08
movq 0xd6b7(%rip), %rsi      # 0x18c10
movl $0x356, %edx            # imm = 0x356
movl $0x1e0, %ecx            # imm = 0x1E0
movl $0x18, %r8d
callq *0xd641(%rip)           # 0x18bb0
movq %rax, 0xd6aa(%rip)      # 0x18c20
testq %rax, %rax
je 0xb6a8
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
movaps %xmm0, 0x70(%rbx)
movaps %xmm0, 0x60(%rbx)
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq 0xd65b(%rip), %rdi      # 0x18c08
movq 0xe8(%rdi), %rcx
movups 0x58(%rcx), %xmm0
movaps %xmm0, 0x10(%rbx)
movl $0x2, 0x20(%rbx)
movl $0x1c, %edx
movq %rax, %rsi
movq %rbx, %rcx
callq *0xd5d4(%rip)           # 0x18ba8
movq %rax, 0xd64d(%rip)      # 0x18c28
movq 0xd626(%rip), %rdi      # 0x18c08
movq 0xe8(%rdi), %rax
movdqu 0x58(%rax), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqa %xmm0, 0x10(%rbx)
movq 0xd621(%rip), %rsi      # 0x18c20
movl $0x1c, %edx
movq %rbx, %rcx
callq *0xd59b(%rip)           # 0x18ba8
movq %rax, 0xd61c(%rip)      # 0x18c30
cmpq $0x0, 0xd60c(%rip)      # 0x18c28
setne %cl
testq %rax, %rax
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0xb6a8
movq 0xd5d6(%rip), %rdi      # 0x18c08
movq 0xd5d7(%rip), %rsi      # 0x18c10
leaq 0x220a(%rip), %rdx      # 0xd84a
callq *0xd54a(%rip)           # 0x18b90
movq 0xd5bb(%rip), %rdi      # 0x18c08
movq 0xd5bc(%rip), %rsi      # 0x18c10
movl $0x1, %edx
callq *0xd571(%rip)           # 0x18bd0
movq 0xd5a2(%rip), %rdi      # 0x18c08
leaq 0x21f2(%rip), %rsi      # 0xd85f
xorl %edx, %edx
callq *0xd57b(%rip)           # 0x18bf0
movq %rax, 0xd5bc(%rip)      # 0x18c38
leaq 0xd5b5(%rip), %rdx      # 0x18c38
movq 0xd57e(%rip), %rdi      # 0x18c08
movq 0xd57f(%rip), %rsi      # 0x18c10
movl $0x1, %ecx
callq *0xd55c(%rip)           # 0x18bf8
movq 0xd565(%rip), %rax      # 0x18c08
movl 0x10(%rax), %eax
jmp 0xb6d0
movq 0xd4b9(%rip), %rdi      # 0x18b68
callq 0x55d0
xorl %eax, %eax
movq %rax, 0xd4ab(%rip)      # 0x18b68
movq %rax, 0xd544(%rip)      # 0x18c08
movq %rax, 0xd545(%rip)      # 0x18c10
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq