// File: /workspace/github_c_makefile_star2_O2/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 0x6e0b(%rip), %rdi      # 0xaa33
leaq 0x646e(%rip), %rsi      # 0xa09d
callq 0x3290
testq %rax, %rax
je 0x3d2e
movw $0x2f, 0x24(%rsp)
movl $0x7665642f, 0x20(%rsp) # imm = 0x7665642F
movq %rbx, (%rsp)
cmpq $0x0, (%rbx)
jne 0x3d5e
movq %rax, %r14
leaq 0x1020(%rsp), %r12
leaq 0x10(%rsp), %r15
leaq 0x18(%rsp), %r13
xorl %ebp, %ebp
movq %r12, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdx
callq 0x3250
movl %ebp, %ebx
testq %rax, %rax
je 0x3cfe
movq %r12, %rdi
leaq 0x6e3d(%rip), %rsi      # 0xaacf
leaq 0x14(%rsp), %rdx
movq %r15, %rcx
movq %r13, %r8
leaq 0x25(%rsp), %r9
xorl %eax, %eax
callq 0x3310
cmpl $0x4, %eax
jne 0x3c71
incl %ebp
movq (%rsp), %rax
movq (%rax), %rdi
leaq 0x8(,%rbx,8), %rsi
callq 0x3330
movq %rax, 0x8(%rsp)
movq (%rsp), %rcx
movq %rax, (%rcx)
leaq 0x20(%rsp), %rdi
callq 0x3150
leaq 0x1(%rax), %rdi
callq 0x3300
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x30d0
jmp 0x3c71
movq (%rsp), %r14
movq (%r14), %rdi
leaq 0x8(,%rbx,8), %rsi
callq 0x3330
movq %rax, (%r14)
andq $0x0, (%rax,%rbx,8)
movl %ebp, %eax
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6d0f(%rip), %rdi      # 0xaa44
callq 0x3380
leaq 0x6d1c(%rip), %rsi      # 0xaa5d
leaq 0x6ceb(%rip), %rdx      # 0xaa33
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x73a8
callq 0x3070
movl (%rax), %edi
callq 0x3400
leaq 0x6d29(%rip), %rsi      # 0xaa8e
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x73a8
pushq $-0x1
popq %rdi
callq 0x3400
nop