// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/deps/jemalloc/src/chunk_dss.c
// Function: chunk_alloc_dss
void *
chunk_alloc_dss(size_t size, size_t alignment, bool *zero)
{
	void *ret;

	cassert(config_dss);
	assert(size > 0 && (size & chunksize_mask) == 0);
	assert(alignment > 0 && (alignment & chunksize_mask) == 0);

	/*
	 * sbrk() uses a signed increment argument, so take care not to
	 * interpret a huge allocation request as a negative increment.
	 */
	if ((intptr_t)size < 0)
		return (NULL);

	malloc_mutex_lock(&dss_mtx);
	if (dss_prev != (void *)-1) {
		size_t gap_size, cpad_size;
		void *cpad, *dss_next;
		intptr_t incr;

		/*
		 * The loop is necessary to recover from races with other
		 * threads that are using the DSS for something other than
		 * malloc.
		 */
		do {
			/* Get the current end of the DSS. */
			dss_max = sbrk(0);
			/*
			 * Calculate how much padding is necessary to
			 * chunk-align the end of the DSS.
			 */
			gap_size = (chunksize - CHUNK_ADDR2OFFSET(dss_max)) &
			    chunksize_mask;
			/*
			 * Compute how much chunk-aligned pad space (if any) is
			 * necessary to satisfy alignment.  This space can be
			 * recycled for later use.
			 */
			cpad = (void *)((uintptr_t)dss_max + gap_size);
			ret = (void *)ALIGNMENT_CEILING((uintptr_t)dss_max,
			    alignment);
			cpad_size = (uintptr_t)ret - (uintptr_t)cpad;
			dss_next = (void *)((uintptr_t)ret + size);
			if ((uintptr_t)ret < (uintptr_t)dss_max ||
			    (uintptr_t)dss_next < (uintptr_t)dss_max) {
				/* Wrap-around. */
				malloc_mutex_unlock(&dss_mtx);
				return (NULL);
			}
			incr = gap_size + cpad_size + size;
			dss_prev = sbrk(incr);
			if (dss_prev == dss_max) {
				/* Success. */
				dss_max = dss_next;
				malloc_mutex_unlock(&dss_mtx);
				if (cpad_size != 0)
					chunk_unmap(cpad, cpad_size);
				if (*zero) {
					VALGRIND_MAKE_MEM_UNDEFINED(ret, size);
					memset(ret, 0, size);
				}
				return (ret);
			}
		} while (dss_prev != (void *)-1);
	}
	malloc_mutex_unlock(&dss_mtx);

	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
testq %rdi, %rdi
js 0x85aa7
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
leaq 0x59fdb(%rip), %rdi     # 0xdf938
callq 0x1faa0
cmpq $-0x1, 0x59ff6(%rip)    # 0xdf960
je 0x85a9b
movq %rbx, 0x8(%rsp)
leaq -0x1(%r12), %rax
movq %rax, 0x10(%rsp)
negq %r12
xorl %edi, %edi
callq 0x1f3e0
movq %rax, %r13
movq %rax, 0x59fd5(%rip)     # 0xdf968
movq 0x10(%rsp), %rax
leaq (%rax,%r13), %rbx
andq %r12, %rbx
cmpq %r13, %rbx
jb 0x859ff
leaq (%rbx,%r14), %r15
cmpq %r13, %r15
jb 0x859ff
leaq 0x59eac(%rip), %rax     # 0xdf860
movq (%rax), %rdi
leaq 0x59ec2(%rip), %rax     # 0xdf880
movq (%rax), %rax
movq %rax, %rcx
andq %r13, %rcx
subq %rcx, %rdi
andq %rax, %rdi
addq %rdi, %r13
movq %rbx, %rbp
subq %r13, %rbp
addq %r14, %rdi
addq %rbp, %rdi
callq 0x1f3e0
movq %rax, 0x59f78(%rip)     # 0xdf960
cmpq 0x59f79(%rip), %rax     # 0xdf968
je 0x85a15
cmpq $-0x1, %rax
jne 0x85982
movb $0x1, %r15b
jmp 0x85a96
leaq 0x59f32(%rip), %rdi     # 0xdf938
callq 0x1fbb0
xorl %r15d, %r15d
xorl %ebx, %ebx
jmp 0x85a96
movq %r15, 0x59f4c(%rip)     # 0xdf968
leaq 0x59f15(%rip), %rdi     # 0xdf938
callq 0x1fbb0
cmpq %r13, %rbx
je 0x85a38
movq %r13, %rdi
movq %rbp, %rsi
callq 0x85687
movq 0x8(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x85a93
leaq 0x20(%rsp), %rax
movq $0x4d430001, (%rax)     # imm = 0x4D430001
movq %rbx, 0x8(%rax)
movq %r14, 0x10(%rax)
xorl %r15d, %r15d
movq %r15, 0x18(%rax)
movq %r15, 0x20(%rax)
movq %r15, 0x28(%rax)
xorl %edx, %edx
rolq $0x3, %rdi
rolq $0xd, %rdi
rolq $0x3d, %rdi
rolq $0x33, %rdi
xchgq %rbx, %rbx
movq %rdx, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1f110
jmp 0x85a96
xorl %r15d, %r15d
testb %r15b, %r15b
je 0x85aa9
leaq 0x59e96(%rip), %rdi     # 0xdf938
callq 0x1fbb0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq