// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x3c26(%rip), %rdi      # 0x857c
callq 0x22b0
testq %rax, %rax
je 0x4972
cmpb $0x0, (%rax)
je 0x4972
movq %rax, %rdi
callq 0x2450
movq %rax, %rbx
jmp 0x4974
xorl %ebx, %ebx
leaq 0x3bf8(%rip), %rdi      # 0x8573
callq 0x22b0
testq %rax, %rax
je 0x4997
cmpb $0x0, (%rax)
je 0x4997
movq %rax, %rdi
callq 0x2450
movq %rax, %r14
jmp 0x499a
xorl %r14d, %r14d
testq %r14, %r14
sete %al
testq %rbx, %rbx
setne %cl
andb %al, %cl
movabsq $0x65686361635f79, %r12 # imm = 0x65686361635F79
cmpb $0x1, %cl
jne 0x4a09
movq %rbx, %rdi
callq 0x20c0
leaq 0xe(%rax), %rdi
callq 0x2310
movq %rax, %r14
testq %rax, %rax
je 0x4a00
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21e0
movq %r14, %rdi
callq 0x20c0
movabsq $0x2f6c61636f6c2e2f, %rcx # imm = 0x2F6C61636F6C2E2F
movq %rcx, (%r14,%rax)
movabsq $0x65726168732f6c, %rcx # imm = 0x65726168732F6C
movq %rcx, 0x6(%r14,%rax)
testq %r14, %r14
setne %bpl
jmp 0x4a21
testq %r14, %r14
setne %bpl
movq %rbx, %rax
orq %r14, %rax
je 0x4c34
testq %rbx, %rbx
je 0x4a64
movq %rbx, %rdi
callq 0x20c0
leaq 0xf(%rax), %rdi
callq 0x2310
movq %rax, (%rsp)
testq %rax, %rax
je 0x4a64
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movq %r15, %rdi
callq 0x20c0
movabsq $0x79656b72746f2e2f, %rcx # imm = 0x79656B72746F2E2F
movq %rcx, (%r15,%rax)
movq %r12, 0x7(%r15,%rax)
testb %bpl, %bpl
je 0x4aab
movq %r14, %rdi
callq 0x20c0
leaq 0x16(%rax), %rdi
callq 0x2310
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x4aab
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x21e0
movq %r15, %rdi
callq 0x20c0
movq %r12, 0xe(%r15,%rax)
movups 0x3b28(%rip), %xmm0     # 0x85ce
movups %xmm0, (%r15,%rax)
andq $0x0, 0x784d(%rip)      # 0xc300
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x4ad5
movq (%rsp,%r15,8), %rdi
testq %rdi, %rdi
je 0x4ad0
xorl %esi, %esi
callq 0x23f0
testl %eax, %eax
je 0x4ad8
incq %r15
jmp 0x4ab6
xorl %r15d, %r15d
movq (%rsp,%r15,8), %rdi
testq %rdi, %rdi
je 0x4af2
callq 0x2450
movq %rax, %r15
movq %rax, 0x7810(%rip)      # 0xc300
jmp 0x4af9
movq 0x7807(%rip), %r15      # 0xc300
testq %r15, %r15
je 0x4bd8
testq %rbx, %rbx
je 0x4b24
movq %rbx, %rdi
callq 0x20c0
movq %rax, %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x20e0
testl %eax, %eax
je 0x4b5c
movq %r15, %rdi
callq 0x2450
movq %rax, %r12
movq %rax, 0x77d2(%rip)      # 0xc308
testq %rax, %rax
jne 0x4b96
movq 0x7496(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x22d0
movq (%r14), %rcx
leaq 0x3a94(%rip), %rdi      # 0x85eb
jmp 0x4c6e
movq %r15, %rdi
callq 0x20c0
subq %r13, %rax
leaq 0x2(%rax), %rdi
callq 0x2310
testq %rax, %rax
je 0x4c52
movq %rax, %r12
movw $0x7e, (%rax)
addq %r15, %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x2430
movq %r12, 0x7772(%rip)      # 0xc308
cmpb $0x1, 0x76fb(%rip)      # 0xc298
je 0x4c0d
leaq 0x3a60(%rip), %rsi      # 0x8606
movq %r15, %rdi
callq 0x2280
movq %rax, 0x775b(%rip)      # 0xc310
testq %rax, %rax
jne 0x4bd8
testb $0x1, 0x76d7(%rip)      # 0xc298
je 0x4bd8
movq 0x7736(%rip), %rsi      # 0xc300
leaq 0x3a38(%rip), %rdi      # 0x8609
xorl %eax, %eax
callq 0x3520
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x4bef
movq (%rsp,%r15,8), %rdi
callq 0x2290
incq %r15
jmp 0x4bdb
movq %rbx, %rdi
callq 0x2290
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2290
movq 0x73c4(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq 0x39d4(%rip), %rsi      # 0x85f2
movq %r12, %rdx
xorl %eax, %eax
callq 0x2340
movq 0x76d1(%rip), %r15      # 0xc300
jmp 0x4b9f
movq 0x739d(%rip), %rax      # 0xbfd8
movq (%rax), %rsi
leaq 0x394a(%rip), %rdi      # 0x858f
callq 0x2250
xorl %r14d, %r14d
jmp 0x4aab
movq 0x737f(%rip), %r14      # 0xbfd8
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x22d0
movq (%r14), %rcx
leaq 0x3976(%rip), %rdi      # 0x85e4
pushq $0x6
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2480
movq (%r14), %rcx
leaq 0x3ab1(%rip), %rdi      # 0x8737
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x2480
xorl %edi, %edi
callq 0x2180
pushq $0x1
popq %rdi
callq 0x22a0