// File: /workspace/github_c_makefile_star2_O3/sTeeLM[P]dwipe/pass.c
// Function: dwipe_random_pass
int dwipe_random_pass( DWIPE_METHOD_SIGNATURE )
{
/**
 * Writes a random pattern to the device.
 *
 */

	/* The result holder. */
	int r;

	/* The IO size. */
	size_t blocksize;

	/* The result buffer for calls to lseek. */
	loff_t offset;

	/* The output buffer. */
	char* b;

	/* The number of bytes remaining in the pass. */
	u64 z = c->device_size;


	if( c->prng_seed.s == NULL )
	{
		dwipe_log( DWIPE_LOG_SANITY, "__FUNCTION__: Null seed pointer." );
		return -1;
	}

	if( c->prng_seed.length <= 0 )
	{
		dwipe_log( DWIPE_LOG_SANITY, "__FUNCTION__: The entropy length member is %i.", c->prng_seed.length );
		return -1;
	}


	/* Create the output buffer. */
	b = malloc( c->device_stat.st_blksize * 1024 );

	/* Check the memory allocation. */
	if( ! b )
	{
		dwipe_perror( errno, __FUNCTION__, "malloc" );
		dwipe_log( DWIPE_LOG_FATAL, "Unable to allocate memory for the output buffer." );
		return -1;
	}

	/* Seed the PRNG. */
	c->prng->init( &c->prng_state, &c->prng_seed );

	/* Reset the file pointer. */
	offset = lseek( c->device_fd, 0, SEEK_SET );

	if( offset == (loff_t)-1 )
	{
		dwipe_perror( errno, __FUNCTION__, "lseek" );
		dwipe_log( DWIPE_LOG_FATAL, "Unable to reset the '%s' file offset.", c->device_name );
		return -1;
	}

	if( offset != 0 )
	{
		/* This is system insanity. */
		dwipe_log( DWIPE_LOG_SANITY, "__FUNCTION__: lseek() returned a bogus offset on '%s'.", c->device_name );
		return -1;
	}


	while( z > 0 )
	{
		if( c->device_stat.st_blksize * 1024 < z )
		{
			blocksize = c->device_stat.st_blksize * 1024;
		}
		else
		{
			/* This is a seatbelt for buggy drivers and programming errors because */
			/* the device size should always be an even multiple of its blocksize. */
			blocksize = z;
			dwipe_log( DWIPE_LOG_WARNING,
			  "%s: The size of '%s' is not a multiple of its block size %i.",
			  __FUNCTION__, c->device_name, c->device_stat.st_blksize );
		}

		/* Fill the output buffer with the random pattern. */
		c->prng->read( &c->prng_state, b, blocksize );

		/* Write the next block out to the device. */
		r = write( c->device_fd, b, blocksize );

		/* Check the result for a fatal error. */
		if( r < 0 )
		{
			dwipe_perror( errno, __FUNCTION__, "write" );
			dwipe_log( DWIPE_LOG_FATAL, "Unable to read from '%s'.", c->device_name );
			return -1;
		}

		/* Check for a partial write. */
		if( r != blocksize )
		{
			/* TODO: Handle a partial write. */

			/* The number of bytes that were not written. */
			int s = blocksize - r;
			
			/* Increment the error count by the number of bytes that were not written. */
			c->pass_errors += s;

			dwipe_log( DWIPE_LOG_WARNING, "Partial write on '%s', %i bytes short.", c->device_name, s );

			/* Bump the file pointer to the next block. */
			offset = lseek( c->device_fd, s, SEEK_CUR );

			if( offset == (loff_t)-1 )
			{
				dwipe_perror( errno, __FUNCTION__, "lseek" );
				dwipe_log( DWIPE_LOG_ERROR, "Unable to bump the '%s' file offset after a partial write.", c->device_name );
				return -1;
			}

		} /* partial write */

		/* Decrement the bytes remaining in this pass. */
		z -= r;

		/* Increment the total progress counters. */
		c->round_done += r;
		c->pass_done += r;

	} /* remaining bytes */

	/* Release the output buffer. */
	free( b );

	/* Tell our parent that we are syncing the device. */
	c->sync_status = 1;

	/* Sync the device. */
	r = fdatasync( c->device_fd );

	/* Tell our parent that we have finished syncing the device. */
	c->sync_status = 0;

	if( r != 0 )
	{
		/* FIXME: Is there a better way to handle this? */
		dwipe_perror( errno, __FUNCTION__, "fdatasync" );
		dwipe_log( DWIPE_LOG_WARNING, "Buffer flush failure on '%s'.", c->device_name );
	}

	/* We're done. */
	return 0;	

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpq $0x0, 0x328(%rdi)
je 0x97be
movq %rdi, %rbx
leaq 0x320(%rdi), %r12
cmpq $0x0, (%r12)
je 0x97cc
movq 0x230(%rbx), %r13
movq 0x270(%rbx), %rdi
shlq $0xa, %rdi
callq 0x3300
testq %rax, %rax
je 0x97df
movq %rax, %r14
movq 0x318(%rbx), %rax
leaq 0x330(%rbx), %r15
movq %r15, %rdi
movq %r12, %rsi
callq *0x8(%rax)
movl 0x8(%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
cmpq $-0x1, %rax
je 0x980e
testq %rax, %rax
jne 0x9838
testq %r13, %r13
je 0x9759
movq 0x270(%rbx), %r8
movq %r8, %r12
shlq $0xa, %r12
cmpq %r13, %r12
jb 0x96c8
movq 0x228(%rbx), %rcx
movl $0x4, %edi
leaq 0x4917(%rip), %rsi      # 0xdfce
leaq 0x4a1d(%rip), %rdx      # 0xe0db
xorl %eax, %eax
callq 0x7a7c
movq %r13, %r12
movq 0x318(%rbx), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq *0x10(%rax)
movl 0x8(%rbx), %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3110
testl %eax, %eax
js 0x984d
movl %eax, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq %r12, %rbp
je 0x9742
subq %rax, %r12
movslq %r12d, %r12
addq %r12, 0x2f8(%rbx)
movq 0x228(%rbx), %rdx
movl $0x4, %edi
leaq 0x4a3d(%rip), %rsi      # 0xe15b
movl %r12d, %ecx
xorl %eax, %eax
callq 0x7a7c
movl 0x8(%rbx), %edi
movq %r12, %rsi
movl $0x1, %edx
callq 0x3420
cmpq $-0x1, %rax
je 0x989b
addq %rbp, 0x340(%rbx)
addq %rbp, 0x2f0(%rbx)
subq %rbp, %r13
jne 0x9691
movq %r14, %rdi
callq 0x3060
movw $0x1, 0x57c(%rbx)
movl 0x8(%rbx), %edi
callq 0x3080
movw $0x0, 0x57c(%rbx)
testl %eax, %eax
je 0x9897
callq 0x3070
movl (%rax), %edi
leaq 0x494a(%rip), %rsi      # 0xe0db
leaq 0x480e(%rip), %rdx      # 0xdfa6
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x4805(%rip), %rsi      # 0xdfb0
xorl %ebx, %ebx
movl $0x4, %edi
xorl %eax, %eax
callq 0x7a7c
jmp 0x9886
leaq 0x48c6(%rip), %rsi      # 0xe08b
movl $0x7, %edi
jmp 0x9805
leaq 0x48d9(%rip), %rsi      # 0xe0ac
movl $0x7, %edi
xorl %edx, %edx
jmp 0x987a
callq 0x3070
movl (%rax), %edi
leaq 0x48ee(%rip), %rsi      # 0xe0db
leaq 0x23bc(%rip), %rdx      # 0xbbb0
callq 0x7cc3
leaq 0x48ed(%rip), %rsi      # 0xe0ed
movl $0x6, %edi
xorl %eax, %eax
callq 0x7a7c
jmp 0x9881
callq 0x3070
movl (%rax), %edi
leaq 0x48bf(%rip), %rsi      # 0xe0db
leaq 0x2576(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x258f(%rip), %rsi      # 0xbdc5
jmp 0x9875
movq 0x228(%rbx), %rdx
leaq 0x48d8(%rip), %rsi      # 0xe11e
movl $0x7, %edi
jmp 0x987a
callq 0x3070
movl (%rax), %edi
leaq 0x4880(%rip), %rsi      # 0xe0db
leaq 0x48f3(%rip), %rdx      # 0xe155
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x4796(%rip), %rsi      # 0xe00b
movl $0x6, %edi
xorl %eax, %eax
callq 0x7a7c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x9886
callq 0x3070
movl (%rax), %edi
leaq 0x4832(%rip), %rsi      # 0xe0db
leaq 0x24e9(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x48bf(%rip), %rsi      # 0xe182
movl $0x5, %edi
jmp 0x987a