// File: /workspace/github_c_makefile_star2_O1/otrtool[P]otrtool/src/main.c
// Function: keycache_open
void keycache_open() {
  char *home = NULL, *data_home = NULL, *pch;
  char *filenames[2] = { NULL, NULL };
  const int n_filenames = 2;
  int i;

  /* Build home and data home strings */
  if ((pch = getenv("HOME")) != NULL && strlen(pch) > 0)
    home = strdup(pch);
  if ((pch = getenv("XDG_DATA_HOME")) != NULL && strlen(pch) > 0)
    data_home = strdup(pch);
  if (data_home == NULL && home != NULL
      && (data_home = malloc(strlen(home) + 14)) != NULL) {
    strcpy(data_home, home);
    strcat(data_home, "/.local/share");
  }
  if (!home && !data_home)
    fputs("warning: neither HOME nor XDG_DATA_HOME is set\n", stderr);

  /* Make array of file name alternatives */
  if (home && (filenames[0] = malloc(strlen(home) + 15)) != NULL) {
    strcpy(filenames[0], home);
    strcat(filenames[0], "/.otrkey_cache");
  }
  if (data_home && (filenames[1] = malloc(strlen(data_home) + 22)) != NULL) {
    strcpy(filenames[1], data_home);
    strcat(filenames[1], "/otrtool/otrkey_cache");
  }

  /* Choose the first existing cache file alternative */
  keyfilename = NULL;
  for (i=0; i < n_filenames; ++i) {
    if (filenames[i] && access(filenames[i], F_OK) == 0) break;
  }
  if (i == n_filenames) i = 0; /* choose default */
  if (filenames[i]) keyfilename = strdup(filenames[i]);

  if (keyfilename) {
    /* Construct abbreviated key file name (replace HOME with ~) */
    if (home && strncmp(keyfilename, home, strlen(home)) == 0) {
      if ((pch = malloc(strlen(keyfilename)-strlen(home)+2)) == NULL)
        PERROR("malloc");
      strcpy(pch, "~");
      strcat(pch, keyfilename+strlen(home));
      keyfileshortname = pch;
    }
    else {
      if ((keyfileshortname = strdup(keyfilename)) == NULL) PERROR("strdup");
    }
    if(opts.verbosity >= VERB_DEBUG)
      fprintf(stderr, "key cache is at %s\n", keyfileshortname);

    /* Open key cache */
    keyfile = fopen(keyfilename, "a+");
    if (keyfile == NULL && opts.verbosity >= VERB_DEBUG)
      perrorf("cannot open key cache: %s", keyfilename);
  }

  for (i=0; i < n_filenames; ++i) free(filenames[i]);
  free(home);
  free(data_home);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x2dcb(%rip), %rdi      # 0x857c
callq 0x22a0
testq %rax, %rax
je 0x57cd
cmpb $0x0, (%rax)
je 0x57cd
movq %rax, %rdi
callq 0x2440
movq %rax, %rbx
jmp 0x57cf
xorl %ebx, %ebx
leaq 0x2d9d(%rip), %rdi      # 0x8573
callq 0x22a0
testq %rax, %rax
je 0x57f2
cmpb $0x0, (%rax)
je 0x57f2
movq %rax, %rdi
callq 0x2440
movq %rax, %r14
jmp 0x57f5
xorl %r14d, %r14d
testq %r14, %r14
sete %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5856
movq %rbx, %rdi
callq 0x20c0
leaq 0xe(%rax), %rdi
callq 0x2300
testq %rax, %rax
je 0x5853
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
movq %r14, %rdi
callq 0x20c0
movabsq $0x2f6c61636f6c2e2f, %rcx # imm = 0x2F6C61636F6C2E2F
movq %rcx, (%r14,%rax)
movabsq $0x65726168732f6c, %rcx # imm = 0x65726168732F6C
movq %rcx, 0x6(%r14,%rax)
jmp 0x5856
xorl %r14d, %r14d
movq %rbx, %rax
orq %r14, %rax
je 0x5a3d
testq %rbx, %rbx
je 0x58b4
movq %rbx, %rdi
callq 0x20c0
leaq 0xf(%rax), %rdi
callq 0x2300
movq %rax, (%rsp)
testq %rax, %rax
je 0x58b4
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
movq %r15, %rdi
callq 0x20c0
movabsq $0x79656b72746f2e2f, %rcx # imm = 0x79656B72746F2E2F
movq %rcx, (%r15,%rax)
movabsq $0x65686361635f79, %rcx # imm = 0x65686361635F79
movq %rcx, 0x7(%r15,%rax)
testq %r14, %r14
je 0x5905
movq %r14, %rdi
callq 0x20c0
leaq 0x16(%rax), %rdi
callq 0x2300
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x5905
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %r15, %rdi
callq 0x20c0
movabsq $0x65686361635f79, %rcx # imm = 0x65686361635F79
movq %rcx, 0xe(%r15,%rax)
movups 0x2cce(%rip), %xmm0     # 0x85ce
movups %xmm0, (%r15,%rax)
movq $0x0, 0x69e0(%rip) # 0xc2f0
xorl %r15d, %r15d
movq (%rsp,%r15,8), %rdi
testq %rdi, %rdi
je 0x5927
xorl %esi, %esi
callq 0x23e0
testl %eax, %eax
je 0x5936
incq %r15
cmpq $0x1, %r15
je 0x5913
movl $0x2, %r15d
xorl %eax, %eax
cmpl $0x2, %r15d
cmovnel %r15d, %eax
movq (%rsp,%rax,8), %rdi
testq %rdi, %rdi
je 0x5955
callq 0x2440
movq %rax, 0x699b(%rip)      # 0xc2f0
movq 0x6994(%rip), %r15      # 0xc2f0
testq %r15, %r15
je 0x5a0b
testq %rbx, %rbx
je 0x5987
movq %rbx, %rdi
callq 0x20c0
movq %rax, %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x20e0
testl %eax, %eax
je 0x59a0
movq %r15, %rdi
callq 0x2440
movq %rax, 0x6962(%rip)      # 0xc2f8
testq %rax, %rax
jne 0x59da
callq 0x2e92
movq %r15, %rdi
callq 0x20c0
subq %r12, %rax
leaq 0x2(%rax), %rdi
callq 0x2300
testq %rax, %rax
je 0x5a55
movq %rax, %r13
movw $0x7e, (%rax)
addq %r12, %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x2420
movq %r13, 0x691e(%rip)      # 0xc2f8
cmpb $0x1, 0x68a7(%rip)      # 0xc288
je 0x5a4e
movq 0x6906(%rip), %rdi      # 0xc2f0
leaq 0x2c15(%rip), %rsi      # 0x8606
callq 0x2270
movq %rax, 0x6903(%rip)      # 0xc300
testq %rax, %rax
jne 0x5a0b
testb $0x1, 0x687f(%rip)      # 0xc288
jne 0x5a47
xorl %r15d, %r15d
movq (%rsp,%r15,8), %rdi
callq 0x2280
incq %r15
cmpq $0x1, %r15
je 0x5a0e
movq %rbx, %rdi
callq 0x2280
movq %r14, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x2280
callq 0x2e7c
jmp 0x5862
callq 0x2f03
jmp 0x5a0b
callq 0x2ee4
jmp 0x59e3
callq 0x2f18