// File: /workspace/github_c_makefile_star2_O0/sTeeLM[P]dwipe/pass.c
// Function: dwipe_random_verify
int dwipe_random_verify( dwipe_context_t* c )
{
/**
 * Verifies that a random pass was correctly written 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 input buffer. */
	char* b;

	/* The pattern buffer that is used to check the input buffer. */
	char* d;

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


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

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

	/* Create the input 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 input buffer." );
		return -1;
	}

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

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

	/* 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, "lseek() returned a bogus offset on '%s'.", c->device_name );
		return -1;
	}

	/* 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 );
	}

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

	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, d, blocksize );

		/* Read the buffer in from the device. */
		r = read( c->device_fd, b, blocksize );

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

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

			/* The number of bytes that were not read. */
			int s = blocksize - r;

			dwipe_log( DWIPE_LOG_WARNING, "%s: Partial read from '%s', %i bytes short.", __FUNCTION__, c->device_name, s );
			
			/* Increment the error count. */
			c->verify_errors += 1;

			/* 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 read.", c->device_name );
				return -1;
			}

		} /* partial read */

		/* Compare buffer contents. */
		if( memcmp( b, d, blocksize ) != 0 ) { c->verify_errors += 1; }

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

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

	} /* while bytes remaining */

	/* Release the buffers. */
	free( b );
	free( d );

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

}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
cmpq $0x0, 0x328(%rax)
jne 0xd639
movl $0x7, %edi
leaq 0x4a28(%rip), %rsi      # 0x1204d
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
movq 0x38(%rsp), %rax
cmpq $0x0, 0x320(%rax)
ja 0xd674
movq 0x38(%rsp), %rax
movq 0x320(%rax), %rdx
movl $0x7, %edi
leaq 0x4a0e(%rip), %rsi      # 0x1206e
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rdi
shlq $0xa, %rdi
callq 0x32f0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0xd6d0
callq 0x3070
movl (%rax), %edi
leaq 0x4840(%rip), %rsi      # 0x11ee4
leaq 0x251e(%rip), %rdx      # 0xfbc9
callq 0xb160
movl $0x6, %edi
leaq 0x483c(%rip), %rsi      # 0x11ef8
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rdi
shlq $0xa, %rdi
callq 0x32f0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0xd72c
callq 0x3070
movl (%rax), %edi
leaq 0x47e4(%rip), %rsi      # 0x11ee4
leaq 0x24c2(%rip), %rdx      # 0xfbc9
callq 0xb160
movl $0x6, %edi
leaq 0x4810(%rip), %rsi      # 0x11f28
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
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 0xd792
callq 0x3070
movl (%rax), %edi
leaq 0x478a(%rip), %rsi      # 0x11ee4
leaq 0x2651(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x6, %edi
leaq 0x2660(%rip), %rsi      # 0xfdde
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
cmpq $0x0, 0x20(%rsp)
je 0xd7c6
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x7, %edi
leaq 0x492e(%rip), %rsi      # 0x120e0
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
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 0xd833
callq 0x3070
movl (%rax), %edi
leaq 0x46dc(%rip), %rsi      # 0x11ee4
leaq 0x474b(%rip), %rdx      # 0x11f5a
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x4, %edi
leaq 0x4738(%rip), %rsi      # 0x11f64
movb $0x0, %al
callq 0xade0
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
cmpq $0x0, 0x8(%rsp)
jbe 0xdaa9
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
cmpq 0x8(%rsp), %rax
jae 0xd897
movq 0x38(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
movq %rax, 0x28(%rsp)
jmp 0xd8d3
movq 0x8(%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 0x46bd(%rip), %rsi      # 0x11f82
leaq 0x4618(%rip), %rdx      # 0x11ee4
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 0x10(%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 0x3230
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jge 0xd963
callq 0x3070
movl (%rax), %edi
leaq 0x45b9(%rip), %rsi      # 0x11ee4
leaq 0x3aed(%rip), %rdx      # 0x1141f
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x5, %edi
leaq 0x4670(%rip), %rsi      # 0x11fbf
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
movslq 0x34(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0xda32
movq 0x28(%rsp), %rax
movslq 0x34(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x4(%rsp)
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rcx
movl 0x4(%rsp), %r8d
movl $0x4, %edi
leaq 0x4638(%rip), %rsi      # 0x11fd9
leaq 0x453c(%rip), %rdx      # 0x11ee4
movb $0x0, %al
callq 0xade0
movq 0x38(%rsp), %rax
movq 0x588(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x588(%rax)
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
movslq 0x4(%rsp), %rsi
movl $0x1, %edx
callq 0x33f0
movq %rax, 0x20(%rsp)
cmpq $-0x1, 0x20(%rsp)
jne 0xda30
callq 0x3070
movl (%rax), %edi
leaq 0x44ec(%rip), %rsi      # 0x11ee4
leaq 0x23b3(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x38(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x5, %edi
leaq 0x45e9(%rip), %rsi      # 0x12005
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0xdac5
jmp 0xda32
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x3240
cmpl $0x0, %eax
je 0xda62
movq 0x38(%rsp), %rax
movq 0x588(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x588(%rax)
movslq 0x34(%rsp), %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x8(%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 0xd85d
movq 0x18(%rsp), %rdi
callq 0x3060
movq 0x10(%rsp), %rdi
callq 0x3060
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop