// File: /workspace/github_cpp_makefile_star2_O2/exrok[P]evdoublebind/./src/make_config.cc
// Function: generate_xkb_file(xkb_keymap*, Config&, Settings&)
void generate_xkb_file(xkb_keymap *keymap, Config &config, Settings &settings) {
    FILE * xkbfile;
    if (settings.xkb_conf_output == NULL) {
        char buf[1024];
        if (system("mkdir -p $HOME/.xkb/symbols/") != 0) {
            fprintf(stderr,"Failed creating $HOME/.xkb/symbols/");
            exit(1);
        }
        int len = snprintf(buf,1024, "%s/.xkb/rules/evdev-doublebind", getenv("HOME"));
        if (len >= 1023) {
            fprintf(stderr,"WOW, your home path is really long, CRITICAL ERROR");
            fprintf(stderr,"You should specify the xkb conf output manually");
            exit(1);
        }
        if (access(buf, F_OK) == -1) {
            fprintf(stderr, "Warning: ~/.xkb/rules/evdev-doublebind is missing. To use the XKB\n");
            fprintf(stderr, "        option 'evdoublebind:mapping' a custom rule set is needed.\n");
            fprintf(stderr, "        Running `evdoublebind-make-config  -g` will generate one.\n\n");
        }
        snprintf(buf,1024, "%s/.xkb/symbols/evdoublebind", getenv("HOME"));
        fprintf(stderr,"Outputing XKB option[evdoublebind:mapping] to:\n    %s\n", buf);
        xkbfile = fopen(buf, "w");
    } else {
        fprintf(stderr,"Outputing XKB option[evdoublebind:mapping] to:\n    %s\n",
                settings.xkb_conf_output);
        xkbfile = fopen(settings.xkb_conf_output, "w");
    }
    if (xkbfile == NULL) {
        perror("Failed Creating xkb config file: ");
        exit(1);
    }

    auto unused = config.unused_keys.begin();

     fprintf(xkbfile,"partial modifier_keys\n");
     fprintf(xkbfile,"xkb_symbols \"mapping\" {\n");
     for (auto & mapping: config.mappings) {
         const char * keyname = xkb_keymap_key_get_name(keymap, mapping.keycode);
         if (mapping.hold_map[0] != NULL) {
             fprintf(xkbfile,"   replace key <%s> { [ ",  keyname);
             print_keysyms(xkbfile,mapping.hold_map);
             fprintf(xkbfile," ] };\n");
         }
         if (mapping.tap_map[0] != NULL) {
             if (unused == config.unused_keys.end()) {
                 fprintf(stderr,"ERROR: not enough unused keys specified for config\n");
                 exit(1);
             }
             const char * tapkeyname = xkb_keymap_key_get_name(keymap, *unused);
             fprintf(xkbfile,"   replace key <%s> { [ ",  tapkeyname);
             print_keysyms(xkbfile,mapping.tap_map);
             fprintf(xkbfile," ] };\n");
             unused++;
         }
         generate_modifier_mapping(xkbfile, keyname, mapping.hold_map);
         fprintf(xkbfile,"\n");
     }
     fprintf(xkbfile,"};\n");
     fclose(xkbfile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdx, %rbx
movq %rsi, (%rsp)
movq %rdi, 0x8(%rsp)
movq 0x38(%rdx), %rdx
testq %rdx, %rdx
jne 0x3a05
leaq 0x2531(%rip), %rdi      # 0x5e48
callq 0x3080
testl %eax, %eax
jne 0x3ba5
leaq 0x1b49(%rip), %rdi      # 0x5474
callq 0x3030
leaq 0x2552(%rip), %rdx      # 0x5e89
leaq 0x10(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rcx
xorl %eax, %eax
callq 0x30a0
cmpl $0x3ff, %eax            # imm = 0x3FF
jge 0x3bbe
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x31c0
cmpl $-0x1, %eax
jne 0x39b4
movq 0x568a(%rip), %r14      # 0x8ff8
movq (%r14), %rcx
leaq 0x2593(%rip), %rdi      # 0x5f0b
pushq $0x42
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x3230
movq (%r14), %rcx
leaq 0x25be(%rip), %rdi      # 0x5f4e
pushq $0x43
popq %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3230
movq (%r14), %rcx
leaq 0x25e9(%rip), %rdi      # 0x5f92
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3230
leaq 0x1ab9(%rip), %rdi      # 0x5474
callq 0x3030
leaq 0x260f(%rip), %rdx      # 0x5fd6
leaq 0x10(%rsp), %rbx
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x30a0
movq 0x5613(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2604(%rip), %rsi      # 0x5ff3
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3140
leaq 0x262a(%rip), %rsi      # 0x602a
movq %rbx, %rdi
jmp 0x3a28
movq 0x55ec(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x25dd(%rip), %rsi      # 0x5ff3
xorl %eax, %eax
callq 0x3140
movq 0x38(%rbx), %rdi
leaq 0x2602(%rip), %rsi      # 0x602a
callq 0x31d0
movq %rax, %r15
testq %rax, %rax
je 0x3bfa
movq (%rsp), %r14
movq 0x20(%r14), %rbx
leaq 0x2606(%rip), %rdi      # 0x604e
pushq $0x16
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0x3230
leaq 0x2604(%rip), %rdi      # 0x6065
pushq $0x18
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3230
movq (%r14), %r13
movslq 0xc(%r14), %rax
addq $0x28, %r13
imulq $0x50, %rax, %r14
testq %r14, %r14
je 0x3b61
movl -0x28(%r13), %esi
movq 0x8(%rsp), %rdi
callq 0x31a0
movq %rax, %rbp
leaq -0x20(%r13), %r12
cmpq $0x0, -0x20(%r13)
je 0x3ad7
movq %r15, %rdi
leaq 0x25d1(%rip), %rsi      # 0x607e
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3140
movq %r15, %rdi
movq %r12, %rsi
callq 0x376c
leaq 0x25ce(%rip), %rdi      # 0x6097
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x3230
cmpq $0x0, (%r13)
je 0x3b3b
movq (%rsp), %rcx
movslq 0x2c(%rcx), %rax
shlq $0x2, %rax
addq 0x20(%rcx), %rax
cmpq %rax, %rbx
je 0x3b90
movl (%rbx), %esi
movq 0x8(%rsp), %rdi
callq 0x31a0
movq %r15, %rdi
leaq 0x2571(%rip), %rsi      # 0x607e
movq %rax, %rdx
xorl %eax, %eax
callq 0x3140
movq %r15, %rdi
movq %r13, %rsi
callq 0x376c
leaq 0x256e(%rip), %rdi      # 0x6097
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x3230
addq $0x4, %rbx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x36f0
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0x3100
addq $0x50, %r13
addq $-0x50, %r14
jmp 0x3a7e
leaq 0x211f(%rip), %rdi      # 0x5c87
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x3230
movq %r15, %rdi
callq 0x3070
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5461(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x24fd(%rip), %rdi      # 0x609e
pushq $0x33
jmp 0x3bb8
movq 0x544c(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x22af(%rip), %rdi      # 0x5e65
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
jmp 0x3bed
movq 0x5433(%rip), %r14      # 0x8ff8
movq (%r14), %rcx
leaq 0x22d9(%rip), %rdi      # 0x5ea8
pushq $0x32
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x3230
movq (%r14), %rcx
leaq 0x22f4(%rip), %rdi      # 0x5edb
pushq $0x2f
popq %rsi
movq %rbx, %rdx
callq 0x3230
pushq $0x1
popq %rdi
callq 0x3220
leaq 0x242b(%rip), %rdi      # 0x602c
callq 0x31e0
jmp 0x3bf2