// File: /workspace/github_c_makefile_star2_O0/nerves-project[P]nerves_initramfs/src/block_device.c
// Function: probe_gpt_devices
static int probe_gpt_devices(int fd, const char *devname, struct block_device_info **devices)
{
    uint8_t block[512];

    // Check for protective MBR
    if (pread(fd, block, sizeof(block), 0) < 0)
        return -1;
    if (block[510] != 0x55 || block[511] != 0xaa || block[446 + 4] != 0xee)
        return -1;

    // Check GPT header
    if (pread(fd, block, sizeof(block), 512) < 0)
        return -1;

    if (block[0] != 'E' ||
        block[1] != 'F' ||
        block[2] != 'I' ||
        block[3] != ' ' ||
        block[4] != 'P' ||
        block[5] != 'A' ||
        block[6] != 'R' ||
        block[7] != 'T')
        return -1;

    // Add the disk
    struct block_device_info *blkdev = alloc_blkdev();
    blkdev->next = *devices;
    blkdev->type = BLOCK_DEVICE_DISK;
    snprintf(blkdev->path, sizeof(blkdev->path), "/dev/%.16s", devname);
    uuid_to_string_me(&block[56], blkdev->uuid);
    *devices = blkdev;

    // Load the partition table
    uint32_t partition_count = from_le32(&block[80]);
    if (partition_count > 256)
        return -1;

    uint32_t partition_size = from_le32(&block[84]);
    if (partition_count > 256)
        return -1;

    uint32_t partition_table_size = partition_count * partition_size;
    uint8_t *partitions = malloc(partition_count * partition_size);

    if (pread(fd, partitions, partition_table_size, 512 * 2) < 0) {
        free(partitions);
        return -1;
    }

    const char *p = p_or_np(devname);
    uint8_t *partition = partitions + partition_size * partition_count;
    for (uint32_t i = partition_count; i > 0; i--) {
        partition -= partition_size;

        if (!is_zeros(partition, 16)) {
            struct block_device_info *blkdev = alloc_blkdev();
            blkdev->next = *devices;
            blkdev->type = BLOCK_DEVICE_PARTITION;
            snprintf(blkdev->path, sizeof(blkdev->path), "/dev/%.16s%s%d", devname, p, i);
            uuid_to_string_me(&partition[16], blkdev->uuid);
            *devices = blkdev;
        }
    }

    free(partitions);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
xorl %eax, %eax
movl %eax, %ecx
callq 0x2430
cmpq $0x0, %rax
jge 0xacb0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
movzbl -0x22(%rbp), %eax
cmpl $0x55, %eax
jne 0xaccf
movzbl -0x21(%rbp), %eax
cmpl $0xaa, %eax
jne 0xaccf
movzbl -0x5e(%rbp), %eax
cmpl $0xee, %eax
je 0xacdb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
movl -0x8(%rbp), %edi
leaq -0x220(%rbp), %rsi
movl $0x200, %ecx            # imm = 0x200
movq %rcx, %rdx
callq 0x2430
cmpq $0x0, %rax
jge 0xad04
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
movzbl -0x220(%rbp), %eax
cmpl $0x45, %eax
jne 0xad64
movzbl -0x21f(%rbp), %eax
cmpl $0x46, %eax
jne 0xad64
movzbl -0x21e(%rbp), %eax
cmpl $0x49, %eax
jne 0xad64
movzbl -0x21d(%rbp), %eax
cmpl $0x20, %eax
jne 0xad64
movzbl -0x21c(%rbp), %eax
cmpl $0x50, %eax
jne 0xad64
movzbl -0x21b(%rbp), %eax
cmpl $0x41, %eax
jne 0xad64
movzbl -0x21a(%rbp), %eax
cmpl $0x52, %eax
jne 0xad64
movzbl -0x219(%rbp), %eax
cmpl $0x54, %eax
je 0xad70
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
callq 0xb0a0
movq %rax, -0x228(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x228(%rbp), %rax
movq %rcx, (%rax)
movq -0x228(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x228(%rbp), %rdi
addq $0xc, %rdi
movq -0x10(%rbp), %rcx
movl $0x20, %esi
leaq 0x2e15(%rip), %rdx      # 0xdbcb
movb $0x0, %al
callq 0x21f0
leaq -0x220(%rbp), %rdi
addq $0x38, %rdi
movq -0x228(%rbp), %rsi
addq $0x2c, %rsi
callq 0xb0d0
movq -0x228(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
leaq -0x220(%rbp), %rdi
addq $0x50, %rdi
callq 0xaff0
movl %eax, -0x22c(%rbp)
cmpl $0x100, -0x22c(%rbp)    # imm = 0x100
jbe 0xae14
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
leaq -0x220(%rbp), %rdi
addq $0x54, %rdi
callq 0xaff0
movl %eax, -0x230(%rbp)
cmpl $0x100, -0x22c(%rbp)    # imm = 0x100
jbe 0xae42
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
movl -0x22c(%rbp), %eax
imull -0x230(%rbp), %eax
movl %eax, -0x234(%rbp)
movl -0x22c(%rbp), %eax
imull -0x230(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
callq 0x2330
movq %rax, -0x240(%rbp)
movl -0x8(%rbp), %edi
movq -0x240(%rbp), %rsi
movl -0x234(%rbp), %eax
movl %eax, %edx
movl $0x400, %ecx            # imm = 0x400
callq 0x2430
cmpq $0x0, %rax
jge 0xaeac
movq -0x240(%rbp), %rdi
callq 0x2040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xafdb
movq -0x10(%rbp), %rdi
callq 0xb030
movq %rax, -0x248(%rbp)
movq -0x240(%rbp), %rax
movl -0x230(%rbp), %ecx
imull -0x22c(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x250(%rbp)
movl -0x22c(%rbp), %eax
movl %eax, -0x254(%rbp)
cmpl $0x0, -0x254(%rbp)
jbe 0xafc8
movl -0x230(%rbp), %ecx
movq -0x250(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x250(%rbp)
movq -0x250(%rbp), %rdi
movl $0x10, %esi
callq 0xb1f0
testb $0x1, %al
jne 0xafb2
callq 0xb0a0
movq %rax, -0x260(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x260(%rbp), %rax
movq %rcx, (%rax)
movq -0x260(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x260(%rbp), %rdi
addq $0xc, %rdi
movq -0x10(%rbp), %rcx
movq -0x248(%rbp), %r8
movl -0x254(%rbp), %r9d
movl $0x20, %esi
leaq 0x2c59(%rip), %rdx      # 0xdbdb
movb $0x0, %al
callq 0x21f0
movq -0x250(%rbp), %rdi
addq $0x10, %rdi
movq -0x260(%rbp), %rsi
addq $0x2c, %rsi
callq 0xb0d0
movq -0x260(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0xafb4
movl -0x254(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0xaee8
movq -0x240(%rbp), %rdi
callq 0x2040
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw (%rax,%rax)