// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rax, 0x8(%rsp)
cmpl $0x2, %edi
jl 0x3642
movl %edi, %ebp
movl $0x1, %r13d
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
leal 0x1(%r13), %r15d
movslq %r13d, %rax
movq (%rbx,%rax,8), %r12
movb (%r12), %r14b
cmpb $0x2d, %r14b
jne 0x3408
cmpb $0x68, 0x1(%r12)
jne 0x33f4
cmpb $0x0, 0x2(%r12)
je 0x363a
cmpb $0x76, 0x1(%r12)
jne 0x3408
cmpb $0x0, 0x2(%r12)
je 0x34ae
movq %r12, %rdi
leaq 0x4c38(%rip), %rsi      # 0x804a
callq 0x30a0
testl %eax, %eax
je 0x349a
movq %r12, %rdi
leaq 0x4c2d(%rip), %rsi      # 0x8052
callq 0x30a0
testl %eax, %eax
je 0x34a4
cmpb $0x2d, %r14b
jne 0x3624
cmpb $0x69, 0x1(%r12)
jne 0x3455
cmpb $0x0, 0x2(%r12)
jne 0x3455
cmpq $0x0, 0x10(%rsp)
leaq 0x10(%rsp), %rax
je 0x347e
cmpb $0x6f, 0x1(%r12)
jne 0x3624
cmpb $0x0, 0x2(%r12)
jne 0x3624
cmpq $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rax
jne 0x3624
cmpl %ebp, %r15d
je 0x362f
addl $0x2, %r13d
movslq %r15d, %rcx
movq (%rbx,%rcx,8), %rcx
movq %rcx, (%rax)
movl %r13d, %r15d
jmp 0x34ba
movl $0x0, 0x4(%rsp)
jmp 0x34ba
movl $0x1, 0x4(%rsp)
jmp 0x34ba
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movl %r15d, %r13d
cmpl %ebp, %r15d
jl 0x33cb
movl 0x4(%rsp), %r15d
testl %r15d, %r15d
js 0x3642
cmpl $0x0, 0x18(%rsp)
jg 0x35b0
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x3504
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3100
movl %eax, %r14d
testl %eax, %eax
jns 0x3507
movq %rbx, %rdi
callq 0x31de
xorl %r14d, %r14d
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x3533
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 0x3538
movq %rbx, %rdi
callq 0x31ec
movl $0x1, %ebp
callq 0x364f
testl %r15d, %r15d
je 0x364a
leaq 0x4bbf(%rip), %rsi      # 0x810c
movl %ebp, %edi
callq 0x30e0
movq %rax, %r15
leaq 0x4bb0(%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 0x3c0a
movq %r14, %rdi
callq 0x3050
movq %r15, %rdi
callq 0x3050
callq 0x364f
testq %rbx, %rbx
je 0x359f
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 0x35c4
cmpl $0x1, %r15d
jne 0x35d0
leaq 0x4afc(%rip), %rdi      # 0x80be
jmp 0x35cb
leaq 0x4ae5(%rip), %rdi      # 0x80b0
callq 0x31c9
movq 0x8a21(%rip), %r14      # 0xbff8
movq (%r14), %rdi
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
leaq 0x4aee(%rip), %rdx      # 0x80d7
cmovneq %rbx, %rdx
leaq 0x4ad8(%rip), %rsi      # 0x80cc
xorl %eax, %eax
callq 0x30b0
movq (%r14), %rdi
movq 0x8(%rsp), %rax
testq %rax, %rax
leaq 0x4adc(%rip), %rdx      # 0x80e9
cmovneq %rax, %rdx
leaq 0x4ac5(%rip), %rsi      # 0x80dd
xorl %eax, %eax
callq 0x30b0
jmp 0x34e4
movq %rbx, %rdi
movq %r12, %rsi
callq 0x316b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x319a
movq %rbx, %rdi
callq 0x324c
movq %rbx, %rdi
callq 0x321f
callq 0x31fa