// File: /workspace/github_c_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movq %rdi, %rbx
leaq 0x7b22(%rip), %rdi      # 0xba33
leaq 0x7185(%rip), %rsi      # 0xb09d
callq 0x3290
testq %rax, %rax
je 0x4075
movw $0x2f, 0x24(%rsp)
movl $0x7665642f, 0x20(%rsp) # imm = 0x7665642F
cmpq $0x0, (%rbx)
jne 0x407a
movq %rax, %r14
movq %rbx, 0x8(%rsp)
leaq 0x1020(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x3250
testq %rax, %rax
je 0x4035
leaq 0x25(%rsp), %r15
leaq 0x7b5e(%rip), %rbx      # 0xbacf
leaq 0x1020(%rsp), %r12
leaq 0x14(%rsp), %r13
leaq 0x4(%rsp), %rbp
movl $0x0, (%rsp)
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rbp, %rcx
leaq 0x18(%rsp), %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x3310
cmpl $0x4, %eax
jne 0x401a
movq %r13, %r12
movq %r14, %r13
movq 0x8(%rsp), %r14
movq (%r14), %rdi
movl (%rsp), %eax
movslq %eax, %rbp
incl %eax
movl %eax, (%rsp)
leaq 0x8(,%rbp,8), %rsi
callq 0x3330
movq %r15, %rbx
movq %rax, %r15
movq %rax, (%r14)
movq %r13, %r14
movq %r12, %r13
leaq 0x20(%rsp), %rdi
callq 0x3150
leaq 0x1(%rax), %rdi
callq 0x3300
movq %rax, (%r15,%rbp,8)
leaq 0x4(%rsp), %rbp
movq %rbx, %r15
leaq 0x7aca(%rip), %rbx      # 0xbacf
leaq 0x1020(%rsp), %r12
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x30d0
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x3250
testq %rax, %rax
jne 0x3f8a
jmp 0x403c
movl $0x0, (%rsp)
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
movslq (%rsp), %r14
leaq 0x8(,%r14,8), %rsi
callq 0x3330
movq %rax, (%rbx)
movq $0x0, (%rax,%r14,8)
movl %r14d, %eax
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3490
leaq 0x7a0d(%rip), %rsi      # 0xba8e
movl $0x5, %edi
xorl %eax, %eax
callq 0x7a7c
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3400
nop