// File: /workspace/github_c_makefile_star2_O2/nerves-project[P]nerves_initramfs/src/uboot_env.c
// Function: uboot_env_write
int uboot_env_write(struct uboot_env *env, char *buffer)
{
    if (env->env_size < 8)
        ERR_RETURN("u-boot environment block size too small");

    // U-boot environment blocks are filled by 0xff by default
    memset(buffer, 0xff, env->env_size);

    // Skip over the CRC until the end.
    char *p = buffer + 4;
    char *end = buffer + env->env_size - 2;

    // Sort the name/value pairs so that their ordering is
    // deterministic.
    uboot_env_sort(env);

    // Add all of the name/value pairs.
    struct uboot_name_value *pair;
    for (pair = env->vars; pair != NULL; pair = pair->next) {
        size_t namelen = strlen(pair->name);
        size_t valuelen = strlen(pair->value);
        if (p + namelen + 1 + valuelen >= end)
            ERR_RETURN("Not enough room in U-boot environment");

        memcpy(p, pair->name, namelen);
        p += namelen;
        *p = '=';
        p++;
        memcpy(p, pair->value, valuelen);
        p += valuelen;
        *p = 0;
        p++;
    }

    // Add the extra NULL byte on the end.
    *p = 0;

    // Calculate and add the CRC-32
    uint32_t crc32 = crc32buf(buffer + 4, env->env_size - 4);
    buffer[0] = crc32 & 0xff;
    buffer[1] = (crc32 >> 8) & 0xff;
    buffer[2] = (crc32 >> 16) & 0xff;
    buffer[3] = crc32 >> 24;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq (%rdi), %rdx
cmpq $0x7, %rdx
ja 0x345c
leaq 0x544c(%rip), %rdi      # 0x88a3
jmp 0x35d4
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
movl $0xff, %esi
callq 0x2240
movq (%rbx), %rax
addq %r14, %rax
movq %rax, (%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x8(%rbx), %rbp
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbp, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x34a0
addq $0x10, %rax
decq %r12
addq $0x8, %r15
jmp 0x348b
testl %r12d, %r12d
je 0x351c
movq %r12, %rbx
notq %rbx
negq %r12
movq %r15, %rdi
callq 0x2380
movq %rax, %r13
movq %rbp, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x34d1
movq %rcx, (%rax)
addq $0x10, %rcx
addq $0x8, %rax
jmp 0x34bc
leaq 0x115(%rip), %rcx       # 0x35ed
pushq $0x8
popq %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2100
movl %ebx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3502
movq (%r13,%rcx,8), %rdx
movq 0x8(%r13,%rcx,8), %rsi
incq %rcx
movq %rsi, 0x10(%rdx)
jmp 0x34ea
movq -0x8(%r13,%r15), %rax
andq $0x0, 0x10(%rax)
movq (%r13), %rax
movq %rax, (%rbp)
movq %r13, %rdi
callq 0x2040
movq %r14, 0x18(%rsp)
addq $0x4, %r14
addq $-0x2, (%rsp)
movq %r14, 0x8(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x35a8
movq (%rbp), %r15
movq %r15, %rdi
callq 0x2190
movq %rax, %rbx
movq 0x8(%rbp), %rdi
callq 0x2190
movq %rax, %r13
leaq (%r14,%rbx), %rax
leaq (%rax,%r13), %r12
incq %r12
cmpq (%rsp), %r12
jae 0x35cd
leaq (%r14,%rbx), %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2350
movb $0x3d, (%r14,%rbx)
movq 0x8(%rbp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x2350
movb $0x0, (%rbx,%r13)
incq %r12
addq $0x10, %rbp
movq %r12, %r14
jmp 0x352f
movb $0x0, (%r14)
movq 0x10(%rsp), %rax
movq (%rax), %rsi
addq $-0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x3150
movq 0x18(%rsp), %rcx
movl %eax, (%rcx)
xorl %eax, %eax
jmp 0x35de
leaq 0x52f7(%rip), %rdi      # 0x88cb
xorl %eax, %eax
callq 0x2ee4
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq