// File: /workspace/github_c_makefile_star2_O2/derhass[P]glx_hook/glx_hook.c
// Function: latency_init
static void
latency_init(GH_latency *lat, int latency, int manual_wait, unsigned gl_wait_timeout_usecs, unsigned int gl_wait_interval_usecs, unsigned int self_wait_interval_usecs)
{
	lat->latency=latency;
	lat->sync_object=NULL;
	lat->cur_pos=0;
	lat->flags=0;
	lat->gl_wait_timeout=(GLuint64)gl_wait_timeout_usecs * (GLuint64)1000;
	lat->gl_wait_interval=(GLuint64)gl_wait_interval_usecs * (GLuint64)1000;
	lat->self_wait_interval=(useconds_t)self_wait_interval_usecs;

	if (manual_wait > 0) {
		lat->flags |= GH_LATENCY_FLAG_MANUAL_WAIT;
	} else if (manual_wait < 0) {
		if ((gl_wait_interval_usecs > 0) || (self_wait_interval_usecs > 0))  {
			lat->flags |= GH_LATENCY_FLAG_MANUAL_WAIT;
		}
	}

	if (latency != GH_LATENCY_NOP) {
		GH_verbose(GH_MSG_INFO, "setting up latency limiter mode %d\n", latency);
	}

	if (latency > 0) {
		if (latency_gl_init()) {
			lat->latency=GH_LATENCY_FINISH_BEFORE;
			GH_verbose(GH_MSG_WARNING, "GPU sync not available, using latency mode %d\n",
					GH_LATENCY_FINISH_BEFORE);
		}
	}

	if (lat->latency > 0) {
		unsigned int cnt=(unsigned)lat->latency;
		lat->sync_object=malloc(sizeof(*lat->sync_object) * cnt);
		if (!lat->sync_object) {
			lat->latency=GH_LATENCY_FINISH_BEFORE;
			GH_verbose(GH_MSG_WARNING, "out of memory for sync objects, using latency mode %d\n");
		} else {
			unsigned int i;
			for (i=0; i<cnt; i++) {
				lat->sync_object[i]=NULL;
			}
			GH_verbose(GH_MSG_DEBUG, "enabling latency limiter: %d\n",lat->latency);
			if (lat->flags & GH_LATENCY_FLAG_MANUAL_WAIT) {
				GH_verbose(GH_MSG_INFO, "latency limiter with manual waits GL: %u usecs + self: %u usecs\n",
						gl_wait_interval_usecs, self_wait_interval_usecs);
			} else {
				GH_verbose(GH_MSG_INFO, "latency limiter with timeout: %u usecs\n",
						gl_wait_timeout_usecs);
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r9d, %ebx
movl %r8d, %ebp
movl %ecx, %r14d
movl %esi, %r12d
movq %rdi, %r15
movl %esi, (%rdi)
movl %ecx, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rdi)
movq %rax, 0x18(%rdi)
movl %r8d, %eax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, 0x20(%rdi)
movl %r9d, 0x28(%rdi)
testl %edx, %edx
jg 0x5f86
sete %al
movl %ebx, %ecx
orl %ebp, %ecx
sete %cl
orb %al, %cl
jne 0x5f8e
movl $0x1, 0x14(%r15)
cmpl $-0x2, %r12d
je 0x615e
leaq 0x1f44(%rip), %rsi      # 0x7ee3
pushq $0x3
popq %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2c49
testl %r12d, %r12d
jle 0x615e
leaq 0x51b4(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x532f(%rip)      # 0xb2f8
jne 0x6013
leaq 0x519e(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x2016(%rip), %r13      # 0x7ff4
movq %r13, %rdi
callq 0x5e63
movq %rax, %r12
leaq 0x15a6(%rip), %rsi      # 0x7596
pushq $0x4
popq %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c49
leaq 0x5169(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x52e5(%rip)      # 0xb2f8
leaq 0x5156(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x52d1(%rip)      # 0xb2f8
je 0x6122
leaq 0x513c(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x52bf(%rip)      # 0xb300
jne 0x608b
leaq 0x5126(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1faa(%rip), %r13      # 0x8000
movq %r13, %rdi
callq 0x5e63
movq %rax, %r12
leaq 0x152e(%rip), %rsi      # 0x7596
pushq $0x4
popq %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c49
leaq 0x50f1(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x5275(%rip)      # 0xb300
leaq 0x50de(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x5261(%rip)      # 0xb300
je 0x612b
leaq 0x50c4(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x524f(%rip)      # 0xb308
jne 0x6103
leaq 0x50ae(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1f3f(%rip), %r13      # 0x800d
movq %r13, %rdi
callq 0x5e63
movq %rax, %r12
leaq 0x14b6(%rip), %rsi      # 0x7596
pushq $0x4
popq %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c49
leaq 0x5079(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r12, 0x5205(%rip)      # 0xb308
leaq 0x5066(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x51f1(%rip)      # 0xb308
jne 0x615e
leaq 0x1eed(%rip), %rdx      # 0x800d
jmp 0x6132
leaq 0x1ecb(%rip), %rdx      # 0x7ff4
jmp 0x6132
leaq 0x1ece(%rip), %rdx      # 0x8000
leaq 0x1d52(%rip), %rsi      # 0x7e8b
pushq $0x2
popq %r12
movl %r12d, %edi
xorl %eax, %eax
callq 0x2c49
andl $0x0, (%r15)
leaq 0x1db5(%rip), %rsi      # 0x7f07
movl %r12d, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2c49
movslq (%r15), %r12
testq %r12, %r12
jle 0x61d2
leaq 0x8(%r15), %r13
leaq (,%r12,8), %rdi
callq 0x2130
movq %rax, (%r13)
testq %rax, %rax
je 0x61e1
xorl %eax, %eax
cmpq %rax, %r12
je 0x6195
movq (%r13), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x6182
leaq 0x1dd1(%rip), %rsi      # 0x7f6d
pushq $0x4
popq %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2c49
testb $0x1, 0x14(%r15)
jne 0x6204
leaq 0x1e15(%rip), %rsi      # 0x7fcc
pushq $0x3
popq %rdi
movl %r14d, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c49
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, (%r15)
leaq 0x1d4a(%rip), %rsi      # 0x7f36
pushq $0x2
popq %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c49
leaq 0x1d80(%rip), %rsi      # 0x7f8b
pushq $0x3
popq %rdi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c49