// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %r14
leaq 0x171ca(%rip), %rsi     # 0x78b05
movl $0x4, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x4ded0
leaq -0x530(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x85c0
leaq 0x13b92(%rip), %rsi     # 0x754f5
movq %r12, %rdi
callq 0x89c0
movl $0x0, %r13d
testq %rax, %rax
je 0x619a3
xorl %ebx, %ebx
leaq 0x13b76(%rip), %r14     # 0x754f5
leaq 0x1(%rbx), %r13
movq %rax, -0x130(%rbp,%rbx,8)
xorl %edi, %edi
movq %r14, %rsi
callq 0x89c0
testq %rax, %rax
je 0x619a3
cmpq $0x1f, %rbx
movq %r13, %rbx
jb 0x6197f
testl %r13d, %r13d
je 0x61a22
movq -0x130(%rbp), %r14
leaq 0x17161(%rip), %rsi     # 0x78b17
movl $0x4, %edx
movq %r14, %rdi
callq 0x82f0
testl %eax, %eax
movl $0x0, %r12d
jne 0x61a25
addq $0x4, %r14
leaq 0x17095(%rip), %rsi     # 0x78a6d
movq %r14, %rdi
callq 0x8890
xorl %r12d, %r12d
testl %eax, %eax
movq %r15, -0x30(%rbp)
je 0x61a3a
xorl %ebx, %ebx
leaq 0x2bf7c(%rip), %r15     # 0x8d970
cmpq $0x21, %rbx
je 0x61a22
movq 0x8(%r15,%rbx,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x8890
incq %rbx
testl %eax, %eax
jne 0x619f4
cmpb $0x0, (%r12)
sete %al
movq -0x30(%rbp), %r15
xorl %r12d, %r12d
jmp 0x61a3e
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
xorl %eax, %eax
testb %al, %al
jne 0x61a25
movw $0x62b5, (%r15)         # imm = 0x62B5
movb $0x6, 0x2(%r15)
leaq 0x15dbc(%rip), %rax     # 0x77810
movb (%rbx,%rax), %al
movb %al, 0x3(%r15)
leaq 0x6(%r15), %r14
shlq $0x7, %rbx
leaq 0x15dd6(%rip), %r12     # 0x77840
movl (%rbx,%r12), %eax
cmpl $0x1, %r13d
setne %cl
movl %r13d, %edx
xorl $0x1, %edx
orl %eax, %edx
je 0x61b95
movl %r13d, %r13d
addq %rbx, %r12
movl $0x2, %edx
leaq 0x15d4f(%rip), %rbx     # 0x777e4
movq %rdx, %r15
decl %eax
cmpl $0x8, %eax
ja 0x61aa8
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
testb $0x1, %cl
je 0x61ae9
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89b0
jmp 0x61aeb
xorl %eax, %eax
testb $0x1, %cl
je 0x61ad0
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89b0
movl %eax, (%r14)
jmp 0x61b41
testb $0x1, %cl
je 0x61b16
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89b0
jmp 0x61b18
xorl %eax, %eax
movb %al, (%r14)
movl $0x1, %eax
jmp 0x61b72
xorps %xmm0, %xmm0
testb $0x1, %cl
je 0x61b0a
movq -0x138(%rbp,%r15,8), %rdi
callq 0x87e0
movsd %xmm0, (%r14)
movl $0x8, %eax
jmp 0x61b72
xorl %eax, %eax
movw %ax, (%r14)
movl $0x2, %eax
jmp 0x61b72
xorps %xmm0, %xmm0
testb $0x1, %cl
je 0x61b3c
movq -0x138(%rbp,%r15,8), %rdi
callq 0x87e0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movl $0x4, %eax
jmp 0x61b72
leaq 0x8aa6(%rip), %rdx      # 0x6a5f5
testb $0x1, %cl
je 0x61b5c
movq -0x138(%rbp,%r15,8), %rdx
movq %r14, %rdi
leaq 0x16fb6(%rip), %rsi     # 0x78b1c
xorl %eax, %eax
callq 0x8120
movl $0x20, %eax
addq %rax, %r14
movl -0x4(%r12,%r15,4), %eax
cmpq %r13, %r15
setb %cl
leaq 0x1(%r15), %rdx
testl %eax, %eax
jne 0x61a95
cmpq %r13, %r15
jb 0x61a95
movq -0x30(%rbp), %rbx
subq %rbx, %r14
movslq %r14d, %rax
leaq 0x2(%rax), %r12
leal -0x6(%rax), %ecx
movw %cx, 0x4(%rbx)
cmpl $0x3, %eax
jl 0x61bd0
movl %r14d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x2, %esi
xorl %eax, %eax
xorl %ecx, %ecx
addb (%rbx,%rsi), %cl
addb %cl, %al
incq %rsi
cmpq %rsi, %rdx
jne 0x61bc1
jmp 0x61bd4
xorl %ecx, %ecx
xorl %eax, %eax
movslq %r14d, %rdx
movb %cl, (%rbx,%rdx)
movb %al, -0x1(%rbx,%r12)
leaq 0x16f3f(%rip), %rsi     # 0x78b25
movl $0x5, %edi
xorl %eax, %eax
callq 0x4ded0
movl $0x5, %edi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x53eee
jmp 0x61a25