// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rsi, (%rsp)
movq %rdi, 0x8(%rsp)
movq 0x38(%rdx), %rdx
testq %rdx, %rdx
jne 0x3c29
leaq 0x22c5(%rip), %rdi      # 0x5e48
callq 0x3080
testl %eax, %eax
jne 0x3dd7
leaq 0x18dd(%rip), %rdi      # 0x5474
callq 0x3030
leaq 0x22e6(%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 0x3ddc
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x31d0
cmpl $-0x1, %eax
jne 0x3bd8
callq 0x3383
leaq 0x1895(%rip), %rdi      # 0x5474
callq 0x3030
leaq 0x23eb(%rip), %rdx      # 0x5fd6
leaq 0x10(%rsp), %r15
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x30a0
movq 0x53ef(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x23e0(%rip), %rsi      # 0x5ff3
movq %r15, %rdx
xorl %eax, %eax
callq 0x3150
leaq 0x2406(%rip), %rsi      # 0x602a
movq %r15, %rdi
jmp 0x3c4c
movq 0x53c8(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x23b9(%rip), %rsi      # 0x5ff3
xorl %eax, %eax
callq 0x3150
movq 0x38(%r15), %rdi
leaq 0x23de(%rip), %rsi      # 0x602a
callq 0x31e0
movq %rax, %r15
testq %rax, %rax
je 0x3de1
movq (%rsp), %r14
movq 0x20(%r14), %rbx
leaq 0x23e2(%rip), %rdi      # 0x604e
movl $0x16, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x3240
leaq 0x23e0(%rip), %rdi      # 0x6065
movl $0x18, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x3240
movslq 0xc(%r14), %rax
testq %rax, %rax
je 0x3d9f
movq (%rsp), %rcx
movq (%rcx), %r12
addq $0x28, %r12
shlq $0x4, %rax
leaq (%rax,%rax,4), %r14
movl -0x28(%r12), %esi
movq 0x8(%rsp), %rdi
callq 0x31b0
movq %rax, %r13
leaq -0x20(%r12), %rbp
cmpq $0x0, -0x20(%r12)
je 0x3d0e
movq %r15, %rdi
leaq 0x239e(%rip), %rsi      # 0x607e
movq %r13, %rdx
xorl %eax, %eax
callq 0x3150
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3a5b
movl $0x6, %esi
movl $0x1, %edx
leaq 0x2391(%rip), %rdi      # 0x6097
movq %r15, %rcx
callq 0x3240
cmpq $0x0, (%r12)
je 0x3d76
movq (%rsp), %rcx
movslq 0x2c(%rcx), %rax
shlq $0x2, %rax
addq 0x20(%rcx), %rax
cmpq %rax, %rbx
je 0x3dd2
movl (%rbx), %esi
movq 0x8(%rsp), %rdi
callq 0x31b0
movq %r15, %rdi
leaq 0x233a(%rip), %rsi      # 0x607e
movq %rax, %rdx
xorl %eax, %eax
callq 0x3150
movq %r15, %rdi
movq %r12, %rsi
callq 0x3a5b
movl $0x6, %esi
movl $0x1, %edx
leaq 0x232d(%rip), %rdi      # 0x6097
movq %r15, %rcx
callq 0x3240
addq $0x4, %rbx
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x39e2
movl $0xa, %edi
movq %r15, %rsi
callq 0x3110
addq $0x50, %r12
addq $-0x50, %r14
jne 0x3cb7
leaq 0x1ee1(%rip), %rdi      # 0x5c87
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x3240
movq %r15, %rdi
callq 0x3070
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x33da
callq 0x335e
callq 0x3414
callq 0x33ff