// File: /workspace/github_c_makefile_star2_O3/briandowns[P]pass/main.c
// Function: main
int main(int argc, char **argv) {
    if (argc < 2) {
        printf(USAGE, STR(app_name));
        return 1;
    }

    if (sodium_init() != 0) {
        return 1;
    }
    
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "version") == 0) {
            printf("version: %s - git: %s\n", STR(app_version), STR(git_sha));
            return 0;
        }

        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "help") == 0) {
            printf(USAGE, STR(bin_name));
            return 0;
        }
            
        char pass_dir_path[PATH_MAX];
        const char *pass_dir = getenv("PASS_DIR");
        if ((pass_dir != NULL) && (pass_dir[0] != 0)) {
            strcpy(pass_dir_path, pass_dir);
        } else {
            strcpy(pass_dir_path, getenv("HOME"));
            strcat(pass_dir_path, "/" CONFIG_DIR);
        }

        char key_file_path[PATH_MAX];
        const char *pass_key = getenv("PASS_KEY");
        if ((pass_key != NULL) && (pass_key[0] == 0)) {
            strcpy(key_file_path, pass_key);
        } else {
            strcpy(key_file_path, pass_dir_path);
            strcat(key_file_path, "/");
            strcat(key_file_path, KEY_NAME);
        }

        if (strcmp(argv[i], "init") == 0) {
            DIR* dir = opendir(pass_dir_path);
            if (ENOENT == errno) {
                mkdir(pass_dir_path, 0700);
            } else {
                closedir(dir);
            }
            
            if (access(key_file_path, F_OK ) != -1) {
                char answer;
                printf("overwrite existing key? [Y/n] ");
                if (scanf("%c", &answer) == EOF) {
                    fprintf(stderr, "error: failed to read user input\n");
                    return 1;
                }
                if (answer == 'Y') {
                    if (create_key(key_file_path) != 0) {
                        fprintf(stderr, "error: failed to generate key\n");
                        return 1;
                    }
                }
            } else {
                if (create_key(key_file_path) != 0) {
                    fprintf(stderr, "error: failed to create key\n");
                    return 1;
                }
            }

            return 0;
        }

        unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];

        FILE *key_fd = fopen(key_file_path, "r");
        fread(&key, crypto_secretstream_xchacha20poly1305_KEYBYTES, 1, key_fd);

        if ((strcmp(argv[i], "ls") == 0) || (strcmp(argv[i], "list") == 0)) {
            if (argc == 3) {
                PASSWORD_FILE_PATH;
                list(fp, 0);
            } else {
                list(pass_dir_path, 0);
            }

            break;
        }

        if (strcmp(argv[i], "set") == 0) {
            COMMAND_ARG_ERR_CHECK;

            char pass_buf[MAX_PASS_SIZE] = {0};
            strcpy(pass_buf, getpass("enter password: "));
            
            if (strlen(pass_buf) == 0) {
                fprintf(stderr, "error: empty passwords are not allowed\n");
                return 1;
            }

            PASSWORD_FILE_PATH;

            if (strchr(fp, '/') != NULL) {
                char *temp_fp = strdup(fp);
                mkdir_p(dirname(temp_fp));
                free(temp_fp);
            }

            if (encrypt_password(fp, pass_buf, key) != 0) {
                return 1;
            }

            break;
        }

        if (strcmp(argv[i], "get") == 0) {
            COMMAND_ARG_ERR_CHECK;
            PASSWORD_FILE_PATH;

            if (access(fp, F_OK ) != -1) {
                if (decrypt_password(fp, key) != 0) {
                    return 1;
                }
            } 

            break;
        }

        if ((strcmp(argv[i], "rm") == 0) || (strcmp(argv[i], "remove") == 0)) {
            COMMAND_ARG_ERR_CHECK;
            PASSWORD_FILE_PATH;

            remove(fp);

            break;
        }

        if (strcmp(argv[i], "config") == 0) {
            printf("path: %s\n", pass_dir_path);
            printf("key : %s\n", key_file_path);
            break;
        }

        if ((strcmp(argv[i], "gen") == 0) || (strcmp(argv[i], "generate") == 0)) {
            COMMAND_ARG_ERR_CHECK;

            int size = atoi(argv[2]);
            char *pass = generate_password(size);

            printf("%s\n", pass);
            free(pass);

            break;
        }

        if (strcmp(argv[i], "check") == 0) {
            COMMAND_ARG_ERR_CHECK;

            check(argv[2]);

            break;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5138, %rsp           # imm = 0x5138
cmpl $0x1, %edi
jg 0x401bae
leaq 0xe046f(%rip), %rdi     # 0x4e2004
leaq 0xe06b9(%rip), %rsi     # 0x4e2255
xorl %eax, %eax
callq 0x4325f0
movl $0x1, %r15d
jmp 0x402355
movq %rsi, %rbx
movl %edi, %ebp
callq 0x403f60
movl $0x1, %r15d
testl %eax, %eax
jne 0x402355
movl %ebp, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %r14d
leaq 0x3130(%rsp), %r13
movq (%rbx,%r14,8), %r15
cmpb $0x2d, (%r15)
jne 0x401c21
cmpb $0x76, 0x1(%r15)
jne 0x401bf7
cmpb $0x0, 0x2(%r15)
je 0x401e8e
movq %r15, %rdi
leaq 0xe05e8(%rip), %rsi     # 0x4e21e9
callq 0x4010b8
testl %eax, %eax
je 0x401e8e
cmpb $0x68, 0x1(%r15)
jne 0x401c38
cmpb $0x0, 0x2(%r15)
jne 0x401c38
jmp 0x401eb2
movq %r15, %rdi
leaq 0xe05be(%rip), %rsi     # 0x4e21e9
callq 0x4010b8
testl %eax, %eax
je 0x401e8e
movq %r15, %rdi
leaq 0xe05f5(%rip), %rsi     # 0x4e2237
callq 0x4010b8
testl %eax, %eax
je 0x401eb2
leaq 0xe05ef(%rip), %rdi     # 0x4e2245
callq 0x4309e0
testq %rax, %rax
je 0x401c72
cmpb $0x0, (%rax)
je 0x401c72
movq %r13, %rdi
movq %rax, %rsi
callq 0x401020
jmp 0x401ca7
leaq 0xe05d5(%rip), %rdi     # 0x4e224e
callq 0x4309e0
movq %r13, %rdi
movq %rax, %rsi
callq 0x401020
movq %r13, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x3130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x3133(%rsp,%rax) # imm = 0x737361
leaq 0xe05ac(%rip), %rdi     # 0x4e225a
callq 0x4309e0
testq %rax, %rax
je 0x401cbd
cmpb $0x0, (%rax)
je 0x401d08
leaq 0x2130(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x2130(%rsp,%rax)
movq %r12, %rdi
callq 0x4010e8
movabsq $0x656b2e737361702e, %rcx # imm = 0x656B2E737361702E
movq %rcx, 0x2130(%rsp,%rax)
movw $0x79, 0x2138(%rsp,%rax)
jmp 0x401d18
leaq 0x2130(%rsp), %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
leaq 0xf04f0(%rip), %rsi     # 0x4f2212
callq 0x4010b8
testl %eax, %eax
je 0x401ff2
movq %r13, %r12
leaq 0x2130(%rsp), %rdi
leaq 0x104726(%rip), %rsi    # 0x506467
callq 0x4414d0
movl $0x20, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %rax, %rcx
callq 0x4415d0
movq (%rbx,%r14,8), %r15
movb (%r15), %r13b
cmpb $0x6c, %r13b
jne 0x401d7c
cmpb $0x73, 0x1(%r15)
jne 0x401d7c
cmpb $0x0, 0x2(%r15)
je 0x401ecf
movq %r15, %rdi
leaq 0xebfd9(%rip), %rsi     # 0x4edd5f
callq 0x4010b8
testl %eax, %eax
je 0x401ecf
movq %r15, %rdi
leaq 0x104cb9(%rip), %rsi    # 0x506a56
callq 0x4010b8
testl %eax, %eax
je 0x402027
movq %r15, %rdi
leaq 0xe058f(%rip), %rsi     # 0x4e2343
callq 0x4010b8
testl %eax, %eax
je 0x40217e
cmpb $0x72, %r13b
jne 0x401dd9
cmpb $0x6d, 0x1(%r15)
jne 0x401dd9
cmpb $0x0, 0x2(%r15)
je 0x401f5e
movq %r15, %rdi
leaq 0xe0564(%rip), %rsi     # 0x4e2347
callq 0x4010b8
testl %eax, %eax
je 0x401f5e
movq %r15, %rdi
leaq 0xe0554(%rip), %rsi     # 0x4e234e
callq 0x4010b8
testl %eax, %eax
je 0x402246
movq %r15, %rdi
leaq 0xe0558(%rip), %rsi     # 0x4e2369
callq 0x4010b8
testl %eax, %eax
je 0x401e5d
movq %r15, %rdi
leaq 0xe0549(%rip), %rsi     # 0x4e236d
callq 0x4010b8
testl %eax, %eax
je 0x401e5d
movq %r15, %rdi
leaq 0xe053f(%rip), %rsi     # 0x4e2376
callq 0x4010b8
testl %eax, %eax
je 0x402277
incq %r14
xorl %r15d, %r15d
cmpq %r14, 0x8(%rsp)
movq %r12, %r13
jne 0x401bdb
jmp 0x402355
cmpl $0x3, %ebp
jne 0x402385
movq 0x10(%rbx), %rdi
callq 0x4301b0
movl %eax, %edi
callq 0x4026c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x441b50
movq %rbx, %rdi
callq 0x450f80
jmp 0x402352
leaq 0xe035c(%rip), %rdi     # 0x4e21f1
leaq 0xe036c(%rip), %rsi     # 0x4e2208
leaq 0xe036b(%rip), %rdx     # 0x4e220e
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x4325f0
jmp 0x402355
leaq 0xe014b(%rip), %rdi     # 0x4e2004
leaq 0xe037c(%rip), %rsi     # 0x4e223c
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x4325f0
jmp 0x402355
cmpl $0x3, %ebp
jne 0x40228e
leaq 0x1130(%rsp), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0358(%rip), %rdi     # 0x4e224e
callq 0x4309e0
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r12, %rdi
movq %r15, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r14,8), %rsi
movq %r12, %rdi
callq 0x401060
movq %r12, %rdi
jmp 0x402296
cmpl $0x3, %ebp
jne 0x402385
leaq 0x1130(%rsp), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe02c9(%rip), %rdi     # 0x4e224e
callq 0x4309e0
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r12, %rdi
movq %r15, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r14,8), %rsi
movq %r12, %rdi
callq 0x401060
movq %r12, %rdi
callq 0x4329a0
jmp 0x402352
leaq 0x3130(%rsp), %rdi
callq 0x484b60
movq %rax, %rbx
callq 0x42a500
cmpl $0x2, (%rax)
jne 0x4022eb
leaq 0x3130(%rsp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485860
jmp 0x4022f3
cmpl $0x3, %ebp
jne 0x402385
leaq 0x1130(%rsp), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe02bc(%rip), %rdi     # 0x4e230a
callq 0x486a00
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
cmpb $0x0, (%r15)
je 0x4023a5
leaq 0x130(%rsp), %r12
xorl %ebp, %ebp
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe01c6(%rip), %rdi     # 0x4e224e
callq 0x4309e0
leaq 0x4130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x4130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x4133(%rsp,%rax) # imm = 0x737361
movq %r12, %rdi
movq %r15, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x130(%rsp,%rax)
movq 0x8(%rbx,%r14,8), %rsi
movq %r12, %rdi
callq 0x401060
movq %r12, %rdi
movl $0x2f, %esi
callq 0x4010f0
testq %rax, %rax
je 0x4022b9
leaq 0x130(%rsp), %rdi
callq 0x453330
movq %rax, %rbx
movq %rax, %rdi
callq 0x488500
leaq 0x1054f4(%rip), %rdx    # 0x507611
leaq 0x30(%rsp), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4326c0
movq %r14, %rdi
callq 0x4010e8
movzbl -0x1(%rax,%r14), %ecx
cmpb $0x2f, %cl
cmovel %ebp, %ecx
movb %cl, -0x1(%rax,%r14)
leaq 0x31(%rsp), %r15
movzbl (%r15), %eax
cmpl $0x2f, %eax
je 0x402164
testl %eax, %eax
jne 0x402179
jmp 0x4022a2
movb $0x0, (%r15)
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485860
movb $0x2f, (%r15)
incq %r15
jmp 0x402152
cmpl $0x3, %ebp
jne 0x402385
leaq 0x1130(%rsp), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe00a9(%rip), %rdi     # 0x4e224e
callq 0x4309e0
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r12, %rdi
movq %r15, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r14,8), %rsi
movq %r12, %rdi
callq 0x401060
movq %r12, %rdi
xorl %esi, %esi
callq 0x485b30
cmpl $-0x1, %eax
je 0x402239
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x40255a
movl $0x1, %ecx
movl $0x1, %r15d
testl %eax, %eax
jne 0x4022e4
movl $0x2, %ecx
xorl %r15d, %r15d
jmp 0x4022e4
leaq 0xe0108(%rip), %rdi     # 0x4e2355
leaq 0x3130(%rsp), %rsi
xorl %eax, %eax
callq 0x4325f0
leaq 0xe00fc(%rip), %rdi     # 0x4e235f
leaq 0x2130(%rsp), %rsi
xorl %eax, %eax
callq 0x4325f0
jmp 0x402352
cmpl $0x3, %ebp
jne 0x402385
movq 0x10(%rbx), %rdi
callq 0x402729
jmp 0x402352
leaq 0x3130(%rsp), %rdi
xorl %esi, %esi
callq 0x4023ce
jmp 0x402352
leaq 0x30(%rsp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485860
movq %rbx, %rdi
callq 0x450f80
leaq 0x130(%rsp), %rdi
leaq 0x1130(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x4024a8
xorl %r15d, %r15d
testl %eax, %eax
setne %r15b
movl $0x2, %ecx
subl %r15d, %ecx
cmpl $0x2, %ecx
je 0x402352
jmp 0x402355
movq %rbx, %rdi
callq 0x484d90
leaq 0x2130(%rsp), %rdi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x485b30
cmpl $-0x1, %eax
je 0x40236a
leaq 0xdff5c(%rip), %rdi     # 0x4e226d
xorl %eax, %eax
callq 0x4325f0
leaq 0xdff6d(%rip), %rdi     # 0x4e228c
leaq 0x1130(%rsp), %rsi
xorl %eax, %eax
callq 0x4329e0
cmpl $-0x1, %eax
je 0x4023ba
cmpb $0x59, 0x1130(%rsp)
jne 0x402352
leaq 0x2130(%rsp), %rdi
callq 0x40266d
testl %eax, %eax
jne 0x4023c4
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x5138, %rsp           # imm = 0x5138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2130(%rsp), %rdi
callq 0x40266d
testl %eax, %eax
je 0x402355
callq 0x401166
jmp 0x401ba3
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rdi
leaq 0xdff57(%rip), %rsi     # 0x4e22ed
movq %r15, %rdx
xorl %eax, %eax
callq 0x432530
jmp 0x401ba3
callq 0x401100
movl $0x1, %ecx
movl $0x1, %r15d
jmp 0x4022e4
callq 0x401144
jmp 0x401ba3
callq 0x401122
jmp 0x401ba3