// File: /workspace/github_c_makefile_star2_O2/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 0x9033
movq %rdi, %rbx
leaq 0x320(%rdi), %r12
cmpq $0x0, (%r12)
je 0x903e
movq 0x230(%rbx), %r13
movq 0x270(%rbx), %rdi
shlq $0xa, %rdi
callq 0x3300
testq %rax, %rax
je 0x904f
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 0x907f
testq %rax, %rax
jne 0x90ac
testq %r13, %r13
je 0x90c1
movq 0x270(%rbx), %r8
movq %r8, %r12
shlq $0xa, %r12
cmpq %r13, %r12
jb 0x8fa7
movq 0x228(%rbx), %rcx
pushq $0x4
popq %rdi
leaq 0x4004(%rip), %rsi      # 0xcf9a
leaq 0x410a(%rip), %rdx      # 0xd0a7
xorl %eax, %eax
callq 0x73a8
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 0x9120
movl %eax, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq %r12, %rbp
je 0x901d
subq %rax, %r12
movslq %r12d, %r12
addq %r12, 0x2f8(%rbx)
movq 0x228(%rbx), %rdx
pushq $0x4
popq %rdi
leaq 0x412c(%rip), %rsi      # 0xd127
movl %r12d, %ecx
xorl %eax, %eax
callq 0x73a8
movl 0x8(%rbx), %edi
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x3420
cmpq $-0x1, %rax
je 0x9166
addq %rbp, 0x340(%rbx)
subq %rbp, %r13
addq %rbp, 0x2f0(%rbx)
jmp 0x8f69
leaq 0x401d(%rip), %rsi      # 0xd057
pushq $0x7
jmp 0x9072
leaq 0x4033(%rip), %rsi      # 0xd078
pushq $0x7
popq %rdi
xorl %edx, %edx
jmp 0x914b
callq 0x3070
movl (%rax), %edi
leaq 0x404a(%rip), %rsi      # 0xd0a7
leaq 0x1b4c(%rip), %rdx      # 0xabb0
callq 0x7600
leaq 0x4049(%rip), %rsi      # 0xd0b9
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x73a8
jmp 0x9152
callq 0x3070
movl (%rax), %edi
leaq 0x401a(%rip), %rsi      # 0xd0a7
leaq 0x1d05(%rip), %rdx      # 0xad99
callq 0x7600
movq 0x228(%rbx), %rdx
leaq 0x1d1e(%rip), %rsi      # 0xadc5
jmp 0x9148
movq 0x228(%rbx), %rdx
leaq 0x4030(%rip), %rsi      # 0xd0ea
pushq $0x7
jmp 0x914a
movq %r14, %rdi
callq 0x3060
movw $0x1, 0x57c(%rbx)
movl 0x8(%rbx), %edi
callq 0x3080
andw $0x0, 0x57c(%rbx)
testl %eax, %eax
je 0x9192
callq 0x3070
movl (%rax), %edi
leaq 0x3faf(%rip), %rsi      # 0xd0a7
leaq 0x3e73(%rip), %rdx      # 0xcf72
callq 0x7600
movq 0x228(%rbx), %rdx
leaq 0x3e6a(%rip), %rsi      # 0xcf7c
xorl %ebx, %ebx
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x73a8
jmp 0x9155
callq 0x3070
movl (%rax), %edi
leaq 0x3f79(%rip), %rsi      # 0xd0a7
leaq 0x3fec(%rip), %rdx      # 0xd121
callq 0x7600
movq 0x228(%rbx), %rdx
leaq 0x3e8f(%rip), %rsi      # 0xcfd7
pushq $0x6
popq %rdi
xorl %eax, %eax
callq 0x73a8
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3070
movl (%rax), %edi
leaq 0x3f33(%rip), %rsi      # 0xd0a7
leaq 0x1c1e(%rip), %rdx      # 0xad99
callq 0x7600
movq 0x228(%rbx), %rdx
leaq 0x3fc0(%rip), %rsi      # 0xd14e
pushq $0x5
jmp 0x914a
xorl %ebx, %ebx
jmp 0x9155