// File: /workspace/github_c_makefile_star2_O1/jeff-1amstudios[P]carmageddon-1-cheat-code-disassembly/src/utility.c
// Function: EncodeFile
void EncodeFile(char *pThe_path)
{
  char line[257];
  char new_file[256];
  FILE *f;
  FILE *d;
  char *s;
  char *result;
  int ch;
  int decode;
  int len;
  int count;

  len = strlen(pThe_path);
  strcpy(new_file, pThe_path);
  strcat(new_file, "ENC");

  f = fopen(pThe_path, "rt");
  if (!f) {
    FatalError(0x6b, new_file);
  }

  ch = fgetc(f);
  ungetc(ch, f);

  if (gDecodeThing == '@' && gDecodeThing == (char)ch) {
    fclose(f);
    return;
  }

  d = fopen(new_file, "wb");
  if (!d) {
    FatalError(0x6b, new_file);
  }

  result = &line[1];

  while (!feof(f)) {
    fgets(result, 256, f);

    if (ch == '@') {
      decode = 1;
    } else {
      decode = 0;
      s = &result[1];
      while (line[0] == ' ' || line[0] == '\t') {
        memmove(result, s, strlen(result));
      }
    }

    if (decode == 0) {
      EncodeLine2(result + decode);
    } else {
      DecodeLine2(result + decode);
    }

    line[0] = '@';
    fputs(&line[decode*2], d);
    count = -1;
    ch = fgetc(f);
    while (ch == '\r' || ch == '\n') {
      count++;
    }
    if (count >= 2) {
      fputc(0x0d, d);
      fputc(0x0a, d);
    }
    fputc(0x0d, d);
    fputc(0x0a, d);

    if (ch != -1) {
      ungetc(ch, f);
    }
  }
  fclose(f);
  fclose(d);

  PDFileUnlock(pThe_path);
  unlink(pThe_path);
  rename(new_file, pThe_path);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdi, %r15
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2070
movq %rbx, %rdi
callq 0x20d0
movl $0x434e45, 0x10(%rsp,%rax) # imm = 0x434E45
leaq 0x58bf(%rip), %rsi      # 0x8205
movq %r15, %rdi
callq 0x21f0
movq %rax, %r14
testq %rax, %rax
jne 0x2967
leaq 0x10(%rsp), %rsi
movl $0x6b, %edi
xorl %eax, %eax
callq 0x2438
movq %r14, %rdi
callq 0x2120
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
callq 0x21c0
cmpb $0x40, %bpl
jne 0x299a
leaq 0x9130(%rip), %rax      # 0xbab8
cmpb $0x40, (%rax)
jne 0x299a
movq %r14, %rdi
callq 0x20c0
jmp 0x2ac2
movq %r15, 0x8(%rsp)
leaq 0x5862(%rip), %rsi      # 0x8208
leaq 0x10(%rsp), %rdi
callq 0x21f0
movq %rax, %r15
testq %rax, %rax
jne 0x29c9
leaq 0x10(%rsp), %rsi
movl $0x6b, %edi
xorl %eax, %eax
callq 0x2438
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
jne 0x2a90
leaq 0x112(%rsp), %r12
leaq 0x111(%rsp), %r13
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2140
movq %r12, %rbx
cmpl $0x40, %ebp
je 0x2a23
movzbl 0x110(%rsp), %eax
cmpl $0x20, %eax
je 0x2ad4
leaq 0x110(%rsp), %rbx
cmpl $0x9, %eax
je 0x2ad4
cmpl $0x40, %ebp
jne 0x2a32
movq %r12, %rdi
callq 0x2bbc
jmp 0x2a3a
movq %r13, %rdi
callq 0x2aec
movb $0x40, 0x110(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2100
movq %r14, %rdi
callq 0x2120
movl %eax, %ebp
movl $0xd, %edi
movq %r15, %rsi
callq 0x2130
movl $0xa, %edi
movq %r15, %rsi
callq 0x2130
cmpl $-0x1, %ebp
je 0x2a80
movl %ebp, %edi
movq %r14, %rsi
callq 0x21c0
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
je 0x29e9
movq %r14, %rdi
callq 0x20c0
movq %r15, %rdi
callq 0x20c0
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2d24
movq %rbx, %rdi
callq 0x2050
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2200
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x20d0
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x2ad4