// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x48(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0xe49f
movl $0x7, %edi
leaq 0x3dbf(%rip), %rsi      # 0x1224a
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jg 0xe4d0
movq 0x40(%rsp), %rax
movl (%rax), %edx
movl $0x7, %edi
leaq 0x3db2(%rip), %rsi      # 0x1226e
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
movq 0x48(%rsp), %rax
movq 0x270(%rax), %rdi
shlq $0xa, %rdi
movq 0x40(%rsp), %rax
movl (%rax), %eax
shll %eax
cltq
addq %rax, %rdi
callq 0x32f0
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0xe53a
callq 0x3070
movl (%rax), %edi
leaq 0x3d8f(%rip), %rsi      # 0x1229d
leaq 0x16b4(%rip), %rdx      # 0xfbc9
callq 0xb160
movl $0x6, %edi
leaq 0x3a02(%rip), %rsi      # 0x11f28
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq 0x270(%rdx), %rdx
shlq $0xa, %rdx
addq %rdx, %rcx
movq 0x40(%rsp), %rdx
movslq (%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0xe5a5
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x40(%rsp), %rax
movslq (%rax), %rdx
callq 0x3290
movq 0x40(%rsp), %rax
movl (%rax), %ecx
movq 0x18(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
jmp 0xe544
movq 0x48(%rsp), %rax
movl 0x8(%rax), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x33f0
movq %rax, 0x28(%rsp)
cmpq $-0x1, 0x28(%rsp)
jne 0xe60b
callq 0x3070
movl (%rax), %edi
leaq 0x3cca(%rip), %rsi      # 0x1229d
leaq 0x17d8(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x6, %edi
leaq 0x17e7(%rip), %rsi      # 0xfdde
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
cmpq $0x0, 0x28(%rsp)
je 0xe63f
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x7, %edi
leaq 0x3aa7(%rip), %rsi      # 0x120d2
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
jmp 0xe641
cmpq $0x0, 0x8(%rsp)
jbe 0xe861
movq 0x48(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
cmpq 0x8(%rsp), %rax
jae 0xe67b
movq 0x48(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
movq %rax, 0x30(%rsp)
jmp 0xe6b7
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rcx
movq 0x48(%rsp), %rax
movq 0x270(%rax), %r8
movl $0x4, %edi
leaq 0x38d9(%rip), %rsi      # 0x11f82
leaq 0x3bed(%rip), %rdx      # 0x1229d
movb $0x0, %al
callq 0xade0
movq 0x48(%rsp), %rax
movl 0x8(%rax), %edi
movq 0x20(%rsp), %rsi
movslq 0x14(%rsp), %rax
addq %rax, %rsi
movq 0x30(%rsp), %rdx
callq 0x3110
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jge 0xe727
callq 0x3070
movl (%rax), %edi
leaq 0x3bae(%rip), %rsi      # 0x1229d
leaq 0x3a13(%rip), %rdx      # 0x12109
callq 0xb160
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x6, %edi
leaq 0x3b9c(%rip), %rsi      # 0x122af
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
movslq 0x3c(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0xe7ef
movq 0x30(%rsp), %rax
movslq 0x3c(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x4(%rsp)
movslq 0x4(%rsp), %rcx
movq 0x48(%rsp), %rax
addq 0x2f8(%rax), %rcx
movq %rcx, 0x2f8(%rax)
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl 0x4(%rsp), %ecx
movl $0x4, %edi
leaq 0x3993(%rip), %rsi      # 0x1210f
movb $0x0, %al
callq 0xade0
movq 0x48(%rsp), %rax
movl 0x8(%rax), %edi
movslq 0x4(%rsp), %rsi
movl $0x1, %edx
callq 0x33f0
movq %rax, 0x28(%rsp)
cmpq $-0x1, 0x28(%rsp)
jne 0xe7ed
callq 0x3070
movl (%rax), %edi
leaq 0x3ae8(%rip), %rsi      # 0x1229d
leaq 0x15f6(%rip), %rdx      # 0xfdb2
callq 0xb160
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x5, %edi
leaq 0x395d(%rip), %rsi      # 0x12136
movb $0x0, %al
callq 0xade0
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0xe8e0
jmp 0xe7ef
movq 0x48(%rsp), %rax
movq 0x270(%rax), %rax
shlq $0xa, %rax
movslq 0x14(%rsp), %rcx
addq %rcx, %rax
movq 0x40(%rsp), %rcx
movslq (%rcx), %rcx
cqto
idivq %rcx
movl %edx, %eax
movl %eax, 0x14(%rsp)
movslq 0x3c(%rsp), %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x8(%rsp)
movslq 0x3c(%rsp), %rcx
movq 0x48(%rsp), %rax
addq 0x340(%rax), %rcx
movq %rcx, 0x340(%rax)
movslq 0x3c(%rsp), %rcx
movq 0x48(%rsp), %rax
addq 0x2f0(%rax), %rcx
movq %rcx, 0x2f0(%rax)
jmp 0xe641
movq 0x48(%rsp), %rax
movw $0x1, 0x57c(%rax)
movq 0x48(%rsp), %rax
movl 0x8(%rax), %edi
callq 0x3080
movl %eax, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movw $0x0, 0x57c(%rax)
cmpl $0x0, 0x3c(%rsp)
je 0xe8ce
callq 0x3070
movl (%rax), %edi
leaq 0x39fa(%rip), %rsi      # 0x1229d
leaq 0x36b0(%rip), %rdx      # 0x11f5a
callq 0xb160
movq 0x48(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x4, %edi
leaq 0x369d(%rip), %rsi      # 0x11f64
movb $0x0, %al
callq 0xade0
movq 0x20(%rsp), %rdi
callq 0x3060
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax)