// File: /workspace/github_c_makefile_star2_O3/sTeeLM[P]dwipe/pass.c
// Function: dwipe_static_pass
int dwipe_static_pass( DWIPE_METHOD_SIGNATURE, dwipe_pattern_t* pattern )
{
/**
 * Writes a static 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;

	/* A pointer into the output buffer. */
	char* p;

	/* The output buffer window offset. */
	int w = 0;

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

	if( pattern == NULL )
	{
		/* Caught insanity. */	
		dwipe_log( DWIPE_LOG_SANITY, "__FUNCTION__: Null pattern pointer." );
		return -1;
	}

	if( pattern->length <= 0 )
	{
		/* Caught insanity. */	
		dwipe_log( DWIPE_LOG_SANITY, "__FUNCTION__: The pattern length member is %i.", pattern->length );
		return -1;
	}

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

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

	for( p = b ; p < b + c->device_stat.st_blksize * 1024 + pattern->length ; p += pattern->length )
	{
		/* Fill the output buffer with the pattern. */
		memcpy( p, pattern->s, pattern->length ); 
	}


	/* 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. */
		/* Write the next block out to the device. */
		r = write( c->device_fd, &b[w], blocksize );

		/* Check the result for a fatal error. */
		if( r < 0 )
		{
			dwipe_perror( errno, __FUNCTION__, "write" );
			dwipe_log( DWIPE_LOG_FATAL, "Unable to write to '%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. */
			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 */


		/* Adjust the window. */
		w = ( c->device_stat.st_blksize * 1024 + w ) % pattern->length;

		/* Intuition check: 
		 *
		 *   If the pattern length evenly divides the block size
		 *   then ( w == 0 ) always.
		 */

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

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

	} /* remaining bytes */

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

	/* Release the output buffer. */
	free( b );
	
	/* We're done. */
	return 0;	

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rsi, %rsi
je 0x9e49
movq %rsi, %r13
movl (%rsi), %r12d
testl %r12d, %r12d
jle 0x9e57
movq %rdi, %rbx
movq 0x230(%rdi), %r14
movq 0x270(%rdi), %r15
shlq $0xa, %r15
leaq (%r15,%r12,2), %rdi
callq 0x3300
testq %rax, %rax
je 0x9e72
movq %r13, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
addq %rax, %r15
leaq (%r15,%r12), %rcx
movq %rax, (%rsp)
cmpq %rcx, %rax
jae 0x9cdd
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r13
movq %r12, %rbx
negq %rbx
movq (%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x32a0
addq %r12, %rbp
leaq (%rbx,%rbp), %rax
cmpq %r15, %rax
jb 0x9cc3
movq 0x10(%rsp), %rbx
movl 0x8(%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
cmpq $-0x1, %rax
je 0x9ea1
testq %rax, %rax
jne 0x9ecb
testq %r14, %r14
movq (%rsp), %r15
je 0x9de8
movq 0x270(%rbx), %r8
xorl %r12d, %r12d
movq %r8, %r13
shlq $0xa, %r13
cmpq %r14, %r13
jb 0x9d48
movq 0x228(%rbx), %rcx
movl $0x4, %edi
leaq 0x4297(%rip), %rsi      # 0xdfce
leaq 0x45ab(%rip), %rdx      # 0xe2e9
xorl %eax, %eax
callq 0x7a7c
movq %r14, %r13
movl 0x8(%rbx), %edi
leaq (%r15,%r12), %rsi
movq %r13, %rdx
callq 0x3110
testl %eax, %eax
js 0x9ee0
movl %eax, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpq %r13, %rbp
je 0x9db0
subq %rax, %r13
movslq %r13d, %r13
addq %r13, 0x2f8(%rbx)
movq 0x228(%rbx), %rdx
movl $0x4, %edi
leaq 0x43cf(%rip), %rsi      # 0xe15b
movl %r13d, %ecx
xorl %eax, %eax
callq 0x7a7c
movl 0x8(%rbx), %edi
movq %r13, %rsi
movl $0x1, %edx
callq 0x3420
cmpq $-0x1, %rax
je 0x9f28
movq 0x270(%rbx), %r8
movq %r8, %rax
shlq $0xa, %rax
addq %r12, %rax
movq 0x8(%rsp), %rcx
movslq (%rcx), %rcx
cqto
idivq %rcx
addq %rbp, 0x340(%rbx)
addq %rbp, 0x2f0(%rbx)
movq %rdx, %r12
subq %rbp, %r14
jne 0x9d18
movw $0x1, 0x57c(%rbx)
movl 0x8(%rbx), %edi
callq 0x3080
movw $0x0, 0x57c(%rbx)
testl %eax, %eax
je 0x9e3a
callq 0x3070
movl (%rax), %edi
leaq 0x44d5(%rip), %rsi      # 0xe2e9
leaq 0x418b(%rip), %rdx      # 0xdfa6
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x4182(%rip), %rsi      # 0xdfb0
movl $0x4, %edi
xorl %eax, %eax
callq 0x7a7c
movq %r15, %rdi
callq 0x3060
xorl %eax, %eax
jmp 0x9f19
leaq 0x4446(%rip), %rsi      # 0xe296
movl $0x7, %edi
jmp 0x9e98
leaq 0x445c(%rip), %rsi      # 0xe2ba
movl $0x7, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x7a7c
jmp 0x9f14
callq 0x3070
movl (%rax), %edi
leaq 0x4469(%rip), %rsi      # 0xe2e9
leaq 0x1d29(%rip), %rdx      # 0xbbb0
callq 0x7cc3
leaq 0x40e1(%rip), %rsi      # 0xdf74
movl $0x6, %edi
xorl %eax, %eax
callq 0x7a7c
jmp 0x9f14
callq 0x3070
movl (%rax), %edi
leaq 0x443a(%rip), %rsi      # 0xe2e9
leaq 0x1ee3(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x1efc(%rip), %rsi      # 0xbdc5
jmp 0x9f08
movq 0x228(%rbx), %rdx
leaq 0x4245(%rip), %rsi      # 0xe11e
movl $0x7, %edi
jmp 0x9f0d
callq 0x3070
movl (%rax), %edi
leaq 0x43fb(%rip), %rsi      # 0xe2e9
leaq 0x4260(%rip), %rdx      # 0xe155
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x43f3(%rip), %rsi      # 0xe2fb
movl $0x6, %edi
xorl %eax, %eax
callq 0x7a7c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3070
movl (%rax), %edi
leaq 0x43b3(%rip), %rsi      # 0xe2e9
leaq 0x1e5c(%rip), %rdx      # 0xbd99
callq 0x7cc3
movq 0x228(%rbx), %rdx
leaq 0x4232(%rip), %rsi      # 0xe182
movl $0x5, %edi
jmp 0x9f0d
nop