// File: /workspace/github_c_makefile_star2_O2/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 0x493b(%rip), %rsi      # 0x7205
movq %r15, %rdi
callq 0x21f0
movq %rax, %r14
testq %rax, %rax
jne 0x28e9
pushq $0x6b
popq %rdi
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x23dc
movq %r14, %rdi
callq 0x2120
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
callq 0x21c0
cmpb $0x40, %bpl
jne 0x291c
leaq 0x91ae(%rip), %rax      # 0xbab8
cmpb $0x40, (%rax)
jne 0x291c
movq %r14, %rdi
callq 0x20c0
jmp 0x2a41
movq %r15, 0x8(%rsp)
leaq 0x48e0(%rip), %rsi      # 0x7208
leaq 0x10(%rsp), %rdi
callq 0x21f0
movq %rax, %r15
testq %rax, %rax
jne 0x2949
pushq $0x6b
popq %rdi
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x23dc
leaq 0x112(%rsp), %r12
leaq 0x111(%rsp), %r13
pushq $0xa
popq %rbx
movq %r14, %rdi
callq 0x2190
testl %eax, %eax
jne 0x2a0f
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2140
cmpl $0x40, %ebp
jne 0x298e
movq %r12, %rdi
callq 0x2b0c
movq %r12, %rdi
jmp 0x29b0
movzbl 0x110(%rsp), %eax
cmpl $0x20, %eax
je 0x29f7
cmpl $0x9, %eax
je 0x29f7
movq %r13, %rdi
callq 0x2a53
leaq 0x110(%rsp), %rdi
movb $0x40, 0x110(%rsp)
movq %r15, %rsi
callq 0x2100
movq %r14, %rdi
callq 0x2120
movl %eax, %ebp
pushq $0xd
popq %rdi
movq %r15, %rsi
callq 0x2130
movl %ebx, %edi
movq %r15, %rsi
callq 0x2130
cmpl $-0x1, %ebp
je 0x295c
movl %ebp, %edi
movq %r14, %rsi
callq 0x21c0
jmp 0x295c
movq %r13, %rdi
callq 0x20d0
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x29f7
movq %r14, %rdi
callq 0x20c0
movq %r15, %rdi
callq 0x20c0
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2c83
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