// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
cmpl $0x2, %edi
jne 0x2560
movq 0x8(%rsi), %rdi
leaq 0x1c5f(%rip), %rsi      # 0x4043
callq 0x32ac
movq %rax, %rbx
movq 0x4bbd(%rip), %rdi      # 0x6fb0
movq 0x4bf6(%rip), %rsi      # 0x6ff0
movq 0x4bc7(%rip), %rcx      # 0x6fc8
movq %rcx, %rdx
callq 0x2190
movq %rax, %r14
leaq -0x1030(%rbp), %r15
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2240
testq %rax, %rax
je 0x252a
movq %rbx, %rdi
leaq -0x430(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2240
testq %rax, %rax
je 0x252a
movq %rbx, %rdi
leaq -0xc30(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2240
testq %rax, %rax
je 0x252a
movq %rbx, %rdi
leaq -0x830(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2240
testq %rax, %rax
je 0x252a
incq %r13
cmpq $0x186a0, %r13          # imm = 0x186A0
jne 0x24a7
movq 0x4b47(%rip), %rax      # 0x6fe0
movq (%rax), %rsi
pushq $0x2e
popq %rdi
callq 0x2150
xorl %r13d, %r13d
movq %r14, %rdi
leaq -0x42f(%rbp), %r12
movq %r12, %rsi
callq 0x2210
testq %rax, %rax
je 0x24c6
incq (%rax)
jmp 0x2416
pushq $0x8
popq %rdi
callq 0x36e0
movq %rax, %r15
movq $0x1, (%rax)
movq %r12, %rdi
callq 0x2d4a
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
leaq -0x1030(%rbp), %r15
callq 0x20d0
movq 0x4afc(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1b40(%rip), %rsi      # 0x4046
movq %r15, %rdx
leaq -0x430(%rbp), %rcx
leaq -0xc30(%rbp), %r8
leaq -0x830(%rbp), %r9
xorl %eax, %eax
callq 0x2280
jmp 0x2416
movq 0x4aaf(%rip), %rax      # 0x6fe0
movq (%rax), %rsi
pushq $0xa
popq %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x20c0
movq %r14, %rdi
callq 0x2060
xorl %eax, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4a79(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x1a90(%rip), %rsi      # 0x4004
xorl %eax, %eax
callq 0x2280
pushq $0x2
popq %rdi
callq 0x20a0
nop