// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x2, %edi
jl 0x34df
movl %edi, %ebp
movl $0x1, %r14d
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movslq %r14d, %rax
movq (%rbx,%rax,8), %r13
movq %r13, %rdi
leaq 0x4c64(%rip), %rsi      # 0x804a
callq 0x30a0
testl %eax, %eax
je 0x3639
leal 0x1(%r14), %r15d
movq %r13, %rdi
leaq 0x4c4c(%rip), %rsi      # 0x804d
callq 0x30a0
testl %eax, %eax
je 0x34af
movq %r13, %rdi
leaq 0x4c38(%rip), %rsi      # 0x8050
callq 0x30a0
testl %eax, %eax
je 0x34b4
movq %r13, %rdi
leaq 0x4c29(%rip), %rsi      # 0x8058
callq 0x30a0
testl %eax, %eax
je 0x34be
movq %r13, %rdi
leaq 0x4c1a(%rip), %rsi      # 0x8060
callq 0x30a0
testl %eax, %eax
jne 0x345c
cmpq $0x0, 0x18(%rsp)
leaq 0x18(%rsp), %rcx
je 0x348a
movq %r13, %rdi
leaq 0x4bfd(%rip), %rsi      # 0x8063
callq 0x30a0
cmpq $0x0, 0x10(%rsp)
movl $0x0, %ecx
leaq 0x10(%rsp), %rdx
cmoveq %rdx, %rcx
testl %eax, %eax
movl $0x0, %eax
cmovneq %rax, %rcx
testq %rcx, %rcx
je 0x364c
cmpl %ebp, %r15d
je 0x3641
addl $0x2, %r14d
movslq %r15d, %rax
movq (%rbx,%rax,8), %rax
movq %rax, (%rcx)
movl %r14d, %r15d
jmp 0x34c6
incl %r12d
jmp 0x34c6
movl $0x0, 0xc(%rsp)
jmp 0x34c6
movl $0x1, 0xc(%rsp)
movl %r15d, %r14d
cmpl %ebp, %r15d
jl 0x33d5
testl %r12d, %r12d
setg %al
movl 0xc(%rsp), %r14d
jmp 0x34e5
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %r14d, %r14d
js 0x3657
testb %al, %al
jne 0x35c5
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x3519
xorl %esi, %esi
xorl %eax, %eax
callq 0x3100
movl %eax, %ebp
testl %eax, %eax
jns 0x351b
leaq 0x18(%rsp), %rdi
callq 0x31e4
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3546
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x3100
movl %eax, %ebx
testl %eax, %eax
jns 0x354b
leaq 0x10(%rsp), %rdi
callq 0x31f5
movl $0x1, %ebx
callq 0x3664
testl %r14d, %r14d
je 0x365f
leaq 0x4bb8(%rip), %rsi      # 0x8118
movl %ebx, %edi
callq 0x30e0
movq %rax, %r14
leaq 0x4ba9(%rip), %rsi      # 0x811a
movl %ebp, %edi
callq 0x30e0
movq %rax, %r15
leaq 0x24(%rsp), %rcx
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x3cf2
movq %r15, %rdi
callq 0x3050
movq %r14, %rdi
callq 0x3050
callq 0x3664
cmpq $0x0, 0x10(%rsp)
je 0x35b4
movl %ebx, %edi
callq 0x3090
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r14d, %r14d
je 0x35d9
cmpl $0x1, %r14d
jne 0x35e5
leaq 0x4af3(%rip), %rdi      # 0x80ca
jmp 0x35e0
leaq 0x4adc(%rip), %rdi      # 0x80bc
callq 0x31c9
movq 0x8a0c(%rip), %rbx      # 0xbff8
movq (%rbx), %rdi
movq 0x18(%rsp), %rax
testq %rax, %rax
leaq 0x4ae5(%rip), %rdx      # 0x80e3
cmovneq %rax, %rdx
leaq 0x4acf(%rip), %rsi      # 0x80d8
xorl %eax, %eax
callq 0x30b0
movq (%rbx), %rdi
movq 0x10(%rsp), %rax
testq %rax, %rax
leaq 0x4ad3(%rip), %rdx      # 0x80f5
cmovneq %rax, %rdx
leaq 0x4abc(%rip), %rsi      # 0x80e9
xorl %eax, %eax
callq 0x30b0
jmp 0x34f6
movq %rbx, %rdi
callq 0x3258
movq %rbx, %rdi
movq %r13, %rsi
callq 0x316b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x319a
movq %rbx, %rdi
callq 0x322b
callq 0x3206