// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
leaq 0x2bdb(%rip), %rdi      # 0xd798
pushq $0x2
popq %rsi
callq 0x5380
movq %rax, 0xef9c(%rip)      # 0x19b68
testq %rax, %rax
je 0xabdb
callq 0x5060
testq %rax, %rax
je 0xabe8
andq $0x0, 0xef85(%rip)      # 0x19b68
jmp 0xae6e
leaq 0x2b5d(%rip), %rbx      # 0xd74c
xorl %r14d, %r14d
leaq 0xef77(%rip), %r15      # 0x19b70
movq %rbx, %r12
cmpq $0x98, %r14
je 0xac3f
movq 0xef5c(%rip), %rdi      # 0x19b68
movslq (%r12), %rsi
addq %rbx, %rsi
callq 0x5500
movq %rax, (%r14,%r15)
callq 0x5060
cmpq $0x0, (%r14,%r15)
je 0xae4a
testq %rax, %rax
jne 0xae4a
addq $0x8, %r14
addq $0x4, %r12
jmp 0xabfc
xorl %edi, %edi
callq *0xef29(%rip)           # 0x19b70
movq %rax, 0xefba(%rip)      # 0x19c08
testq %rax, %rax
je 0xae4a
movq 0xe8(%rax), %rcx
movq 0x10(%rcx), %rsi
movq 0x60(%rcx), %rcx
andq $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %rcx, 0x10(%rsp)
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x356, %r8d            # imm = 0x356
movl $0x1e0, %r9d            # imm = 0x1E0
callq *0xeeea(%rip)           # 0x19b78
movq %rax, 0xef7b(%rip)      # 0x19c10
movq 0xef6c(%rip), %rdi      # 0x19c08
leaq 0x2aff(%rip), %rsi      # 0xd7a2
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq *0xeee0(%rip)           # 0x19b98
testl %eax, %eax
je 0xae4a
movq 0xef41(%rip), %rdi      # 0x19c08
leaq 0x2ad4(%rip), %rsi      # 0xd7a2
callq *0xeecc(%rip)           # 0x19ba0
testq %rax, %rax
je 0xae4a
movl 0x4(%rax), %eax
movl %eax, 0xef32(%rip)      # 0x19c18
movq 0xef1b(%rip), %rdi      # 0x19c08
callq *0xeed5(%rip)           # 0x19bc8
movq 0xef0e(%rip), %rdi      # 0x19c08
movq 0xef0f(%rip), %rsi      # 0x19c10
pushq $0x18
popq %r8
movl $0x356, %edx            # imm = 0x356
movl $0x1e0, %ecx            # imm = 0x1E0
callq *0xee9b(%rip)           # 0x19bb0
movq %rax, 0xef04(%rip)      # 0x19c20
testq %rax, %rax
je 0xae4a
xorps %xmm0, %xmm0
leaq 0x30(%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 0xeeb5(%rip), %rdi      # 0x19c08
movq 0xe8(%rdi), %rcx
movups 0x58(%rcx), %xmm0
movaps %xmm0, 0x10(%rbx)
movl $0x2, 0x20(%rbx)
pushq $0x1c
popq %r14
movq %rax, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq *0xee2c(%rip)           # 0x19ba8
movq %rax, 0xeea5(%rip)      # 0x19c28
movq 0xee7e(%rip), %rdi      # 0x19c08
movq 0xe8(%rdi), %rax
movdqu 0x58(%rax), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqa %xmm0, 0x10(%rbx)
movq 0xee79(%rip), %rsi      # 0x19c20
movq %r14, %rdx
movq %rbx, %rcx
callq *0xedf5(%rip)           # 0x19ba8
movq %rax, 0xee76(%rip)      # 0x19c30
cmpq $0x0, 0xee66(%rip)      # 0x19c28
setne %cl
testq %rax, %rax
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0xae4a
movq 0xee30(%rip), %rdi      # 0x19c08
movq 0xee31(%rip), %rsi      # 0x19c10
leaq 0x29c4(%rip), %rdx      # 0xd7aa
callq *0xeda4(%rip)           # 0x19b90
movq 0xee15(%rip), %rdi      # 0x19c08
movq 0xee16(%rip), %rsi      # 0x19c10
pushq $0x1
popq %rdx
callq *0xedcd(%rip)           # 0x19bd0
movq 0xedfe(%rip), %rdi      # 0x19c08
leaq 0x29ae(%rip), %rsi      # 0xd7bf
xorl %edx, %edx
callq *0xedd7(%rip)           # 0x19bf0
movq %rax, 0xee18(%rip)      # 0x19c38
leaq 0xee11(%rip), %rdx      # 0x19c38
movq 0xedda(%rip), %rdi      # 0x19c08
movq 0xeddb(%rip), %rsi      # 0x19c10
pushq $0x1
popq %rcx
callq *0xedba(%rip)           # 0x19bf8
movq 0xedc3(%rip), %rax      # 0x19c08
movl 0x10(%rax), %eax
jmp 0xae71
movq 0xed17(%rip), %rdi      # 0x19b68
callq 0x55e0
andq $0x0, 0xed0a(%rip)      # 0x19b68
andq $0x0, 0xeda2(%rip)      # 0x19c08
andq $0x0, 0xeda2(%rip)      # 0x19c10
pushq $-0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq