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

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

  c_string filename, to_id, from_id;

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

  c_string homedir = pw->pw_dir;
  c_string clmm_path, to_pk_path, from_sk_path, from_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(&from_pk_path, "%s/%s.public_encryption_key", clmm_path, from_id);
  c_string from_pk_b64 = (c_string)file_contents(from_pk_path, 0)->data;
  u8* from_pk = b64decode(from_pk_b64, 0)->data;

  asprintf(&from_sk_path, "%s/%s.secret_encryption_key", clmm_path, from_id);
  u8 from_sk[sek_size];
  FILE *f = fopen(from_sk_path, "r");
  fread(from_sk, sek_size, 1, f);
  fclose(f);

  // 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++) from_sk[i] ^= passwd_hash[i];

  u8 from_pk1[pek_size];
  crypto_scalarmult_base(from_pk1, from_sk);
  if (crypto_verify_32(from_pk, from_pk1)) die("Incorrect pass phrase");

  p_vector msg = file_contents(filename, crypto_box_ZEROBYTES);
  u8 *m = msg->padding;
  size_t mlen = msg->size;
  size_t clen = mlen + crypto_box_ZEROBYTES;

  // Pre-encryption

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

  p_vector nonce = mkvector(crypto_box_NONCEBYTES, 0);
  randombytes(nonce->data, crypto_box_NONCEBYTES);

  // Allocate ciphertext
  size_t offset = crypto_box_BOXZEROBYTES;
  p_vector ciphertext = mkvector(clen - offset, offset);
  u8 *c = ciphertext->padding;

  // Encrypt

  if (crypto_box_afternm(c, m, clen, nonce->data, k1) != 0)
    die("Encrpytion failed");

  // Output

  c_string s = b64encode(ciphertext);
  c_string s_end = s + strlen(s) - 76;

  printf("%s\n%s\n", b64encode(nonce), from_pk_b64);
  c_string s1 = s;
  for (; s1<s_end; s1+=76) {
    fwrite(s1, 76, 1, stdout);
    putchar('\n');
  }
  puts(s1);
  putchar('\n');
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1090
movl %eax, %edi
callq 0x1070
movq %rax, -0x18(%rbp)
cmpl $0x3, -0x8(%rbp)
jne 0x12d4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x1321
cmpl $0x4, -0x8(%rbp)
jne 0x1300
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x131f
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x4cf6(%rip), %rdi      # 0x6004
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x1160
jmp 0x1321
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdx
leaq -0x40(%rbp), %rdi
leaq 0x4cfd(%rip), %rsi      # 0x6039
movb $0x0, %al
callq 0x1100
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq 0x4cec(%rip), %rsi      # 0x6042
movb $0x0, %al
callq 0x1100
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x18a0
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a20
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x58(%rbp), %rdi
leaq 0x4ca8(%rip), %rsi      # 0x6042
movb $0x0, %al
callq 0x1100
movq -0x58(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x18a0
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a20
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x50(%rbp), %rdi
leaq 0x4c80(%rip), %rsi      # 0x605e
movb $0x0, %al
callq 0x1100
movq -0x50(%rbp), %rdi
leaq 0x4c8a(%rip), %rsi      # 0x607a
callq 0x1130
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rcx
movl $0x20, %esi
movl $0x1, %edx
callq 0x1050
movq -0xa8(%rbp), %rdi
callq 0x1060
leaq 0x4c50(%rip), %rdi      # 0x607c
callq 0x1110
movq %rax, -0xb0(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x1080
movq -0x1a8(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movq %rax, %rdx
callq 0x44e0
movl $0x0, -0xf4(%rbp)
cmpl $0x20, -0xf4(%rbp)
jge 0x14c1
movslq -0xf4(%rbp), %rax
movzbl -0xf0(%rbp,%rax), %edx
movslq -0xf4(%rbp), %rax
movzbl -0xa0(%rbp,%rax), %ecx
xorl %edx, %ecx
movb %cl, -0xa0(%rbp,%rax)
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x1480
leaq -0x120(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3c50
movq -0x78(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x20e0
cmpl $0x0, %eax
je 0x14f5
leaq 0x4ba2(%rip), %rdi      # 0x6092
callq 0x16f0
movq -0x20(%rbp), %rdi
movl $0x20, %esi
callq 0x18a0
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x130(%rbp)
movq -0x128(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x140(%rbp)
leaq -0x160(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x3cc0
cmpl $0x0, %eax
je 0x1567
leaq 0x4b46(%rip), %rdi      # 0x60a8
callq 0x16f0
movl $0x18, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x17d0
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x18, %esi
callq 0x1710
movq $0x10, -0x170(%rbp)
movq -0x140(%rbp), %rdi
subq -0x170(%rbp), %rdi
movq -0x170(%rbp), %rsi
callq 0x17d0
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq -0x168(%rbp), %rax
movq 0x10(%rax), %rcx
leaq -0x160(%rbp), %r8
callq 0x3d10
cmpl $0x0, %eax
je 0x160c
leaq 0x4ab5(%rip), %rdi      # 0x60bc
callq 0x16f0
movq -0x178(%rbp), %rdi
callq 0x19d0
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x188(%rbp), %rdi
callq 0x1080
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
addq %rcx, %rax
addq $-0x4c, %rax
movq %rax, -0x190(%rbp)
movq -0x168(%rbp), %rdi
callq 0x19d0
movq %rax, %rsi
movq -0x70(%rbp), %rdx
leaq 0x4a63(%rip), %rdi      # 0x60ce
movb $0x0, %al
callq 0x10a0
movq -0x188(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x198(%rbp), %rax
cmpq -0x190(%rbp), %rax
jae 0x16ce
movq -0x198(%rbp), %rdi
movq 0x7942(%rip), %rax      # 0x8fe0
movq (%rax), %rcx
movl $0x4c, %esi
movl $0x1, %edx
callq 0x1170
movl $0xa, %edi
callq 0x1030
movq -0x198(%rbp), %rax
addq $0x4c, %rax
movq %rax, -0x198(%rbp)
jmp 0x1680
movq -0x198(%rbp), %rdi
callq 0x1040
movl $0xa, %edi
callq 0x1030
movl -0x4(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq