// File: /workspace/github_c_makefile_star2_O3/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 0x37c6
leaq 0x60ee(%rip), %rdi      # 0x98a3
xorl %eax, %eax
callq 0x3204
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3948
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
movl $0xff, %esi
callq 0x2240
movq %rbx, 0x10(%rsp)
leaq 0x4(%rbx), %r12
movq %r14, 0x8(%rsp)
movq 0x8(%r14), %r14
movq %r12, (%rsp)
testq %r14, %r14
je 0x3925
movq 0x8(%rsp), %r15
movq (%r15), %rax
movq 0x10(%rsp), %rcx
addq %rcx, %rax
addq $-0x2, %rax
movq %rax, 0x18(%rsp)
movq $-0x1, %rbx
xorl %edi, %edi
movq %r14, %rax
movq 0x10(%rax), %rax
incq %rbx
addq $0x8, %rdi
testq %rax, %rax
jne 0x381d
leaq 0x1(%rbx), %r13
callq 0x2380
movq %rax, %r12
movq %r14, (%rax)
movq 0x10(%r14), %r14
addq $0x8, %rax
testq %r14, %r14
jne 0x3839
leaq 0x113(%rip), %rcx       # 0x3963
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x2100
testl %ebx, %ebx
je 0x3887
movq (%r12), %rdx
movq %r12, %rax
addq $0x8, %rax
movq %rbx, %rcx
movq (%rax), %rsi
movq %rsi, 0x10(%rdx)
addq $0x8, %rax
movq %rsi, %rdx
decq %rcx
jne 0x3872
jmp 0x3889
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rax
movq $0x0, 0x10(%rax)
movq (%r12), %r14
movq %r14, 0x8(%r15)
movq %r12, %rdi
callq 0x2040
movq (%rsp), %r12
testq %r14, %r14
je 0x3925
movq (%rsp), %rbx
movq (%r14), %rbp
movq %rbp, %rdi
callq 0x2190
movq %rax, %r15
movq 0x8(%r14), %rdi
callq 0x2190
movq %rax, %r13
leaq (%rbx,%r15), %rax
leaq (%rax,%r13), %r12
incq %r12
cmpq 0x18(%rsp), %r12
jae 0x3957
leaq (%rbx,%r15), %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2350
movb $0x3d, (%rbx,%r15)
movq 0x8(%r14), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x2350
movb $0x0, (%rbx,%r13)
incq %r12
movq 0x10(%r14), %r14
movq %r12, %rbx
testq %r14, %r14
jne 0x38b2
movb $0x0, (%r12)
movq 0x8(%rsp), %rax
movq (%rax), %rsi
addq $-0x4, %rsi
movq (%rsp), %rdi
callq 0x3474
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5f6d(%rip), %rdi      # 0x98cb
jmp 0x37b5