// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x20d0
movl %eax, -0x23c(%rbp)
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x2070
leaq -0x210(%rbp), %rdi
leaq 0x74e9(%rip), %rsi      # 0xa205
callq 0x2210
movq -0x8(%rbp), %rdi
leaq 0x74dd(%rip), %rsi      # 0xa209
callq 0x21f0
movq %rax, -0x218(%rbp)
cmpq $0x0, -0x218(%rbp)
jne 0x2d55
leaq -0x210(%rbp), %rsi
movl $0x6b, %edi
movb $0x0, %al
callq 0x24f0
movq -0x218(%rbp), %rdi
callq 0x2120
movl %eax, -0x234(%rbp)
movl -0x234(%rbp), %edi
movq -0x218(%rbp), %rsi
callq 0x21c0
leaq 0xad48(%rip), %rax      # 0xdac8
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x2db0
leaq 0xad39(%rip), %rax      # 0xdac8
movsbl (%rax), %eax
movl -0x234(%rbp), %ecx
movsbl %cl, %ecx
cmpl %ecx, %eax
jne 0x2db0
movq -0x218(%rbp), %rdi
callq 0x20c0
jmp 0x3036
leaq -0x210(%rbp), %rdi
leaq 0x744e(%rip), %rsi      # 0xa20c
callq 0x21f0
movq %rax, -0x220(%rbp)
cmpq $0x0, -0x220(%rbp)
jne 0x2de7
leaq -0x210(%rbp), %rsi
movl $0x6b, %edi
movb $0x0, %al
callq 0x24f0
leaq -0x110(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x230(%rbp)
movq -0x218(%rbp), %rdi
callq 0x2190
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2e16
jmp 0x2ffc
movq -0x230(%rbp), %rdi
movq -0x218(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x2140
cmpl $0x40, -0x234(%rbp)
jne 0x2e46
movl $0x1, -0x238(%rbp)
jmp 0x2ed7
movl $0x0, -0x238(%rbp)
movq -0x230(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x228(%rbp)
movsbl -0x110(%rbp), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x241(%rbp)
je 0x2e89
movsbl -0x110(%rbp), %eax
cmpl $0x9, %eax
sete %al
movb %al, -0x241(%rbp)
movb -0x241(%rbp), %al
testb $0x1, %al
jne 0x2e95
jmp 0x2ed5
movq -0x230(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x228(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x230(%rbp), %rdi
callq 0x20d0
movq -0x258(%rbp), %rdi
movq -0x250(%rbp), %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x2e62
jmp 0x2ed7
cmpl $0x0, -0x238(%rbp)
jne 0x2ef8
movq -0x230(%rbp), %rdi
movslq -0x238(%rbp), %rax
addq %rax, %rdi
callq 0x3040
jmp 0x2f0e
movq -0x230(%rbp), %rdi
movslq -0x238(%rbp), %rax
addq %rax, %rdi
callq 0x31c0
movb $0x40, -0x110(%rbp)
movl -0x238(%rbp), %eax
shll %eax
cltq
leaq -0x110(%rbp), %rdi
addq %rax, %rdi
movq -0x220(%rbp), %rsi
callq 0x2100
movl $0xffffffff, -0x240(%rbp) # imm = 0xFFFFFFFF
movq -0x218(%rbp), %rdi
callq 0x2120
movl %eax, -0x234(%rbp)
movb $0x1, %al
cmpl $0xd, -0x234(%rbp)
movb %al, -0x259(%rbp)
je 0x2f72
cmpl $0xa, -0x234(%rbp)
sete %al
movb %al, -0x259(%rbp)
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x2f7e
jmp 0x2f8f
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x2f51
cmpl $0x2, -0x240(%rbp)
jl 0x2fba
movq -0x220(%rbp), %rsi
movl $0xd, %edi
callq 0x2130
movq -0x220(%rbp), %rsi
movl $0xa, %edi
callq 0x2130
movq -0x220(%rbp), %rsi
movl $0xd, %edi
callq 0x2130
movq -0x220(%rbp), %rsi
movl $0xa, %edi
callq 0x2130
cmpl $-0x1, -0x234(%rbp)
je 0x2ff7
movl -0x234(%rbp), %edi
movq -0x218(%rbp), %rsi
callq 0x21c0
jmp 0x2df9
movq -0x218(%rbp), %rdi
callq 0x20c0
movq -0x220(%rbp), %rdi
callq 0x20c0
movq -0x8(%rbp), %rdi
callq 0x3460
movq -0x8(%rbp), %rdi
callq 0x2050
leaq -0x210(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x2200
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nop