// File: /workspace/github_c_makefile_star2_O0/sTeeLM[P]dwipe/device.c
// Function: dwipe_device_scan
int dwipe_device_scan( char*** device_names )
{
    /**
     * Scans the the filesystem for storage device names.
     *
     * @parameter device_names  A reference to a null array pointer.
     * @modifies  device_names  Populates device_names with an array of strings.
     * @returns                 The number of strings in the device_names array.
     *
     */

    /* The partitions file pointer.  Usually '/proc/partitions'. */
    FILE* fp;

    /* The input buffer. */
    char b [FILENAME_MAX];

    /* Buffer for the major device number. */
    int dmajor;

    /* Buffer for the minor device number. */
    int dminor;

    /* Buffer for the device block count.  */
    loff_t dblocks;

    /* Buffer for the device file name.    */
    char dname [FILENAME_MAX];

    /* Names in the partition file do not have the '/dev/' prefix. */
    char dprefix [] = DWIPE_KNOB_PARTITIONS_PREFIX;

    /* The number of devices that have been found. */
    int dcount = 0;
 
    /* Open the partitions file. */
    fp = fopen( DWIPE_KNOB_PARTITIONS, "r" );

    if( fp == NULL )
    {
        perror( "dwipe_device_scan: fopen" );
        dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to open the partitions file '%s'.\n", DWIPE_KNOB_PARTITIONS );
        exit( errno );
    }

    /* Copy the device prefix into the name buffer. */
    strcpy( dname, dprefix );

    /* Sanity check: If device_name is non-null, then it is probably being used. */
    if( *device_names != NULL )
    {
        dwipe_log( DWIPE_LOG_ERROR, "Sanity Error: dwipe_device_scan: Non-null device_names pointer.\n" );
        exit( -1 );
    }

    /* Read every line in the partitions file. */
    while( fgets( b, sizeof( b ), fp ) != NULL )
    {
        /* Scan for a device line. */
        if( sscanf( b, "%i %i %lli %s", &dmajor, &dminor, &dblocks, &dname[ strlen( dprefix ) ] ) == 4 )
        {
            /* Increment the device count. */
            dcount += 1;

            /* TODO: Check whether the device numbers are sensible. */

            /* Allocate another name pointer. */
            *device_names = realloc( *device_names, dcount * sizeof(char*) );

            /* Allocate the device name string. */
            (*device_names)[ dcount -1 ] = malloc( strlen( dname ) +1 );
            
            /* Copy the buffer into the device name string. */
            strcpy( (*device_names)[ dcount -1 ], dname );

        } /* if sscanf */

    } /* while fgets */

    /* Pad the array with a null pointer. */
    *device_names = realloc( *device_names, ( dcount +1 ) * sizeof(char*) );
    (*device_names)[ dcount ] = NULL;

    /* Return the number of devices that were found. */
    return dcount;

}

// Assembly:
subq $0x2048, %rsp           # imm = 0x2048
movq %rdi, 0x2040(%rsp)
movl 0xb86f(%rip), %eax      # 0xf8c4
movl %eax, 0x1a(%rsp)
movw 0xb868(%rip), %ax       # 0xf8c8
movw %ax, 0x1e(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0xb9f1(%rip), %rdi      # 0xfa65
leaq 0xb03a(%rip), %rsi      # 0xf0b5
callq 0x3280
movq %rax, 0x2038(%rsp)
cmpq $0x0, 0x2038(%rsp)
jne 0x40c5
leaq 0xb9dc(%rip), %rdi      # 0xfa76
callq 0x3360
movl $0x5, %edi
leaq 0xb9e4(%rip), %rsi      # 0xfa8f
leaq 0xb9b3(%rip), %rdx      # 0xfa65
movb $0x0, %al
callq 0xade0
callq 0x3070
movl (%rax), %edi
callq 0x33e0
leaq 0x20(%rsp), %rdi
leaq 0x1a(%rsp), %rsi
callq 0x30d0
movq 0x2040(%rsp), %rax
cmpq $0x0, (%rax)
je 0x40ff
movl $0x5, %edi
leaq 0xb9d2(%rip), %rsi      # 0xfac0
movb $0x0, %al
callq 0xade0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x33e0
jmp 0x4101
leaq 0x1030(%rsp), %rdi
movq 0x2038(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
cmpq $0x0, %rax
je 0x4204
leaq 0x1030(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x1a(%rsp), %rdi
callq 0x3150
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %r9
addq %rax, %r9
leaq 0xb9b1(%rip), %rsi      # 0xfb01
leaq 0x102c(%rsp), %rdx
leaq 0x1028(%rsp), %rcx
leaq 0x1020(%rsp), %r8
movb $0x0, %al
callq 0x3300
cmpl $0x4, %eax
jne 0x41ff
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x2040(%rsp), %rax
movq (%rax), %rdi
movslq 0x14(%rsp), %rsi
shlq $0x3, %rsi
callq 0x3310
movq %rax, %rcx
movq 0x2040(%rsp), %rax
movq %rcx, (%rax)
leaq 0x20(%rsp), %rdi
callq 0x3150
movq %rax, %rdi
addq $0x1, %rdi
callq 0x32f0
movq %rax, %rdx
movq 0x2040(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x2040(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x20(%rsp), %rsi
callq 0x30d0
jmp 0x4101
movq 0x2040(%rsp), %rax
movq (%rax), %rdi
movl 0x14(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x3310
movq %rax, %rcx
movq 0x2040(%rsp), %rax
movq %rcx, (%rax)
movq 0x2040(%rsp), %rax
movq (%rax), %rax
movslq 0x14(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addq $0x2048, %rsp           # imm = 0x2048
retq
nopw %cs:(%rax,%rax)
nop