// File: /workspace/github_c_makefile_star2_O0/gmcvicker[P]check_fastq/filter_dup_reads.c
// Function: main
int main(int argc, char **argv) {
  gzFile gzf;
  char *input_filename, *seq_str, *count_ptr;
  char line1[MAX_LINE], line2[MAX_LINE], line3[MAX_LINE], line4[MAX_LINE];
  GHashTable *hashtab;
  long *val, count;

  if(argc != 2) {
	fprintf(stderr, "usage: %s my_fastq_file.txt.gz | "
			"gzip > filtered_fastq.txt.gz\n", argv[0]);
	exit(2);
  }

  input_filename = argv[1];

  gzf = util_must_gzopen(input_filename, "rb");

  hashtab = g_hash_table_new_full(g_str_hash, g_str_equal,
								  free, free);

  count = 0;
  while(TRUE) {
	/* read all 4 fastq lines at once time */
	if((gzgets(gzf, line1, MAX_LINE) == NULL) ||
	   (gzgets(gzf, line2, MAX_LINE) == NULL) ||
	   (gzgets(gzf, line3, MAX_LINE) == NULL) ||
	   (gzgets(gzf, line4, MAX_LINE) == NULL)) {
	  break;
	}
	
	count += 1;
	if(count == 100000) {
	  count = 0;
	  fprintf(stderr, ".");
	}


	/* the second line is the sequence, we want to check if we've seen
	 * the same one before
	 */
	seq_str = &line2[1];
	
	val = g_hash_table_lookup(hashtab, seq_str);
	if(val == NULL) {
	  /* we have not seen a read with this sequence before */

	  /* record the read in the hash table */
	  val = my_new(long, 1);
	  *val = 1;
	  g_hash_table_insert(hashtab, util_str_dup(seq_str), val);
	  
	  /* print the read to stdout in fastq format */
	  fprintf(stdout, "%s%s%s%s", line1, line2, line3, line4);
	} else {
	  /* we have seen this sequence before, increment count and discard */
	  *val += 1;
	  /* fprintf(stderr, "discarding duplicate read (seen %d times) %s \n",
	   * seq_str, *val);
	   */
	}
  }  

  fprintf(stderr, "\n");

  gzclose(gzf);
  g_hash_table_destroy(hashtab);

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1050, %rsp           # imm = 0x1050
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x23f8
movq 0x4c0a(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2c1d(%rip), %rsi      # 0x5004
movb $0x0, %al
callq 0x2270
movl $0x2, %edi
callq 0x20b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x2c34(%rip), %rsi      # 0x5043
callq 0x3c80
movq %rax, -0x18(%rbp)
movq 0x4b91(%rip), %rdi      # 0x6fb0
movq 0x4bca(%rip), %rsi      # 0x6ff0
movq 0x4b9b(%rip), %rcx      # 0x6fc8
movq %rcx, %rdx
callq 0x2180
movq %rax, -0x1038(%rbp)
movq $0x0, -0x1048(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x430(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
cmpq $0x0, %rax
je 0x24b3
movq -0x18(%rbp), %rdi
leaq -0x830(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
cmpq $0x0, %rax
je 0x24b3
movq -0x18(%rbp), %rdi
leaq -0xc30(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
cmpq $0x0, %rax
je 0x24b3
movq -0x18(%rbp), %rdi
leaq -0x1030(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
cmpq $0x0, %rax
jne 0x24b8
jmp 0x25c6
movq -0x1048(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1048(%rbp)
cmpq $0x186a0, -0x1048(%rbp) # imm = 0x186A0
jne 0x24fa
movq $0x0, -0x1048(%rbp)
movq 0x4af7(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
leaq 0x2b53(%rip), %rsi      # 0x5046
movb $0x0, %al
callq 0x2270
leaq -0x830(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x1038(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2210
movq %rax, -0x1040(%rbp)
cmpq $0x0, -0x1040(%rbp)
jne 0x25b0
movl $0x8, %edi
callq 0x4220
movq %rax, -0x1040(%rbp)
movq -0x1040(%rbp), %rax
movq $0x1, (%rax)
movq -0x1038(%rbp), %rax
movq %rax, -0x1050(%rbp)
movq -0x28(%rbp), %rdi
callq 0x3240
movq -0x1050(%rbp), %rdi
movq %rax, %rsi
movq -0x1040(%rbp), %rdx
callq 0x20e0
movq 0x4a77(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq -0x430(%rbp), %rdx
leaq -0x830(%rbp), %rcx
leaq -0xc30(%rbp), %r8
leaq -0x1030(%rbp), %r9
leaq 0x2aa1(%rip), %rsi      # 0x5048
movb $0x0, %al
callq 0x2270
jmp 0x25c1
movq -0x1040(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x2447
movq 0x4a13(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
leaq 0x2baa(%rip), %rsi      # 0x5181
movb $0x0, %al
callq 0x2270
movq -0x18(%rbp), %rdi
callq 0x20d0
movq -0x1038(%rbp), %rdi
callq 0x2060
xorl %eax, %eax
addq $0x1050, %rsp           # imm = 0x1050
popq %rbp
retq
nop