// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]kivaloo/liball/../lib/dynamodb/dynamodb_kv.c
// Function: dynamodb_kv_icas
char *
dynamodb_kv_icas(const char * table, const char * key,
    const uint8_t * buf, size_t len, const uint8_t * buf2, size_t len2)
{
	/**
	 * An ICAS is a PutItem request with a body (with spaces added) of:
	 * { "TableName": "TABLE",
	 *   "Item": {
	 *     "K": { "S": "KEY" },
	 *     "V": { "B": "BASE64NEW" }
	 *   },
	 *   "ConditionExpression": "(V = :old) OR (V = :new)",
	 *   "ExpressionAttributeValues": {
	 *       ":old": { "B": "BASE64OLD" },
	 *       ":new": { "B": "BASE64NEW" }
	 *   },
	 *   "ReturnConsumedCapacity": "TOTAL"
	 * }
	 */
	const char * s1 = "{\"TableName\":\"";
	const char * s2 = "\",\"Item\":{\"K\":{\"S\":\"";
	const char * s3 = "\"},\"V\":{\"B\":\"";
	const char * s4 = "\"}},\"ConditionExpression\":\"(V=:old)OR(V=:new)\",";
	const char * s5 = "\"ExpressionAttributeValues\":{\":old\":{\"B\":\"";
	const char * s6 = "\"},\":new\":{\"B\":\"";
	const char * s7 = "\"}},\"ReturnConsumedCapacity\":\"TOTAL\"}";
	size_t slen = strlen(s1) + strlen(table) + strlen(s2) + strlen(key) +
	    strlen(s3) + b64len(len2) + strlen(s4) + strlen(s5) +
	    b64len(len) + strlen(s6) + b64len(len2) + strlen(s7);
	size_t spos = 0;
	char * s;

	/* Allocate request string. */
	if ((s = malloc(slen + 1)) == NULL)
		goto done;

	/* Construct the request, piece by piece. */
	COPYANDINCR(s, spos, s1);
	COPYANDINCR(s, spos, table);
	COPYANDINCR(s, spos, s2);
	COPYANDINCR(s, spos, key);
	COPYANDINCR(s, spos, s3);
	b64encode(buf2, &s[spos], len2);
	spos += b64len(len2);
	COPYANDINCR(s, spos, s4);
	COPYANDINCR(s, spos, s5);
	b64encode(buf, &s[spos], len);
	spos += b64len(len);
	COPYANDINCR(s, spos, s6);
	b64encode(buf2, &s[spos], len2);
	spos += b64len(len2);
	COPYANDINCR(s, spos, s7);
	s[spos] = '\0';

	/* Check that we got the buffer size right. */
	assert(slen == spos);

done:
	/* Return string (or NULL if allocation failed). */
	return (s);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, 0x30(%rsp)
movq %rcx, %r13
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
callq 0x3190
movq %rax, %r15
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0x3190
movq %rax, 0x10(%rsp)
leaq (%r15,%rax), %r14
addq $0x22, %r14
leaq 0x2(%rbx), %rax
pushq $0x3
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r12
movq %r13, 0x28(%rsp)
leaq 0x2(%r13), %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r13
leaq (,%r12,8), %rax
leaq (%rax,%r13,4), %rax
leaq (%rax,%r14), %rdi
addq $0x9c, %rdi
callq 0x34d0
movq %rax, %rbp
testq %rax, %rax
je 0x59fc
movq %rbx, (%rsp)
movq %r15, %rbx
addq $0x22, %rbx
shlq $0x2, %r13
movabsq $0x223a22656d614e65, %rax # imm = 0x223A22656D614E65
movq %rax, 0x6(%rbp)
movabsq $0x4e656c626154227b, %rax # imm = 0x4E656C626154227B
movq %rax, (%rbp)
leaq 0xe(%rbp), %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x3290
movups 0xacb3(%rip), %xmm0     # 0x10594
movups %xmm0, 0xe(%rbp,%r15)
movl $0x223a2253, 0x1e(%rbp,%r15) # imm = 0x223A2253
addq %rbp, %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x3290
movabsq $0x7b3a2256222c7d22, %rax # imm = 0x7B3A2256222C7D22
movq %rax, (%rbp,%r14)
movabsq $0x223a2242227b3a22, %rax # imm = 0x223A2242227B3A22
movq %rax, 0x5(%rbp,%r14)
leaq 0xd(%rbp,%r14), %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq (%rsp), %rdx
callq 0xa614
leaq (%r14,%r12,4), %rbx
addq $0xd, %rbx
movups 0xacb4(%rip), %xmm0     # 0x105fc
movups %xmm0, 0x1f(%rbp,%rbx)
movups 0xac99(%rip), %xmm0     # 0x105ed
movups %xmm0, 0x10(%rbp,%rbx)
movups 0xac7d(%rip), %xmm0     # 0x105dd
movups %xmm0, (%rbp,%rbx)
movups 0xaca1(%rip), %xmm0     # 0x1060d
movups %xmm0, 0x2f(%rbp,%rbx)
movups 0xaca5(%rip), %xmm0     # 0x1061d
movups %xmm0, 0x3f(%rbp,%rbx)
movups 0xaca3(%rip), %xmm0     # 0x10627
movups %xmm0, 0x49(%rbp,%rbx)
leaq (%rbx,%rbp), %rsi
addq $0x59, %rsi
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
callq 0xa614
leaq (%rbx,%r13), %rax
addq $0x59, %rax
leaq (%rbp,%rax), %rbx
movups 0xac84(%rip), %xmm0     # 0x10638
movups %xmm0, (%rbp,%rax)
leaq (%rax,%rbp), %rsi
addq $0x10, %rsi
movq %r15, %rdi
movq (%rsp), %rdx
callq 0xa614
movabsq $0x7d224c41544f5422, %rax # imm = 0x7D224C41544F5422
movq %rax, 0x2d(%rbx,%r12,4)
movups 0xabe4(%rip), %xmm0     # 0x105c7
movups %xmm0, 0x20(%rbx,%r12,4)
movups 0xabc7(%rip), %xmm0     # 0x105b7
movups %xmm0, 0x10(%rbx,%r12,4)
movb $0x0, 0x35(%rbx,%r12,4)
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq