// File: /workspace/github_c_makefile_star2_O0/nerves-project[P]nerves_initramfs/src/block_device.c
// Function: probe_mbr_devices
static int probe_mbr_devices(int fd, const char *devname, struct block_device_info **devices)
{
    uint8_t mbr[512];
    if (pread(fd, mbr, sizeof(mbr), 0) < 0)
        return -1;

    // Check for MBR signature
    if (mbr[510] != 0x55 || mbr[511] != 0xaa)
        return -1;

    // Check for GPT
    // First partition has type 0xee
    if (mbr[446 + 4] == 0xee)
        return -1;

    // Capture the disk UUID since MBR partitions don't have UUIDs
    uint32_t disk_uuid = from_le32(&mbr[440]);

    // 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);
    snprintf(blkdev->uuid, sizeof(blkdev->uuid), "%08x", disk_uuid);
    *devices = blkdev;

    // Enumerate the primary partitions
    const char *p = p_or_np(devname);
    const uint8_t *partition = &mbr[446 + 4*16];
    for (int i = 4; i > 0; i--) {
        // If non-empty partition
        partition -= 16;
        if (partition[4] != 0) {
            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);
            snprintf(blkdev->uuid, sizeof(blkdev->uuid), "%08x-%02x", disk_uuid, i);
            *devices = blkdev;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
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 0xaa80
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xac61
movzbl -0x22(%rbp), %eax
cmpl $0x55, %eax
jne 0xaa94
movzbl -0x21(%rbp), %eax
cmpl $0xaa, %eax
je 0xaaa0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xac61
movzbl -0x5e(%rbp), %eax
cmpl $0xee, %eax
jne 0xaab7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xac61
leaq -0x220(%rbp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
callq 0xaff0
movl %eax, -0x224(%rbp)
callq 0xb0a0
movq %rax, -0x230(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x230(%rbp), %rax
movq %rcx, (%rax)
movq -0x230(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x230(%rbp), %rdi
addq $0xc, %rdi
movq -0x10(%rbp), %rcx
movl $0x20, %esi
leaq 0x30b5(%rip), %rdx      # 0xdbcb
movb $0x0, %al
callq 0x21f0
movq -0x230(%rbp), %rdi
addq $0x2c, %rdi
movl -0x224(%rbp), %ecx
movl $0x30, %esi
leaq 0x309c(%rip), %rdx      # 0xdbd6
movb $0x0, %al
callq 0x21f0
movq -0x230(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
callq 0xb030
movq %rax, -0x238(%rbp)
leaq -0x220(%rbp), %rax
addq $0x1fe, %rax            # imm = 0x1FE
movq %rax, -0x240(%rbp)
movl $0x4, -0x244(%rbp)
cmpl $0x0, -0x244(%rbp)
jle 0xac5a
movq -0x240(%rbp), %rax
addq $-0x10, %rax
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
je 0xac44
callq 0xb0a0
movq %rax, -0x250(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x250(%rbp), %rax
movq %rcx, (%rax)
movq -0x250(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x250(%rbp), %rdi
addq $0xc, %rdi
movq -0x10(%rbp), %rcx
movq -0x238(%rbp), %r8
movl -0x244(%rbp), %r9d
movl $0x20, %esi
leaq 0x2fd7(%rip), %rdx      # 0xdbdb
movb $0x0, %al
callq 0x21f0
movq -0x250(%rbp), %rdi
addq $0x2c, %rdi
movl -0x224(%rbp), %ecx
movl -0x244(%rbp), %r8d
movl $0x30, %esi
leaq 0x2fbb(%rip), %rdx      # 0xdbea
movb $0x0, %al
callq 0x21f0
movq -0x250(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0xac46
movl -0x244(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0xab7d
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
nopl (%rax)