// File: /workspace/github_c_makefile_star2_O1/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 0x383e
leaq 0x6076(%rip), %rdi      # 0x98a3
xorl %eax, %eax
callq 0x3270
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x39eb
movq %rsi, %r15
movq %rdi, %rbp
movq %rsi, %rdi
movl $0xff, %esi
callq 0x2240
movq 0x8(%rbp), %r13
xorl %r14d, %r14d
testq %r13, %r13
je 0x386c
movq %r13, %rax
incl %r14d
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x3860
movq (%rbp), %rbx
testl %r14d, %r14d
movq %r15, (%rsp)
je 0x3907
movl %r14d, %r12d
leaq (,%r12,8), %rdi
callq 0x2370
movq %rax, %r15
testq %r13, %r13
je 0x38a8
movq %r15, %rax
movq %r13, (%rax)
movq 0x10(%r13), %r13
addq $0x8, %rax
testq %r13, %r13
jne 0x3898
leaq 0x14b(%rip), %rcx       # 0x39fa
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2100
leal -0x1(%r14), %eax
cmpl $0x2, %r14d
jl 0x38e6
movl %eax, %ecx
xorl %edx, %edx
movq (%r15,%rdx,8), %rsi
movq 0x8(%r15,%rdx,8), %rdi
leaq 0x1(%rdx), %r8
movq %rdi, 0x10(%rsi)
movq %r8, %rdx
cmpq %r8, %rcx
jne 0x38cd
cltq
movq (%r15,%rax,8), %rax
movq $0x0, 0x10(%rax)
movq (%r15), %rax
movq %rax, 0x8(%rbp)
movq %r15, %rdi
callq 0x2040
movq (%rsp), %r15
leaq 0x4(%r15), %r13
movq %rbp, 0x18(%rsp)
movq 0x8(%rbp), %r14
movq %r13, 0x8(%rsp)
testq %r14, %r14
je 0x39c8
movq (%rsp), %rax
addq %rbx, %rax
addq $-0x2, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %r13
movq (%r14), %rbx
movq %rbx, %rdi
callq 0x2190
movq %rax, %r15
movq 0x8(%r14), %rdi
callq 0x2190
movq %rax, %rbp
leaq (%r15,%r13), %rax
leaq (%rax,%rbp), %r12
incq %r12
cmpq 0x10(%rsp), %r12
jae 0x39a2
leaq (%r15,%r13), %rax
incq %rax
movq %rax, 0x20(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2340
movb $0x3d, (%r13,%r15)
movq 0x8(%r14), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x2340
movb $0x0, (%rbx,%rbp)
leaq 0x1(%r12), %r13
jmp 0x39b0
leaq 0x5f22(%rip), %rdi      # 0x98cb
xorl %eax, %eax
callq 0x3270
cmpq 0x10(%rsp), %r12
jae 0x3834
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x3937
movb $0x0, (%r13)
movq 0x18(%rsp), %rax
movq (%rax), %rsi
addq $-0x4, %rsi
movq 0x8(%rsp), %rdi
callq 0x34e8
movq (%rsp), %rcx
movl %eax, (%rcx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq