// File: /workspace/github_c_makefile_star2_O2/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 0x353d
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
pushq $0x38
popq %rdi
callq 0x2270
testq %rax, %rax
je 0x3477
movq %rax, %rbx
movq %r15, 0x18(%rax)
movq %r14, 0x20(%rax)
movl %r12d, (%rax)
andq $0x0, 0x30(%rax)
movq %r13, %rdi
callq 0x2390
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x346f
movq %rax, %r14
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3bc8
testl %eax, %eax
je 0x348a
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 0x34bb
movq 0x18(%rsp), %rax
xorl %edx, %edx
divq %r12
testq %rdx, %rdx
je 0x34c7
leaq 0x9048(%rip), %rdi      # 0xc4f2
xorl %eax, %eax
callq 0x8cc0
callq 0x20a0
andl $0x0, (%rax)
jmp 0x3467
orq $-0x1, 0x10(%rbx)
andq $0x0, 0x8(%rbx)
jmp 0x3479
movq %rdi, %rcx
shlq $0x18, %rcx
addq %rcx, %rax
addq $-0x1000001, %rax       # imm = 0xFEFFFFFF
movq %rax, 0x10(%rbx)
leaq 0xc(%rsp), %r12
andl $0x0, (%r12)
incq %rdi
callq 0x3de0
movq %r12, (%rsp)
leaq 0x62(%rip), %r9         # 0x355a
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x91a8
testl %eax, %eax
jne 0x3467
leaq 0xc(%rsp), %rdi
callq 0x4bef
testl %eax, %eax
jne 0x3467
movq 0x10(%rsp), %rax
shlq $0x18, %rax
addq $0x1000000, %rax        # imm = 0x1000000
movq %rax, 0x8(%rbx)
jmp 0x3479
leaq 0x8edf(%rip), %rdi      # 0xc423
leaq 0x8f3b(%rip), %rsi      # 0xc486
leaq 0x8f3e(%rip), %rcx      # 0xc490
pushq $0x3b
popq %rdx
callq 0x21a0