// File: /workspace/github_c_makefile_star2_O0/gmcvicker[P]check_fastq/split_fastq.c
// Function: main
int main(int argc, char **argv) {
  ReadSeq read;
  gzFile gzf, out_gzf;
  long rec_num, line_num, n_err;
  int file_num = 0;
  char min_qual, max_qual;
  char *input_filename, *output_dir, *prefix;
  char output_filename[MAX_LINE];
  int n_written;
  
  if(argc != 3) {
    fprintf(stderr, "usage: %s <fastq_file.txt.gz> <output_dir>\n", argv[0]);
    exit(2);
  }

  input_filename = argv[1];
  output_dir = argv[2];

  gzf = util_must_gzopen(input_filename, "rb");

  prefix = get_prefix(output_dir, input_filename);

  rec_num = 0;
  line_num = 1;
  n_err = 0;
  min_qual = max_qual = -1;

  out_gzf = NULL;

  rec_num = 0;

  while(TRUE) {
    long r = 0;

    r = parse_fastq_read(&read, gzf);

    if(r == FASTQ_END) {
      /* we have reached the end of the file */
      break;
    }

    if(r == FASTQ_ERR) {
      my_warn("%s:%d: invalid fastq record starting on line %ld:\n", 
	      __FILE__, __LINE__, line_num);
      n_err += 1;
      fprintf(stderr, "  %s\n  %s\n  %s\n  %s\n", read.line1, 
	      read.line2, read.line3, read.line4);
    }
    
    if(r == FASTQ_OK) {
      /* record max and min quality values observed */
      if((min_qual == -1) || (min_qual > read.min_qual)) {
	min_qual = read.min_qual;
      }
      if((max_qual == -1) || (max_qual < read.max_qual)) {
	max_qual = read.max_qual;
      }

      if(out_gzf == NULL || rec_num > READS_PER_FILE) {
	if(out_gzf) {
	  /* close old output file */
	  fprintf(stderr, "\n");
	  gzclose(out_gzf);
	}
	
	file_num += 1;
	
	n_written = snprintf(output_filename, MAX_LINE, "%s.%d.txt.gz", 
			     prefix, file_num);

	if(n_written > MAX_LINE) {
	  my_err("%s:%d: filename too long\n", __FILE__, __LINE__);
	}

	fprintf(stderr, "writing to file '%s'\n",  output_filename);
	out_gzf = util_must_gzopen(output_filename, "wb");

	rec_num = 0;
      }
      
      /* write record to file */
      n_written = gzprintf(out_gzf, "%s\n%s\n%s\n%s\n", read.line1,
			   read.line2, read.line3, read.line4);
      if(n_written == 0) {
	my_err("%s:%d: failed to write to output file", __FILE__, __LINE__);
      }

      rec_num += 1;
    }

    line_num += 4;

    if((rec_num % 100000) == 0) {
        fprintf(stderr, ".");
    }
  }

  report_qual_type(min_qual, max_qual);

  fprintf(stderr, "\n");
  fprintf(stderr, "fastq records: written=%ld, errors=%ld\n", rec_num, n_err);

  gzclose(gzf);
  if(out_gzf) {
    gzclose(out_gzf);
  }

  my_free(prefix);

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1890, %rsp           # imm = 0x1890
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x1464(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x13f2
movq 0x6c28(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x3c23(%rip), %rsi      # 0x5004
movb $0x0, %al
callq 0x1170
movl $0x2, %edi
callq 0x1250
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1470(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1478(%rbp)
movq -0x1470(%rbp), %rdi
leaq 0x3c12(%rip), %rsi      # 0x5030
callq 0x3b40
movq %rax, -0x1440(%rbp)
movq -0x1478(%rbp), %rdi
movq -0x1470(%rbp), %rsi
callq 0x1820
movq %rax, -0x1480(%rbp)
movq $0x0, -0x1450(%rbp)
movq $0x1, -0x1458(%rbp)
movq $0x0, -0x1460(%rbp)
movb $-0x1, -0x1466(%rbp)
movb $-0x1, -0x1465(%rbp)
movq $0x0, -0x1448(%rbp)
movq $0x0, -0x1450(%rbp)
movq $0x0, -0x1890(%rbp)
movq -0x1440(%rbp), %rsi
leaq -0x1434(%rbp), %rdi
callq 0x19c0
cltq
movq %rax, -0x1890(%rbp)
cmpq $-0x1, -0x1890(%rbp)
jne 0x14bf
jmp 0x1770
cmpq $0x1, -0x1890(%rbp)
jne 0x1550
movq -0x1458(%rbp), %rcx
leaq 0x3b58(%rip), %rdi      # 0x5033
leaq 0x3b84(%rip), %rsi      # 0x5066
movl $0x1e8, %edx            # imm = 0x1E8
movb $0x0, %al
callq 0x3fd0
movq -0x1460(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1460(%rbp)
movq 0x6af1(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq -0x1434(%rbp), %rdx
addq $0x41e, %rdx            # imm = 0x41E
leaq -0x1434(%rbp), %rcx
addq $0x81e, %rcx            # imm = 0x81E
leaq -0x1434(%rbp), %r8
addq $0xc1e, %r8             # imm = 0xC1E
leaq -0x1434(%rbp), %r9
addq $0x101e, %r9            # imm = 0x101E
leaq 0x3b2b(%rip), %rsi      # 0x5074
movb $0x0, %al
callq 0x1170
cmpq $0x0, -0x1890(%rbp)
jne 0x172a
movsbl -0x1465(%rbp), %eax
cmpl $-0x1, %eax
je 0x157c
movsbl -0x1465(%rbp), %eax
movsbl -0x1018(%rbp), %ecx
cmpl %ecx, %eax
jle 0x1588
movb -0x1018(%rbp), %al
movb %al, -0x1465(%rbp)
movsbl -0x1466(%rbp), %eax
cmpl $-0x1, %eax
je 0x15a6
movsbl -0x1466(%rbp), %eax
movsbl -0x1017(%rbp), %ecx
cmpl %ecx, %eax
jge 0x15b2
movb -0x1017(%rbp), %al
movb %al, -0x1466(%rbp)
cmpq $0x0, -0x1448(%rbp)
je 0x15cd
cmpq $0x4c4b40, -0x1450(%rbp) # imm = 0x4C4B40
jle 0x16a2
cmpq $0x0, -0x1448(%rbp)
je 0x15fb
movq 0x6a1a(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3cda(%rip), %rsi      # 0x52c2
movb $0x0, %al
callq 0x1170
movq -0x1448(%rbp), %rdi
callq 0x1030
movl -0x1464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1464(%rbp)
leaq -0x1880(%rbp), %rdi
movq -0x1480(%rbp), %rcx
movl -0x1464(%rbp), %r8d
movl $0x400, %esi            # imm = 0x400
leaq 0x3a5e(%rip), %rdx      # 0x5089
movb $0x0, %al
callq 0x1130
movl %eax, -0x1884(%rbp)
cmpl $0x400, -0x1884(%rbp)   # imm = 0x400
jle 0x165e
leaq 0x3a4b(%rip), %rdi      # 0x5096
leaq 0x3a14(%rip), %rsi      # 0x5066
movl $0x204, %edx            # imm = 0x204
movb $0x0, %al
callq 0x3ed0
movq 0x6993(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq -0x1880(%rbp), %rdx
leaq 0x3a3a(%rip), %rsi      # 0x50b0
movb $0x0, %al
callq 0x1170
leaq -0x1880(%rbp), %rdi
leaq 0x3a3b(%rip), %rsi      # 0x50c6
callq 0x3b40
movq %rax, -0x1448(%rbp)
movq $0x0, -0x1450(%rbp)
movq -0x1448(%rbp), %rdi
leaq -0x1434(%rbp), %rdx
addq $0x41e, %rdx            # imm = 0x41E
leaq -0x1434(%rbp), %rcx
addq $0x81e, %rcx            # imm = 0x81E
leaq -0x1434(%rbp), %r8
addq $0xc1e, %r8             # imm = 0xC1E
leaq -0x1434(%rbp), %r9
addq $0x101e, %r9            # imm = 0x101E
leaq 0x39e1(%rip), %rsi      # 0x50c9
movb $0x0, %al
callq 0x1100
movl %eax, -0x1884(%rbp)
cmpl $0x0, -0x1884(%rbp)
jne 0x1718
leaq 0x39d1(%rip), %rdi      # 0x50d6
leaq 0x395a(%rip), %rsi      # 0x5066
movl $0x211, %edx            # imm = 0x211
movb $0x0, %al
callq 0x3ed0
movq -0x1450(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1450(%rbp)
movq -0x1458(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x1458(%rbp)
movq -0x1450(%rbp), %rax
movl $0x186a0, %ecx          # imm = 0x186A0
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x176b
movq 0x689e(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3d78(%rip), %rsi      # 0x54dc
movb $0x0, %al
callq 0x1170
jmp 0x1489
movb -0x1465(%rbp), %al
movsbl %al, %edi
movsbl -0x1466(%rbp), %esi
callq 0x1b40
movq 0x686c(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
leaq 0x3b2c(%rip), %rsi      # 0x52c2
movb $0x0, %al
callq 0x1170
movq 0x6854(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq -0x1450(%rbp), %rdx
movq -0x1460(%rbp), %rcx
leaq 0x3940(%rip), %rsi      # 0x50fc
movb $0x0, %al
callq 0x1170
movq -0x1440(%rbp), %rdi
callq 0x1030
cmpq $0x0, -0x1448(%rbp)
je 0x17e5
movq -0x1448(%rbp), %rdi
callq 0x1030
movq -0x1480(%rbp), %rdi
leaq 0x3873(%rip), %rsi      # 0x5066
movl $0x228, %edx            # imm = 0x228
callq 0x4170
movq $0x0, -0x1480(%rbp)
xorl %eax, %eax
addq $0x1890, %rsp           # imm = 0x1890
popq %rbp
retq
nopw %cs:(%rax,%rax)