// File: /workspace/github_c_makefile_star2_O0/rongarret[P]clmm/decrypt.c
// Function: main
int main(int argc, char** argv) {

  struct passwd *pw = getpwuid(getuid());

  c_string filename, to_id;

  if (argc == 2) {
    filename = argv[1];
    to_id = pw->pw_name;
  } else if (argc == 3) {
    filename = argv[1];
    to_id = argv[2];
  } else {
    printf("Usage: %s filename [recipient_key_id]\n", argv[0]);
    exit(1);
  }

  // Get the keys

  c_string homedir = pw->pw_dir;
  c_string clmm_path, to_sk_path, to_pk_path;
  asprintf(&clmm_path, "%s/.clmm", homedir);
  
  asprintf(&to_pk_path, "%s/%s.public_encryption_key", clmm_path, to_id);
  c_string to_pk_b64 = (c_string)file_contents(to_pk_path, 0)->data;
  u8* to_pk = b64decode(to_pk_b64, 0)->data;
  
  asprintf(&to_sk_path, "%s/%s.secret_encryption_key", clmm_path, to_id);
  u8* to_sk = file_contents(to_sk_path, 0)->data;

  // Decrypt the secret key

  char *passwd = getpass("Enter a pass phrase: ");
  u8 passwd_hash[crypto_hash_BYTES];
  crypto_hash(passwd_hash, (u8*)passwd, strlen(passwd));
  for (int i=0; i<sek_size; i++) to_sk[i] ^= passwd_hash[i];
  
  u8 to_pk1[pek_size];
  crypto_scalarmult_base(to_pk1, to_sk);
  if (crypto_verify_32(to_pk, to_pk1)) die("Incorrect pass phrase");

  // Get the from_key, nonce and ciphertext from the encrypted message

  c_string s = (c_string)file_contents(filename, 0)->data;
  c_string nonce_b64 = strtok(s, "\n");
  c_string from_pk_b64 = strtok(0, "\n");
  c_string ciphertext_b64 = strtok(0, "\0");

  u8* nonce = b64decode(nonce_b64, 0)->data;
  u8* from_pk = b64decode(from_pk_b64, 0)->data;

  // Remove newlines from ciphertext
  c_string s1,s2;
  s1 = s2 = ciphertext_b64;
  do {
    if (*s1 != '\n') *s2++ = *s1;
  } while (*s1++);
  *s2++ = '\0';

  size_t offset = crypto_box_BOXZEROBYTES;
  p_vector ciphertext = b64decode(ciphertext_b64, offset);
  u8* c = ciphertext->padding;
  size_t clen = ciphertext->size + offset;

  // Allocate cleartext buffer
  p_vector msg = mkvector(clen, crypto_box_ZEROBYTES);

  // Pre-decryption

  unsigned char k1[crypto_box_BEFORENMBYTES];
  if (crypto_box_beforenm(k1, from_pk, to_sk) != 0)
    die("Crypto setup failed");

  // Decrypt

  if (crypto_box_open_afternm(msg->padding, c, clen, nonce, k1) != 0)
    die("Decrpytion failed");

  fwrite(msg->data, clen-crypto_box_ZEROBYTES, 1, stdout);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1080
movl %eax, %edi
callq 0x1060
movq %rax, -0x18(%rbp)
cmpl $0x2, -0x8(%rbp)
jne 0x12c8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1309
cmpl $0x3, -0x8(%rbp)
jne 0x12e8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x1307
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x4d0e(%rip), %rdi      # 0x6004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1160
jmp 0x1309
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rdi
leaq 0x4d07(%rip), %rsi      # 0x602b
movb $0x0, %al
callq 0x10f0
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq 0x4cf6(%rip), %rsi      # 0x6034
movb $0x0, %al
callq 0x10f0
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1820
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x19a0
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x40(%rbp), %rdi
leaq 0x4cce(%rip), %rsi      # 0x6050
movb $0x0, %al
callq 0x10f0
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1820
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
leaq 0x4cc7(%rip), %rdi      # 0x606c
callq 0x1100
movq %rax, -0x68(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x68(%rbp), %rdi
callq 0x1070
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq %rax, %rdx
callq 0x4460
movl $0x0, -0xb4(%rbp)
cmpl $0x20, -0xb4(%rbp)
jge 0x142d
movslq -0xb4(%rbp), %rax
movzbl -0xb0(%rbp,%rax), %esi
movq -0x60(%rbp), %rax
movslq -0xb4(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x13f0
leaq -0xe0(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x3bd0
movq -0x58(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2060
cmpl $0x0, %eax
je 0x145e
leaq 0x4c29(%rip), %rdi      # 0x6082
callq 0x1670
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1820
movq 0x10(%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
leaq 0x4ba5(%rip), %rsi      # 0x6029
callq 0x1140
movq %rax, -0xf0(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x4b8e(%rip), %rsi      # 0x6029
callq 0x1140
movq %rax, -0xf8(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x4c0c(%rip), %rsi      # 0x60be
callq 0x1140
movq %rax, -0x100(%rbp)
movq -0xf0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x19a0
movq 0x10(%rax), %rax
movq %rax, -0x108(%rbp)
movq -0xf8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x19a0
movq 0x10(%rax), %rax
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x120(%rbp)
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x1538
movq -0x118(%rbp), %rax
movb (%rax), %cl
movq -0x120(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x120(%rbp)
movb %cl, (%rax)
jmp 0x153a
movq -0x118(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x118(%rbp)
cmpb $0x0, (%rax)
jne 0x1509
movq -0x120(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x120(%rbp)
movb $0x0, (%rax)
movq $0x10, -0x128(%rbp)
movq -0x100(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x19a0
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rax
movq (%rax), %rax
addq -0x128(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
movl $0x20, %esi
callq 0x1750
movq %rax, -0x148(%rbp)
leaq -0x170(%rbp), %rdi
movq -0x110(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x3c40
cmpl $0x0, %eax
je 0x15fb
leaq 0x4aa2(%rip), %rdi      # 0x6098
callq 0x1670
movq -0x148(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x138(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq -0x108(%rbp), %rcx
leaq -0x170(%rbp), %r8
callq 0x3cd0
cmpl $0x0, %eax
je 0x1638
leaq 0x4a79(%rip), %rdi      # 0x60ac
callq 0x1670
movq -0x148(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x140(%rbp), %rsi
subq $0x20, %rsi
movq 0x798b(%rip), %rax      # 0x8fe0
movq (%rax), %rcx
movl $0x1, %edx
callq 0x1170
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nop