// File: /workspace/github_cpp_makefile_star2_O2/DoubleString[P]RtConverter/src/Rtklib/ublox.cpp
// Function: bamboo::gen_ubx(char const*, unsigned char*)
extern int gen_ubx(const char *msg, unsigned char *buff)
{
    const char *cmd[]={
        "PRT","USB","MSG","NMEA","RATE","CFG","TP","NAV2","DAT","INF",
        "RST","RXM","ANT","FXN","SBAS","LIC","TM","TM2","TMODE","EKF",
        "GNSS","ITFM","LOGFILTER","NAV5","NAVX5","ODO","PM2","PWR","RINV","SMGR",
        "TMODE2","TMODE3","TPS","TXSLOT",""
    };
    const unsigned char id[]={
        0x00,0x1B,0x01,0x17,0x08,0x09,0x07,0x1A,0x06,0x02,
        0x04,0x11,0x13,0x0E,0x16,0x80,0x10,0x19,0x1D,0x12,
        0x3E,0x39,0x47,0x24,0x23,0x1E,0x3B,0x57,0x34,0x62,
        0x36,0x71,0x31,0x53
    };
    const int prm[][32]={
        {FU1,FU1,FU2,FU4,FU4,FU2,FU2,FU2,FU2},    /* PRT */
        {FU2,FU2,FU2,FU2,FU2,FU2,FS32,FS32,FS32}, /* USB */
        {FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1},        /* MSG */
        {FU1,FU1,FU1,FU1},                        /* NMEA */
        {FU2,FU2,FU2},                            /* RATE */
        {FU4,FU4,FU4,FU1},                        /* CFG */
        {FU4,FU4,FI1,FU1,FU2,FI2,FI2,FI4},        /* TP */
        {FU1,FU1,FU2,FU1,FU1,FU1,FU1,FI4,FU1,FU1,FU1,FU1,FU1,FU1,FU2,FU2,FU2,FU2,
         FU2,FU1,FU1,FU2,FU4,FU4},                /* NAV2 */
        {FR8,FR8,FR4,FR4,FR4,FR4,FR4,FR4,FR4},    /* DAT */
        {FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1}, /* INF */
        {FU2,FU1,FU1},                            /* RST */
        {FU1,FU1},                                /* RXM */
        {FU2,FU2},                                /* ANT */
        {FU4,FU4,FU4,FU4,FU4,FU4,FU4,FU4},        /* FXN */
        {FU1,FU1,FU1,FU1,FU4},                    /* SBAS */
        {FU2,FU2,FU2,FU2,FU2,FU2},                /* LIC */
        {FU4,FU4,FU4},                            /* TM */
        {FU1,FU1,FU2,FU4,FU4},                    /* TM2 */
        {FU4,FI4,FI4,FI4,FU4,FU4,FU4},            /* TMODE */
        {FU1,FU1,FU1,FU1,FU4,FU2,FU2,FU1,FU1,FU2}, /* EKF */
        {FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU4},    /* GNSS */
        {FU4,FU4},                                /* ITFM */
        {FU1,FU1,FU2,FU2,FU2,FU4},                /* LOGFILTER */
        {FU2,FU1,FU1,FI4,FU4,FI1,FU1,FU2,FU2,FU2,FU2,FU1,FU1,FU1,FU1,FU1,FU1,FU2,
         FU1,FU1,FU1,FU1,FU1,FU1},                /* NAV5 */
        {FU2,FU2,FU4,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU2,FU1,FU1,FU1,FU1,
         FU1,FU1,FU1,FU1,FU1,FU1,FU2},            /* NAVX5 */
        {FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1,FU1},    /* ODO */
        {FU1,FU1,FU1,FU1,FU4,FU4,FU4,FU4,FU2,FU2}, /* PM2 */
        {FU1,FU1,FU1,FU1,FU4},                    /* PWR */
        {FU1,FU1},                                /* RINV */
        {FU1,FU1,FU2,FU2,FU1,FU1,FU2,FU2,FU2,FU2,FU4}, /* SMGR */
        {FU1,FU1,FU2,FI4,FI4,FI4,FU4,FU4,FU4},    /* TMODE2 */
        {FU1,FU1,FU2,FI4,FI4,FI4,FU4,FU4,FU4},    /* TMODE3 */
        {FU1,FU1,FU1,FU1,FI2,FI2,FU4,FU4,FU4,FU4,FI4,FU4}, /* TPS */
        {FU1,FU1,FU1,FU1,FU4,FU4,FU4,FU4,FU4}     /* TXSLOT */
    };
    unsigned char *q=buff;
    char mbuff[1024],*args[32],*p;
    int i,j,n,narg=0;
    
    trace(4,"gen_ubxf: msg=%s\n",msg);
    
    strcpy(mbuff,msg);
    for (p=strtok(mbuff," ");p&&narg<32;p=strtok(NULL," ")) {
        args[narg++]=p;
    }
    if (narg<1||strncmp(args[0],"CFG-",4)) return 0;
    
    for (i=0;*cmd[i];i++) {
        if (!strcmp(args[0]+4,cmd[i])) break;
    }
    if (!*cmd[i]) return 0;
    
    *q++=UBXSYNC1;
    *q++=UBXSYNC2;
    *q++=UBXCFG;
    *q++=id[i];
    q+=2;
    for (j=1;prm[i][j-1]||j<narg;j++) {
        switch (prm[i][j-1]) {
            case FU1 : setU1(q,j<narg?(unsigned char )atoi(args[j]):0); q+=1; break;
            case FU2 : setU2(q,j<narg?(unsigned short)atoi(args[j]):0); q+=2; break;
            case FU4 : setU4(q,j<narg?(unsigned int  )atoi(args[j]):0); q+=4; break;
            case FI1 : setI1(q,j<narg?(signed char   )atoi(args[j]):0); q+=1; break;
            case FI2 : setI2(q,j<narg?(short         )atoi(args[j]):0); q+=2; break;
            case FI4 : setI4(q,j<narg?(int           )atoi(args[j]):0); q+=4; break;
            case FR4 : setR4(q,j<narg?(float         )atof(args[j]):0); q+=4; break;
            case FR8 : setR8(q,j<narg?(double)atof(args[j]):0); q+=8; break;
            case FS32: sprintf((char *)q,"%-32.32s",j<narg?args[j]:""); q+=32; break;
            default  : setU1(q,j<narg?(unsigned char )atoi(args[j]):0); q+=1; break;
        }
    }
    n=(int)(q-buff)+2;
    setU2(buff+4,(unsigned short)(n-8));
    setcs(buff,n);
    
    trace(5,"gen_ubx: buff=\n"); traceb(5,buff,n);
    return n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1732b(%rip), %rsi     # 0x6eab1
xorl %r13d, %r13d
pushq $0x4
popq %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x455bc
leaq -0x530(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x85c0
leaq 0x13d36(%rip), %r14     # 0x6b4e5
movq %r15, %rdi
movq %r14, %rsi
callq 0x89a0
testq %rax, %rax
je 0x577d4
cmpq $0x1f, %r13
ja 0x577d4
movq %rax, -0x130(%rbp,%r13,8)
incq %r13
xorl %edi, %edi
jmp 0x577b2
xorl %r15d, %r15d
testq %r13, %r13
je 0x579bd
movq -0x130(%rbp), %r12
leaq 0x172d5(%rip), %rsi     # 0x6eac3
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x8300
testl %eax, %eax
jne 0x579bd
movq %rbx, -0x30(%rbp)
addq $0x4, %r12
leaq 0x15f70(%rip), %r14     # 0x6d780
leaq 0x15fb8(%rip), %rbx     # 0x6d7cf
xorl %r15d, %r15d
cmpq $0x88, %r15
je 0x57964
leaq 0x15f0e(%rip), %rax     # 0x6d73c
movslq (%r15,%rax), %rsi
addq %rax, %rsi
movq %r12, %rdi
callq 0x8890
subq $-0x80, %r14
addq $0x4, %r15
incq %rbx
testl %eax, %eax
jne 0x5781a
movq -0x30(%rbp), %rcx
movw $0x62b5, (%rcx)         # imm = 0x62B5
movb $0x6, 0x2(%rcx)
movb (%rbx), %al
movq %rcx, %rbx
movb %al, 0x3(%rcx)
leaq 0x6(%rcx), %r15
pushq $0x1
popq %r12
movl (%r14), %eax
testl %eax, %eax
setne %cl
cmpq %r13, %r12
setb %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x57969
decl %eax
cmpl $0x8, %eax
ja 0x57899
leaq 0x15e88(%rip), %rcx     # 0x6d718
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq %r13, %r12
jae 0x578da
movq -0x130(%rbp,%r12,8), %rdi
callq 0x8990
jmp 0x578dc
xorl %eax, %eax
cmpq %r13, %r12
jae 0x578c1
movq -0x130(%rbp,%r12,8), %rdi
callq 0x8990
movl %eax, (%r15)
jmp 0x57929
cmpq %r13, %r12
jae 0x57901
movq -0x130(%rbp,%r12,8), %rdi
callq 0x8990
jmp 0x57903
xorl %eax, %eax
movb %al, (%r15)
pushq $0x1
jmp 0x57954
xorps %xmm0, %xmm0
cmpq %r13, %r12
jae 0x578f8
movq -0x130(%rbp,%r12,8), %rdi
callq 0x87b0
movsd %xmm0, (%r15)
pushq $0x8
jmp 0x57954
xorl %eax, %eax
movw %ax, (%r15)
pushq $0x2
jmp 0x57954
xorps %xmm0, %xmm0
cmpq %r13, %r12
jae 0x57924
movq -0x130(%rbp,%r12,8), %rdi
callq 0x87b0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15)
pushq $0x4
jmp 0x57954
leaq 0x8cb1(%rip), %rdx      # 0x605e5
cmpq %r13, %r12
jae 0x57941
movq -0x130(%rbp,%r12,8), %rdx
movq %r15, %rdi
leaq 0x1717d(%rip), %rsi     # 0x6eac8
xorl %eax, %eax
callq 0x8110
pushq $0x20
popq %rax
addq %rax, %r15
incq %r12
addq $0x4, %r14
jmp 0x57869
xorl %r15d, %r15d
jmp 0x579bd
subq %rbx, %r15
leal -0x6(%r15), %eax
movw %ax, 0x4(%rbx)
movslq %r15d, %rax
pushq $0x2
popq %rdx
xorl %ecx, %ecx
xorl %esi, %esi
cmpq %rax, %rdx
jge 0x5798e
addb (%rbx,%rdx), %cl
addb %cl, %sil
incq %rdx
jmp 0x5797e
leaq 0x2(%rax), %r15
movb %cl, (%rbx,%rax)
movb %sil, 0x1(%rbx,%rax)
leaq 0x17130(%rip), %rsi     # 0x6ead1
pushq $0x5
popq %r14
movl %r14d, %edi
xorl %eax, %eax
callq 0x455bc
movl %r14d, %edi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x4ad1c
movl %r15d, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq