// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
cmpq $0x0, 0x328(%rax)
jne 0xdb19
movl $0x7, %edi
leaq 0x453a(%rip), %rsi      # 0x1203f
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
movq 0x38(%rsp), %rax
cmpq $0x0, 0x320(%rax)
ja 0xdb54
movq 0x38(%rsp), %rax
movq 0x320(%rax), %rdx
movl $0x7, %edi
leaq 0x4520(%rip), %rsi      # 0x12060
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rdi
shlq $0xa, %rdi
callq 0x32f0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0xdbb0
callq 0x3070
movl (%rax), %edi
leaq 0x450b(%rip), %rsi      # 0x1208f
leaq 0x203e(%rip), %rdx      # 0xfbc9
callq 0xb160
movl $0x6, %edi
leaq 0x4505(%rip), %rsi      # 0x120a1
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
movq 0x38(%rsp), %rax
movq 0x318(%rax), %rax
movq 0x8(%rax), %rax
movq 0x38(%rsp), %rdi
addq $0x330, %rdi            # imm = 0x330
movq 0x38(%rsp), %rsi
addq $0x320, %rsi            # imm = 0x320
callq *%rax
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x33f0
movq %rax, 0x20(%rsp)
cmpq $-0x1, 0x20(%rsp)
jne 0xdc40
callq 0x3070
movl (%rax), %edi
leaq 0x4487(%rip), %rsi      # 0x1208f
leaq 0x21a3(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x6, %edi
leaq 0x21b2(%rip), %rsi      # 0xfdde
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
cmpq $0x0, 0x20(%rsp)
je 0xdc74
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x7, %edi
leaq 0x4472(%rip), %rsi      # 0x120d2
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
jmp 0xdc76
cmpq $0x0, 0x10(%rsp)
jbe 0xde8b
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
cmpq 0x10(%rsp), %rax
jae 0xdcb0
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
movq %rax, 0x28(%rsp)
jmp 0xdcec
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rcx
movq 0x38(%rsp), %rax
movq 0x270(%rax), %r8
movl $0x4, %edi
leaq 0x42a4(%rip), %rsi      # 0x11f82
leaq 0x43aa(%rip), %rdx      # 0x1208f
movb $0x0, %al
callq 0xade0
movq 0x38(%rsp), %rax
movq 0x318(%rax), %rax
movq 0x10(%rax), %rax
movq 0x38(%rsp), %rdi
addq $0x330, %rdi            # imm = 0x330
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq *%rax
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x3110
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jge 0xdd7c
callq 0x3070
movl (%rax), %edi
leaq 0x434b(%rip), %rsi      # 0x1208f
leaq 0x43be(%rip), %rdx      # 0x12109
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x6, %edi
leaq 0x4257(%rip), %rsi      # 0x11fbf
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
movslq 0x34(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0xde44
movq 0x28(%rsp), %rax
movslq 0x34(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0xc(%rsp)
movslq 0xc(%rsp), %rcx
movq 0x38(%rsp), %rax
addq 0x2f8(%rax), %rcx
movq %rcx, 0x2f8(%rax)
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl 0xc(%rsp), %ecx
movl $0x4, %edi
leaq 0x433e(%rip), %rsi      # 0x1210f
movb $0x0, %al
callq 0xade0
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
movslq 0xc(%rsp), %rsi
movl $0x1, %edx
callq 0x33f0
movq %rax, 0x20(%rsp)
cmpq $-0x1, 0x20(%rsp)
jne 0xde42
callq 0x3070
movl (%rax), %edi
leaq 0x4285(%rip), %rsi      # 0x1208f
leaq 0x1fa1(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x5, %edi
leaq 0x4308(%rip), %rsi      # 0x12136
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdf0a
jmp 0xde44
movslq 0x34(%rsp), %rcx
movq 0x10(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x10(%rsp)
movslq 0x34(%rsp), %rcx
movq 0x38(%rsp), %rax
addq 0x340(%rax), %rcx
movq %rcx, 0x340(%rax)
movslq 0x34(%rsp), %rcx
movq 0x38(%rsp), %rax
addq 0x2f0(%rax), %rcx
movq %rcx, 0x2f0(%rax)
jmp 0xdc76
movq 0x18(%rsp), %rdi
callq 0x3060
movq 0x38(%rsp), %rax
movw $0x1, 0x57c(%rax)
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
callq 0x3080
movl %eax, 0x34(%rsp)
movq 0x38(%rsp), %rax
movw $0x0, 0x57c(%rax)
cmpl $0x0, 0x34(%rsp)
je 0xdf02
callq 0x3070
movl (%rax), %edi
leaq 0x41b8(%rip), %rsi      # 0x1208f
leaq 0x407c(%rip), %rdx      # 0x11f5a
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x4, %edi
leaq 0x4069(%rip), %rsi      # 0x11f64
movb $0x0, %al
callq 0xade0
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)