// File: /workspace/github_c_makefile_star2_O0/michaelforney[P]wld/buffered_surface.c
// Function: surface_back
struct buffer *
surface_back(struct wld_surface *base)
{
	struct buffered_surface *surface = buffered_surface(base);
	unsigned index;

	if (surface->back)
		return surface->back->buffer;

	/* The buffer socket may need to process any incoming buffer releases. */
	if (surface->buffer_socket)
		surface->buffer_socket->impl->process(surface->buffer_socket);

	for (index = 0; index < surface->entries_size; ++index) {
		if (!surface->entries[index].busy) {
			surface->back = &surface->entries[index];
			return surface->back->buffer;
		}
	}

	/* If there are no free buffers, we need to allocate another one. */
	struct buffer *buffer;

	buffer = surface->context->impl->create_buffer(surface->context, surface->width, surface->height,
	                                               surface->format, surface->flags);

	if (!buffer)
		goto error0;

	if (surface->entries_size == surface->entries_capacity) {
		struct buffer_entry *new_entries;
		size_t new_capacity = surface->entries_capacity * 2 + 1;

		new_entries = realloc(surface->entries,
		                      new_capacity * sizeof surface->entries[0]);

		if (!new_entries)
			goto error1;

		surface->entries = new_entries;
		surface->entries_capacity = new_capacity;
	}

	surface->back = &surface->entries[surface->entries_size++];
	*surface->back = (struct buffer_entry){
		.buffer = buffer,
		.busy = false
	};

	return buffer;

error1:
	wld_buffer_unreference(&buffer->base);
error0:
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xa290
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x9ea8
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0xa03b
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x9ecc
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rdi
callq *%rax
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jae 0x9f32
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
testb $0x1, 0x8(%rax)
jne 0x9f25
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x1c(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0xa03b
jmp 0x9f27
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9ed3
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x18(%rbp), %rcx
movl 0x30(%rcx), %esi
movq -0x18(%rbp), %rcx
movl 0x34(%rcx), %edx
movq -0x18(%rbp), %rcx
movl 0x38(%rcx), %ecx
movq -0x18(%rbp), %r8
movl 0x3c(%r8), %r8d
callq *%rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9f78
jmp 0xa033
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0x9fd5
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
shll %eax
addl $0x1, %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x38(%rbp), %rsi
shlq $0x4, %rsi
callq 0x9620
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x9fbc
jmp 0xa02a
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rdx
movl 0x20(%rdx), %eax
movl %eax, %esi
addl $0x1, %esi
movl %esi, 0x20(%rdx)
movl %eax, %eax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rcx
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa03b
movq -0x28(%rbp), %rdi
callq 0x9270
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)