// File: /workspace/github_cpp_makefile_star2_O2/sbingner[P]kerneldec/lzfse/src/lzfse_main.c
// Function: main
int main(int argc, char **argv) {
  const char *in_file = 0;  // stdin
  const char *out_file = 0; // stdout
  int op = -1;              // invalid op
  int verbosity = 0;        // quiet

  // Parse options
  for (int i = 1; i < argc;) {
    // no args
    const char *a = argv[i++];
    if (strcmp(a, "-h") == 0)
      USAGE(argc, argv);
    if (strcmp(a, "-v") == 0) {
      verbosity++;
      continue;
    }
    if (strcmp(a, "-encode") == 0) {
      op = LZFSE_ENCODE;
      continue;
    }
    if (strcmp(a, "-decode") == 0) {
      op = LZFSE_DECODE;
      continue;
    }

    // one arg
    const char **arg_var = 0;
    if (strcmp(a, "-i") == 0 && in_file == 0)
      arg_var = &in_file;
    else if (strcmp(a, "-o") == 0 && out_file == 0)
      arg_var = &out_file;
    if (arg_var != 0) {
      // Flag is recognized. Check if there is an argument.
      if (i == argc)
        USAGE_MSG(argc, argv, "Error: Missing arg after %s\n", a);
      *arg_var = argv[i++];
      continue;
    }

    USAGE_MSG(argc, argv, "Error: invalid flag %s\n", a);
  }
  if (op < 0)
    USAGE_MSG(argc, argv, "Error: -encode|-decode required\n");

  // Info
  if (verbosity > 0) {
    if (op == LZFSE_ENCODE)
      fprintf(stderr, "LZFSE encode\n");
    if (op == LZFSE_DECODE)
      fprintf(stderr, "LZFSE decode\n");
    fprintf(stderr, "Input: %s\n", in_file ? in_file : "stdin");
    fprintf(stderr, "Output: %s\n", out_file ? out_file : "stdout");
  }

  // Load input
  size_t out_size = 0;     // used in OUT
  int in_fd = -1;          // input file desc

  if (in_file != 0) {
    // If we have a file name, open it, and allocate the exact input size
    struct stat st;
#if defined(_WIN32)
    in_fd = open(in_file, O_RDONLY | O_BINARY);
#else
    in_fd = open(in_file, O_RDONLY);
#endif
    if (in_fd < 0) {
      perror(in_file);
      exit(1);
    }
  } else {
    in_fd = 0;
#if defined(_WIN32)
    if (setmode(in_fd, O_BINARY) == -1) {
      perror("setmode");
      exit(1);
    }
#endif
  }

  int out_fd = -1;
  if (out_file) {
#if defined(_WIN32)
    out_fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
      S_IWRITE);
#else
    out_fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
#endif
    if (out_fd < 0) {
      perror(out_file);
      exit(1);
    }
  } else {
    out_fd = 1; // stdout
#if defined(_WIN32)
    if (setmode(out_fd, O_BINARY) == -1) {
      perror("setmode");
      exit(1);
    }
#endif
  }
  //  Encode/decode
  //  Compute size for result buffer; we assume here that encode shrinks size,
  //  and that decode grows by no more than 4x.  These are reasonable common-
  //  case guidelines, but are not formally guaranteed to be satisfied.

  double c0 = get_time();
  if (op == LZFSE_ENCODE) {
      fprintf(stderr, "encode not implemented yet\n");
      exit(1);
  } else {
      FILE *outfile = fdopen(out_fd, "w");
      FILE *infile = fdopen(in_fd, "r");
      int error;
      out_size = lzfse_decode_file(outfile, infile, 0, &error, NULL);
      fclose(infile);
      fclose(outfile);
  }

  double c1 = get_time();

  if (out_file != 0) {
    close(out_fd);
    out_fd = -1;
  }

  return 0; // OK
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
leaq 0x10(%rsp), %rax
andq $0x0, (%rax)
movl %edi, %r14d
leal -0x1(%r14), %ebp
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
pushq $0x1
popq %rax
movl %eax, %r12d
movslq %r12d, %r12
cmpl %r14d, %r12d
jge 0x338a
movq (%rbx,%r12,8), %r13
movb (%r13), %r15b
cmpb $0x2d, %r15b
jne 0x32d4
cmpb $0x68, 0x1(%r13)
jne 0x32c2
cmpb $0x0, 0x2(%r13)
je 0x353b
cmpb $0x76, 0x1(%r13)
jne 0x32d4
cmpb $0x0, 0x2(%r13)
je 0x337e
movq %r13, %rdi
leaq 0x4d6c(%rip), %rsi      # 0x804a
callq 0x30a0
testl %eax, %eax
je 0x336b
movq %r13, %rdi
leaq 0x4d5d(%rip), %rsi      # 0x8052
callq 0x30a0
testl %eax, %eax
je 0x3375
cmpb $0x2d, %r15b
jne 0x34f5
cmpb $0x69, 0x1(%r13)
jne 0x3323
cmpb $0x0, 0x2(%r13)
jne 0x3323
cmpq $0x0, 0x18(%rsp)
leaq 0x18(%rsp), %rax
je 0x334a
cmpb $0x6f, 0x1(%r13)
jne 0x34f5
cmpb $0x0, 0x2(%r13)
jne 0x34f5
cmpq $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rax
jne 0x34f5
cmpl %r12d, %ebp
je 0x3510
leaq 0x1(%r12), %rcx
movslq %ecx, %rcx
movq (%rbx,%rcx,8), %rcx
movq %rcx, (%rax)
addq $0x2, %r12
jmp 0x3299
movl $0x0, 0x8(%rsp)
jmp 0x3382
pushq $0x1
popq %rax
movl %eax, 0x8(%rsp)
jmp 0x3382
incl 0xc(%rsp)
incl %r12d
jmp 0x3296
movl 0x8(%rsp), %r15d
testl %r15d, %r15d
js 0x354a
cmpl $0x0, 0xc(%rsp)
jne 0x3471
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0x33c2
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3100
movl %eax, %r14d
testl %eax, %eax
jns 0x33c5
jmp 0x33e9
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x33f6
movq %rbx, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x3100
movl %eax, %ebp
testl %eax, %eax
jns 0x33f9
movq %rbx, %rdi
callq 0x3110
jmp 0x3533
pushq $0x1
popq %rbp
callq 0x3585
testl %r15d, %r15d
je 0x3567
leaq 0x4cfe(%rip), %rsi      # 0x810c
movl %ebp, %edi
callq 0x30e0
movq %rax, %r15
leaq 0x4cef(%rip), %rsi      # 0x810e
movl %r14d, %edi
callq 0x30e0
movq %rax, %r14
leaq 0x24(%rsp), %rcx
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x3b67
movq %r14, %rdi
callq 0x3050
movq %r15, %rdi
callq 0x3050
callq 0x3585
testq %rbx, %rbx
je 0x3460
movl %ebp, %edi
callq 0x3090
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
je 0x3485
cmpl $0x1, %r15d
jne 0x34a1
leaq 0x4c3b(%rip), %rdi      # 0x80be
jmp 0x348c
leaq 0x4c24(%rip), %rdi      # 0x80b0
movq 0x9b65(%rip), %rax      # 0xcff8
movq (%rax), %rcx
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x3130
movq 0x9b50(%rip), %r14      # 0xcff8
movq (%r14), %rdi
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
leaq 0x4c1d(%rip), %rdx      # 0x80d7
cmovneq %rbx, %rdx
leaq 0x4c07(%rip), %rsi      # 0x80cc
xorl %eax, %eax
callq 0x30b0
movq (%r14), %rdi
movq 0x10(%rsp), %rax
testq %rax, %rax
leaq 0x4c0b(%rip), %rdx      # 0x80e9
cmovneq %rax, %rdx
leaq 0x4bf4(%rip), %rsi      # 0x80dd
xorl %eax, %eax
callq 0x30b0
jmp 0x33a8
movq %rbx, %rsi
callq 0x323c
movq 0x9af4(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4b69(%rip), %rsi      # 0x8077
jmp 0x3529
movq %rbx, %rsi
callq 0x323c
movq 0x9ad9(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4b31(%rip), %rsi      # 0x805a
movq %r13, %rdx
xorl %eax, %eax
callq 0x30b0
pushq $0x1
popq %rdi
callq 0x3120
movq %rbx, %rsi
callq 0x323c
xorl %edi, %edi
callq 0x3120
movq %rbx, %rsi
callq 0x323c
movq 0x9a9f(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4b2c(%rip), %rdi      # 0x808f
pushq $0x20
jmp 0x357a
movq 0x9a8a(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4b78(%rip), %rdi      # 0x80f0
pushq $0x1b
popq %rsi
pushq $0x1
popq %rdx
callq 0x3130
jmp 0x3533