// File: /workspace/github_c_makefile_star2_O1/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 0x245f(%rip), %rdi      # 0xd778
movl $0x2, %esi
callq 0x5370
movq %rax, 0xd83e(%rip)      # 0x18b68
testq %rax, %rax
je 0xb339
callq 0x5060
testq %rax, %rax
je 0xb358
movq $0x0, 0xd824(%rip) # 0x18b68
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x23cd(%rip), %rbx      # 0xd72c
xorl %r14d, %r14d
leaq 0xd807(%rip), %r15      # 0x18b70
movq %rbx, %r12
movq 0xd7f5(%rip), %rdi      # 0x18b68
movslq (%r12), %rsi
addq %rbx, %rsi
callq 0x54f0
movq %rax, (%r14,%r15)
callq 0x5060
cmpq $0x0, (%r14,%r15)
movl $0x5, %ecx
je 0xb3af
testq %rax, %rax
jne 0xb3af
addq $0x8, %r14
addq $0x4, %r12
cmpq $0x98, %r14
jne 0xb36c
movl $0x2, %ecx
cmpl $0x5, %ecx
je 0xb5db
cmpl $0x2, %ecx
jne 0xb349
xorl %edi, %edi
callq *0xd7ab(%rip)           # 0x18b70
movq %rax, 0xd83c(%rip)      # 0x18c08
testq %rax, %rax
je 0xb5db
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 *0xd773(%rip)           # 0x18b78
addq $0x20, %rsp
movq %rax, 0xd800(%rip)      # 0x18c10
movq 0xd7f1(%rip), %rdi      # 0x18c08
leaq 0x2364(%rip), %rsi      # 0xd782
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x4(%rsp), %r8
callq *0xd765(%rip)           # 0x18b98
testl %eax, %eax
je 0xb5db
movq 0xd7c6(%rip), %rdi      # 0x18c08
leaq 0x2339(%rip), %rsi      # 0xd782
callq *0xd751(%rip)           # 0x18ba0
testq %rax, %rax
je 0xb5db
movl 0x4(%rax), %eax
movl %eax, 0xd7b7(%rip)      # 0x18c18
movq 0xd7a0(%rip), %rdi      # 0x18c08
callq *0xd75a(%rip)           # 0x18bc8
movq 0xd793(%rip), %rdi      # 0x18c08
movq 0xd794(%rip), %rsi      # 0x18c10
movl $0x356, %edx            # imm = 0x356
movl $0x1e0, %ecx            # imm = 0x1E0
movl $0x18, %r8d
callq *0xd71e(%rip)           # 0x18bb0
movq %rax, 0xd787(%rip)      # 0x18c20
testq %rax, %rax
je 0xb5db
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 0xd738(%rip), %rdi      # 0x18c08
movq 0xe8(%rdi), %rcx
movq 0x58(%rcx), %rdx
movq %rdx, 0x10(%rbx)
movq 0x60(%rcx), %rcx
movq %rcx, 0x18(%rbx)
movl $0x2, 0x20(%rbx)
movl $0x1c, %edx
movq %rax, %rsi
movq %rbx, %rcx
callq *0xd6a9(%rip)           # 0x18ba8
movq %rax, 0xd722(%rip)      # 0x18c28
movq 0xd6fb(%rip), %rdi      # 0x18c08
movq 0xe8(%rdi), %rax
movq 0x60(%rax), %rcx
movq %rcx, 0x10(%rbx)
movq 0x58(%rax), %rax
movq %rax, 0x18(%rbx)
movq 0xd6f5(%rip), %rsi      # 0x18c20
movl $0x1c, %edx
movq %rbx, %rcx
callq *0xd66f(%rip)           # 0x18ba8
movq %rax, 0xd6f0(%rip)      # 0x18c30
cmpq $0x0, 0xd6e0(%rip)      # 0x18c28
setne %cl
testq %rax, %rax
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0xb5db
movq 0xd6a6(%rip), %rdi      # 0x18c08
movq 0xd6a7(%rip), %rsi      # 0x18c10
leaq 0x221a(%rip), %rdx      # 0xd78a
callq *0xd61a(%rip)           # 0x18b90
movq 0xd68b(%rip), %rdi      # 0x18c08
movq 0xd68c(%rip), %rsi      # 0x18c10
movl $0x1, %edx
callq *0xd641(%rip)           # 0x18bd0
movq 0xd672(%rip), %rdi      # 0x18c08
leaq 0x2202(%rip), %rsi      # 0xd79f
xorl %edx, %edx
callq *0xd64b(%rip)           # 0x18bf0
movq %rax, 0xd68c(%rip)      # 0x18c38
leaq 0xd685(%rip), %rdx      # 0x18c38
movq 0xd64e(%rip), %rdi      # 0x18c08
movq 0xd64f(%rip), %rsi      # 0x18c10
movl $0x1, %ecx
callq *0xd62c(%rip)           # 0x18bf8
movq 0xd635(%rip), %rax      # 0x18c08
movl 0x10(%rax), %eax
jmp 0xb349
movq 0xd586(%rip), %rdi      # 0x18b68
callq 0x55d0
xorl %eax, %eax
movq %rax, 0xd578(%rip)      # 0x18b68
movq %rax, 0xd611(%rip)      # 0x18c08
movq %rax, 0xd612(%rip)      # 0x18c10
jmp 0xb344