// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x8, (%rax)
jae 0x3bc6
jmp 0x3bac
leaq 0x8cc0(%rip), %rdi      # 0xc873
movb $0x0, %al
callq 0x32a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d55
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl $0xff, %esi
callq 0x2210
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
addq (%rcx), %rax
addq $-0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3d60
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3ce5
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x2170
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2170
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
addq -0x38(%rbp), %rax
addq $0x1, %rax
addq -0x40(%rbp), %rax
cmpq -0x28(%rbp), %rax
jb 0x3c6d
jmp 0x3c53
leaq 0x8c41(%rip), %rdi      # 0xc89b
movb $0x0, %al
callq 0x32a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3d55
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rdx
callq 0x2300
movq -0x38(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x3d, (%rax)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rdx
callq 0x2300
movq -0x40(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x3c0f
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
subq $0x4, %rsi
callq 0x3640
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, (%rax)
movl -0x44(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x1(%rax)
movl -0x44(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x44(%rbp), %eax
shrl $0x18, %eax
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x3(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop