// File: /workspace/github_c_makefile_star2_O3/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 0x268b
movq 0x8(%rsi), %rdi
leaq 0x1b5f(%rip), %rsi      # 0x4043
callq 0x34e4
movq %rax, %rbx
movq 0x3abd(%rip), %rdi      # 0x5fb0
movq 0x3af6(%rip), %rsi      # 0x5ff0
movq 0x3ac7(%rip), %rcx      # 0x5fc8
movq %rcx, %rdx
callq 0x2190
movq %rax, -0x30(%rbp)
leaq -0x830(%rbp), %rsi
movq %rbx, %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
testq %rax, %rax
je 0x2652
leaq -0x42f(%rbp), %r13
leaq -0x430(%rbp), %r15
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
testq %rax, %rax
je 0x2652
movq %rbx, %rdi
leaq -0x1030(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
testq %rax, %rax
je 0x2652
movq %rbx, %rdi
leaq -0xc30(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
testq %rax, %rax
je 0x2652
incq %r12
cmpq $0x186a0, %r12          # imm = 0x186A0
jne 0x25b1
movq 0x3a3f(%rip), %rax      # 0x5fe0
movq (%rax), %rsi
movl $0x2e, %edi
callq 0x2150
xorl %r12d, %r12d
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x2200
testq %rax, %rax
je 0x25ce
incq (%rax)
leaq -0x830(%rbp), %r14
jmp 0x2639
movl $0x8, %edi
callq 0x3868
movq %r13, %r15
movq %rax, %r13
movq $0x1, (%rax)
movq %r15, %rdi
callq 0x2ee7
movq -0x30(%rbp), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq %r15, %r13
leaq -0x430(%rbp), %r15
callq 0x20d0
movq 0x39eb(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1a2f(%rip), %rsi      # 0x4046
leaq -0x830(%rbp), %r14
movq %r14, %rdx
movq %r15, %rcx
leaq -0x1030(%rbp), %r8
leaq -0xc30(%rbp), %r9
xorl %eax, %eax
callq 0x2270
movq %rbx, %rdi
movq %r14, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2230
testq %rax, %rax
jne 0x253b
movq 0x3987(%rip), %rax      # 0x5fe0
movq (%rax), %rsi
movl $0xa, %edi
callq 0x2150
movq %rbx, %rdi
callq 0x20c0
movq -0x30(%rbp), %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 %rsi, %rdi
callq 0x22c0
nop