// File: /workspace/github_c_makefile_star2_O3/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 0x3af2
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movl $0x38, %edi
callq 0x2270
testq %rax, %rax
je 0x3a20
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 0x3a18
movq %rax, %r14
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x41c0
testl %eax, %eax
je 0x3a33
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 0x3a67
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %r12
testq %rdx, %rdx
je 0x3a79
leaq 0x9a9f(%rip), %rdi      # 0xd4f2
xorl %eax, %eax
callq 0x99df
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3a10
movq $-0x1, 0x10(%rbx)
movq $0x0, 0x8(%rbx)
jmp 0x3a22
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 0x4400
movq %r12, (%rsp)
leaq 0x64(%rip), %r9         # 0x3b11
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x9f04
testl %eax, %eax
jne 0x3a10
leaq 0xc(%rsp), %rdi
callq 0x51fd
testl %eax, %eax
jne 0x3a10
movq 0x10(%rsp), %rax
shlq $0x18, %rax
addq $0x1000000, %rax        # imm = 0x1000000
movq %rax, 0x8(%rbx)
jmp 0x3a22
leaq 0x992a(%rip), %rdi      # 0xd423
leaq 0x9986(%rip), %rsi      # 0xd486
leaq 0x9989(%rip), %rcx      # 0xd490
movl $0x3b, %edx
callq 0x21a0