// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x20(%rbp)
ja 0x3e55
jmp 0x3e74
leaq 0x10783(%rip), %rdi     # 0x145df
leaq 0x107df(%rip), %rsi     # 0x14642
movl $0x3b, %edx
leaq 0x107dd(%rip), %rcx     # 0x1464c
callq 0x21c0
movl $0x38, %edi
callq 0x2290
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x3e8d
jmp 0x4011
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x18(%rbp), %rdi
callq 0x23a0
movq -0x30(%rbp), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x3ed9
jmp 0x4008
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x4d30
cmpl $0x0, %eax
je 0x3ef8
jmp 0x3ffb
cmpq $0x0, -0x38(%rbp)
jne 0x3f1c
movq -0x30(%rbp), %rax
movq $-0x1, 0x10(%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x3ff1
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
cmpq $0x0, %rdx
je 0x3f4e
jmp 0x3f30
leaq 0x10777(%rip), %rdi     # 0x146ae
movb $0x0, %al
callq 0xe050
callq 0x20a0
movl $0x0, (%rax)
jmp 0x3ffb
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
shlq $0x18, %rcx
movq -0x40(%rbp), %rax
xorl %edx, %edx
divq -0x20(%rbp)
addq %rax, %rcx
subq $0x1, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
addq $0x1, %rdi
callq 0x5060
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %r8d
leaq 0x7a(%rip), %r9         # 0x4030
leaq -0x44(%rbp), %rax
movq %r8, %rcx
movq %rax, (%rsp)
callq 0xea80
cmpl $0x0, %eax
je 0x3fcd
jmp 0x3ffb
leaq -0x44(%rbp), %rdi
callq 0x6d20
cmpl $0x0, %eax
je 0x3fdd
jmp 0x3ffb
movq -0x38(%rbp), %rcx
addq $0x1, %rcx
shlq $0x18, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4019
movq -0x30(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2040
movq -0x30(%rbp), %rdi
callq 0x2040
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)