// File: /workspace/github_c_makefile_star2_O1/Tarsnap[P]kivaloo/lbs-s3/s3state.c
// Function: s3state_init
struct s3state *
s3state_init(struct wire_requestqueue * Q_S3, const char * bucket,
    size_t blklen, struct deleteto * D)
{
	struct s3state * S;
	uint64_t L;
	size_t olen;
	int putdone;

	/* Sanity check. */
	assert(blklen <= UINT32_MAX);

	/* Allocate a structure and initialize. */
	if ((S = malloc(sizeof(struct s3state))) == NULL)
		goto err0;
	S->Q_S3 = Q_S3;
	S->D = D;
	S->blklen = (uint32_t)blklen;
	S->npending = 0;

	/* Duplicate bucket name string. */
	if ((S->bucket = strdup(bucket)) == NULL)
		goto err1;

	/* Find the last written (non-empty) object and its size. */
	if (findlast(Q_S3, bucket, &L, &olen))
		goto err2;

	/* If we have no objects stored, just set initial values. */
	if (L == 0) {
		S->lastblk = (uint64_t)(-1);
		S->nextblk = 0;
		goto done;
	}

	/* The object size should be a multiple of the block size. */
	if (olen % blklen != 0) {
		warn0("S3 object size is not a multiple of the block size!");
		goto err2;
	}

	/* Compute the last block #. */
	S->lastblk = (L - 1) * BLKSPEROBJECT + olen / blklen - 1;

	/*
	 * Write an empty object #(L+1): We can't use this object number for
	 * data because a previous LBS-S3 might have issued a PUT for it
	 * before dying, but we need to ensure that the object exists in order
	 * for the FindLast algorithm to work.
	 */
	putdone = 0;
	if (proto_s3_request_put(S->Q_S3, S->bucket,
	    objmap(L + 1), 0, NULL, callback_putdone, &putdone))
		goto err2;
	if (events_spin(&putdone))
		goto err2;

	/* The next block # is the start of object #(L+2). */
	S->nextblk = (L + 1) * BLKSPEROBJECT;

done:
	/* Success! */
	return (S);

err2:
	free(S->bucket);
err1:
	free(S);
err0:
	/* Failure! */
	return (NULL);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdx, %rax
shrq $0x20, %rax
jne 0x3b0e
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movl $0x38, %edi
callq 0x2270
testq %rax, %rax
je 0x3a3c
movq %rax, %rbx
movq %r15, 0x18(%rax)
movq %r14, 0x20(%rax)
movl %r12d, (%rax)
movq $0x0, 0x30(%rax)
movq %r13, %rdi
callq 0x2390
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x3a34
movq %rax, %r14
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x41d8
testl %eax, %eax
je 0x3a4f
movq %r14, %rdi
callq 0x2040
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3a83
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %r12
testq %rdx, %rdx
je 0x3a95
leaq 0x9af7(%rip), %rdi      # 0xd566
xorl %eax, %eax
callq 0x9997
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3a2c
movq $-0x1, 0x10(%rbx)
movq $0x0, 0x8(%rbx)
jmp 0x3a3e
movq %rdi, %rcx
shlq $0x18, %rcx
addq %rcx, %rax
addq $-0x1000001, %rax       # imm = 0xFEFFFFFF
movq %rax, 0x10(%rbx)
leaq 0xc(%rsp), %r12
movl $0x0, (%r12)
incq %rdi
callq 0x4418
movq %r12, (%rsp)
leaq 0x64(%rip), %r9         # 0x3b2d
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x9ec0
testl %eax, %eax
jne 0x3a2c
leaq 0xc(%rsp), %rdi
callq 0x5261
testl %eax, %eax
jne 0x3a2c
movq 0x10(%rsp), %rax
shlq $0x18, %rax
addq $0x1000000, %rax        # imm = 0x1000000
movq %rax, 0x8(%rbx)
jmp 0x3a3e
leaq 0x9982(%rip), %rdi      # 0xd497
leaq 0x99de(%rip), %rsi      # 0xd4fa
leaq 0x99e1(%rip), %rcx      # 0xd504
movl $0x3b, %edx
callq 0x21a0