// File: /workspace/github_c_makefile_star2_O1/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, %r13d
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 0x6355
sete %al
movl %ebx, %ecx
orl %ebp, %ecx
sete %cl
orb %al, %cl
jne 0x635d
movl $0x1, 0x14(%r15)
cmpl $-0x2, %r12d
je 0x6379
leaq 0x1b79(%rip), %rsi      # 0x7ee3
movl $0x3, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2c88
testl %r12d, %r12d
jle 0x6540
leaq 0x4de7(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4f62(%rip)      # 0xb2f8
jne 0x63e2
leaq 0x4dd1(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1c49(%rip), %r12      # 0x7ff4
movq %r12, %rdi
callq 0x5d53
movq %rax, %r14
leaq 0x11d9(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c88
leaq 0x4d9a(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r14, 0x4f16(%rip)      # 0xb2f8
leaq 0x4d87(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4f02(%rip)      # 0xb2f8
je 0x6501
leaq 0x4d6d(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4ef0(%rip)      # 0xb300
jne 0x6462
leaq 0x4d57(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1bdb(%rip), %r12      # 0x8000
movq %r12, %rdi
callq 0x5d53
movl %r13d, %r14d
movq %rax, %r13
leaq 0x115c(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c88
leaq 0x4d1d(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r13, 0x4ea1(%rip)      # 0xb300
movl %r14d, %r13d
leaq 0x4d07(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4e8a(%rip)      # 0xb300
je 0x650a
leaq 0x4ced(%rip), %rdi      # 0xb170
callq 0x21a0
cmpq $0x0, 0x4e78(%rip)      # 0xb308
jne 0x64e2
leaq 0x4cd7(%rip), %rdi      # 0xb170
callq 0x2120
leaq 0x1b68(%rip), %r12      # 0x800d
movq %r12, %rdi
callq 0x5d53
movl %r13d, %r14d
movq %rax, %r13
leaq 0x10dc(%rip), %rsi      # 0x7596
movl $0x4, %edi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2c88
leaq 0x4c9d(%rip), %rdi      # 0xb170
callq 0x21a0
movq %r13, 0x4e29(%rip)      # 0xb308
movl %r14d, %r13d
leaq 0x4c87(%rip), %rdi      # 0xb170
callq 0x2120
cmpq $0x0, 0x4e12(%rip)      # 0xb308
jne 0x6540
leaq 0x1b0e(%rip), %rdx      # 0x800d
jmp 0x6511
leaq 0x1aec(%rip), %rdx      # 0x7ff4
jmp 0x6511
leaq 0x1aef(%rip), %rdx      # 0x8000
leaq 0x1973(%rip), %rsi      # 0x7e8b
movl $0x2, %edi
xorl %eax, %eax
callq 0x2c88
movl $0x0, (%r15)
leaq 0x19d5(%rip), %rsi      # 0x7f07
movl $0x2, %edi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2c88
movslq (%r15), %r12
testq %r12, %r12
jle 0x65b7
leaq 0x8(%r15), %r14
leaq (,%r12,8), %rdi
callq 0x2130
movq %rax, (%r14)
testq %rax, %rax
je 0x65c6
xorl %eax, %eax
movq (%r14), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x6563
leaq 0x19f0(%rip), %rsi      # 0x7f6d
movl $0x4, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2c88
testb $0x1, 0x14(%r15)
jne 0x65ee
leaq 0x1a32(%rip), %rsi      # 0x7fcc
movl $0x3, %edi
movl %r13d, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c88
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%r15)
leaq 0x1962(%rip), %rsi      # 0x7f36
movl $0x2, %edi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c88
leaq 0x1996(%rip), %rsi      # 0x7f8b
movl $0x3, %edi
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 0x2c88