// File: /workspace/github_c_makefile_star2_O3/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 $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2070
movq %r14, %rdi
callq 0x20d0
movl $0x434e45, (%rsp,%rax)  # imm = 0x434E45
leaq 0x58d2(%rip), %rsi      # 0x8205
movq %rbx, %rdi
callq 0x21f0
movq %rax, %r14
testq %rax, %rax
jne 0x2952
movq %rsp, %rsi
movl $0x6b, %edi
xorl %eax, %eax
callq 0x2428
movq %r14, %rdi
callq 0x2120
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
callq 0x21c0
cmpb $0x40, %bpl
jne 0x2985
leaq 0x9145(%rip), %rax      # 0xbab8
cmpb $0x40, (%rax)
jne 0x2985
movq %r14, %rdi
callq 0x20c0
jmp 0x2a95
leaq 0x587c(%rip), %rsi      # 0x8208
movq %rsp, %rdi
callq 0x21f0
movq %rax, %r15
testq %rax, %rax
jne 0x29ab
movq %rsp, %rsi
movl $0x6b, %edi
xorl %eax, %eax
callq 0x2428
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
jne 0x2a6a
leaq 0x102(%rsp), %r12
leaq 0x101(%rsp), %r13
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2140
cmpl $0x40, %ebp
jne 0x29ed
movq %r12, %rdi
callq 0x2b9a
movq %r12, %rdi
jmp 0x2a17
movzbl 0x100(%rsp), %eax
cmpl $0x20, %eax
je 0x2aa7
cmpl $0x9, %eax
je 0x2aa7
movq %r13, %rdi
callq 0x2abf
leaq 0x100(%rsp), %rdi
movb $0x40, 0x100(%rsp)
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 0x2a5a
movl %ebp, %edi
movq %r14, %rsi
callq 0x21c0
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
je 0x29cb
movq %r14, %rdi
callq 0x20c0
movq %r15, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2d08
movq %rbx, %rdi
callq 0x2050
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x2200
addq $0x208, %rsp            # imm = 0x208
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 0x2aa7