// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/lbs/storage.c
// Function: storage_init
struct storage_state *
storage_init(const char * storagedir, size_t blocklen, long latency,
    int nosync)
{
	struct storage_state * S;
	struct elasticqueue * files;
	struct storage_file * sf;
	struct file_state fs;
	char * s;
	int rc;
	off_t num_blocks;

	/* Sanity-check the block size. */
	assert(blocklen > 0);
#ifdef OFF_MAX
	assert(blocklen <= OFF_MAX);
#else
	assert(blocklen <= INT32_MAX);
#endif

	/* Allocate structure and fill in static data. */
	if ((S = malloc(sizeof(struct storage_state))) == NULL)
		goto err0;
	S->storagedir = storagedir;
	S->blocklen = blocklen;
	S->latency = latency;
	S->nosync = nosync;

	/*
	 * Figure out the maximum number of blocks a file can contain without
	 * having its length overflow either uint64_t or off_t.
	 */
#ifdef OFF_MAX
	S->maxnblks = UINT64_MAX;
	if (S->maxnblks > OFF_MAX)
		S->maxnblks = OFF_MAX;
#else
	/* Just treat off_t as an int32_t. */
	assert((int32_t)(off_t)(INT32_MAX) == INT32_MAX);
	S->maxnblks = INT32_MAX;
#endif
	S->maxnblks = S->maxnblks / S->blocklen;

	/* Create an elastic queue to hold block file state. */
	if ((S->files = elasticqueue_init(sizeof(struct file_state))) == NULL)
		goto err1;

	/* Get a sorted list of block files. */
	if ((files = storage_findfiles(S->storagedir)) == NULL)
		goto err2;

	/* If we have at least one file, its # is where the blocks start. */
	if (elasticqueue_getlen(files) > 0) {
		sf = elasticqueue_get(files, 0);
		S->minblk = sf->fileno;
	} else {
		/* Otherwise, we have nothing. */
		S->minblk = 0;
	}

	/* Look at files one by one. */
	S->nextblk = S->minblk;
	while (elasticqueue_getlen(files) > 0) {
		/* Grab a file. */
		sf = elasticqueue_get(files, 0);

		/* The first block is the file number. */
		fs.start = sf->fileno;

		/* Is the first block the one we expected? */
		if (fs.start != S->nextblk) {
			warn0("Start of block storage file does not match"
			    " end of previous file: %016" PRIx64, sf->fileno);
			goto err3;
		}

		/* Does it have a non-integer number of blocks? */
		if ((sf->len % (off_t)S->blocklen) != 0) {
			/* Not permitted for files in the middle. */
			if (elasticqueue_getlen(files) > 1) {
				warn0("Block storage file has non-integer"
				    " number of blocks: %016" PRIx64,
				    sf->fileno);
				goto err3;
			}

			/*
			 * The final file may have a non-integer number of
			 * blocks due to an interrupted write; just remove
			 * any partial block.
			 */
			if ((s = storage_util_mkpath(S, sf->fileno)) == NULL)
				goto err3;
			if (truncate(s, sf->len - (sf->len %
			    (off_t)S->blocklen)))
				goto err4;
			free(s);
		}

		/* Compute number of blocks. */
		num_blocks = sf->len / (off_t)S->blocklen;
#if UINTMAX_MAX > UINT64_MAX
		if ((uintmax_t)num_blocks > (uintmax_t)UINT64_MAX)
			goto err3;
#endif
		fs.len = (uint64_t)num_blocks;

		/* Add to the queue of block file state structures. */
		if (elasticqueue_add(S->files, &fs))
			goto err3;

		/* Adjust nextblk to account for this latest block file. */
		S->nextblk = fs.start + fs.len;

		/* Remove the file from the queue. */
		elasticqueue_delete(files);
	}

	/* Free the (now empty) queue of files. */
	elasticqueue_free(files);

	/* Create a lock on the dynamic data. */
	if ((rc = pthread_rwlock_init(&S->lck, NULL)) != 0) {
		warn0("pthread_rwlock_init: %s", strerror(rc));
		goto err2;
	}

	/* Success! */
	return (S);

err4:
	free(s);
err3:
	elasticqueue_free(files);
err2:
	elasticqueue_free(S->files);
err1:
	free(S);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rsi, %rsi
je 0x4e40
movq %rsi, %r14
movq %rsi, %rax
shrq $0x1f, %rax
jne 0x4e5f
movl %ecx, %ebp
movq %rdx, %r15
movq %rdi, %r12
movl $0x78, %edi
callq 0x2380
testq %rax, %rax
je 0x4e2c
movq %rax, %rbx
movq %r12, (%rax)
movq %r14, 0x8(%rax)
movq %r15, 0x18(%rax)
movl %ebp, 0x20(%rax)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
divq %r14
movq %rax, 0x10(%rbx)
movl $0x10, %edi
callq 0x5c50
movq %rax, 0x60(%rbx)
testq %rax, %rax
je 0x4e24
movq (%rbx), %rdi
callq 0x5374
testq %rax, %rax
je 0x4e1b
movq %rax, %r14
movq %rax, %rdi
callq 0x5d7a
testq %rax, %rax
je 0x4cc4
movq %r14, %rdi
xorl %esi, %esi
callq 0x5d7f
movq (%rax), %rax
jmp 0x4cc6
xorl %eax, %eax
movq %rax, 0x68(%rbx)
movq %rax, 0x70(%rbx)
movq %r14, %rdi
callq 0x5d7a
testq %rax, %rax
je 0x4db2
leaq 0x8(%rsp), %r15
movq %r14, %rdi
xorl %esi, %esi
callq 0x5d7f
movq (%rax), %rsi
movq %rsi, 0x8(%rsp)
cmpq 0x70(%rbx), %rsi
jne 0x4df1
movq %rax, %r12
movq 0x8(%rax), %rax
cqto
idivq 0x8(%rbx)
testq %rdx, %rdx
je 0x4d67
movq %r14, %rdi
callq 0x5d7a
movq (%r12), %rsi
cmpq $0x2, %rax
jae 0x4dfa
movq %rbx, %rdi
callq 0x5758
testq %rax, %rax
je 0x4e13
movq %rax, %r13
movq 0x8(%r12), %rsi
movq %rsi, %rax
cqto
idivq 0x8(%rbx)
subq %rdx, %rsi
movq %r13, %rdi
callq 0x2340
movl %eax, %ebp
movq %r13, %rdi
callq 0x2040
testl %ebp, %ebp
jne 0x4e13
movq 0x8(%r12), %rax
cqto
idivq 0x8(%rbx)
movq %rax, 0x10(%rsp)
movq 0x60(%rbx), %rdi
movq %r15, %rsi
callq 0x5cc1
testl %eax, %eax
jne 0x4e13
movq 0x10(%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, 0x70(%rbx)
movq %r14, %rdi
callq 0x5ce9
movq %r14, %rdi
callq 0x5d7a
testq %rax, %rax
jne 0x4ce4
movq %r14, %rdi
callq 0x5d9b
movq %rbx, %rdi
addq $0x28, %rdi
xorl %esi, %esi
callq 0x23d0
testl %eax, %eax
je 0x4e2e
movl %eax, %edi
callq 0x2520
leaq 0x8c89(%rip), %rdi      # 0xda63
movq %rax, %rsi
xorl %eax, %eax
callq 0xad37
callq 0x20a0
movl $0x0, (%rax)
jmp 0x4e1b
leaq 0x8be7(%rip), %rdi      # 0xd9df
jmp 0x4e01
leaq 0x8c26(%rip), %rdi      # 0xda27
xorl %eax, %eax
callq 0xad37
callq 0x20a0
movl $0x0, (%rax)
movq %r14, %rdi
callq 0x5d9b
movq 0x60(%rbx), %rdi
callq 0x5d9b
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8b27(%rip), %rdi      # 0xd96e
leaq 0x8b2d(%rip), %rsi      # 0xd97b
leaq 0x8b30(%rip), %rcx      # 0xd985
movl $0x2d, %edx
callq 0x2220
leaq 0x8b63(%rip), %rdi      # 0xd9c9
leaq 0x8b0e(%rip), %rsi      # 0xd97b
leaq 0x8b11(%rip), %rcx      # 0xd985
movl $0x31, %edx
callq 0x2220