// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rdi, %r14
leaq 0x17dae(%rip), %rsi     # 0x78add
xorl %r15d, %r15d
movl $0x4, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x4d55d
leaq -0x530(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x85c0
leaq 0x14775(%rip), %rsi     # 0x754cf
movq %r12, %rdi
callq 0x89b0
testq %rax, %rax
je 0x60dfb
xorl %ebx, %ebx
leaq 0x1475b(%rip), %r14     # 0x754cf
leaq 0x1(%rbx), %r12
movq %rax, -0x130(%rbp,%rbx,8)
xorl %edi, %edi
movq %r14, %rsi
callq 0x89b0
testq %rax, %rax
je 0x60d98
cmpq $0x1f, %rbx
movq %r12, %rbx
jb 0x60d74
movq -0x130(%rbp), %r14
leaq 0x17d49(%rip), %rsi     # 0x78aef
movl $0x4, %edx
movq %r14, %rdi
callq 0x82f0
testl %eax, %eax
jne 0x60df8
movq %r13, -0x30(%rbp)
addq $0x4, %r14
leaq 0x17c7f(%rip), %rsi     # 0x78a45
leaq 0x16a47(%rip), %r13     # 0x77814
xorl %r15d, %r15d
leaq 0x16979(%rip), %rbx     # 0x77750
movq %r14, %rdi
callq 0x8880
testl %eax, %eax
je 0x60e10
movslq 0x4(%rbx,%r15,4), %rsi
incq %r15
addq %rbx, %rsi
subq $-0x80, %r13
cmpq $0x22, %r15
jne 0x60dd7
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rcx
movw $0x62b5, (%rcx)         # imm = 0x62B5
movb $0x6, 0x2(%rcx)
leaq 0x169bc(%rip), %rax     # 0x777e0
movb (%r15,%rax), %al
movb %al, 0x3(%rcx)
leaq 0x6(%rcx), %r14
movl -0x4(%r13), %eax
testl %eax, %eax
sete %dl
cmpq $0x1, %r12
setne %cl
sete %sil
testb %sil, %dl
jne 0x60f56
movl $0x2, %r15d
leaq 0x168d3(%rip), %rbx     # 0x7772c
decl %eax
cmpl $0x8, %eax
ja 0x60e69
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
testb $0x1, %cl
je 0x60eaa
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89a0
jmp 0x60eac
xorl %eax, %eax
testb $0x1, %cl
je 0x60e91
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89a0
movl %eax, (%r14)
jmp 0x60f02
testb $0x1, %cl
je 0x60ed7
movq -0x138(%rbp,%r15,8), %rdi
callq 0x89a0
jmp 0x60ed9
xorl %eax, %eax
movb %al, (%r14)
movl $0x1, %eax
jmp 0x60f33
xorps %xmm0, %xmm0
testb $0x1, %cl
je 0x60ecb
movq -0x138(%rbp,%r15,8), %rdi
callq 0x87d0
movsd %xmm0, (%r14)
movl $0x8, %eax
jmp 0x60f33
xorl %eax, %eax
movw %ax, (%r14)
movl $0x2, %eax
jmp 0x60f33
xorps %xmm0, %xmm0
testb $0x1, %cl
je 0x60efd
movq -0x138(%rbp,%r15,8), %rdi
callq 0x87d0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movl $0x4, %eax
jmp 0x60f33
leaq 0x96e5(%rip), %rdx      # 0x6a5f5
testb $0x1, %cl
je 0x60f1d
movq -0x138(%rbp,%r15,8), %rdx
movq %r14, %rdi
leaq 0x17bcd(%rip), %rsi     # 0x78af4
xorl %eax, %eax
callq 0x8120
movl $0x20, %eax
addq %rax, %r14
movl (%r13), %eax
testl %eax, %eax
setne %dl
cmpq %r12, %r15
setb %cl
orb %cl, %dl
addq $0x4, %r13
incq %r15
testb %dl, %dl
jne 0x60e59
movq -0x30(%rbp), %rbx
subq %rbx, %r14
movslq %r14d, %rax
leaq 0x2(%rax), %r15
leal -0x6(%rax), %ecx
movw %cx, 0x4(%rbx)
cmpl $0x3, %eax
jl 0x60f91
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 0x60f82
jmp 0x60f95
xorl %ecx, %ecx
xorl %eax, %eax
movslq %r14d, %rdx
movb %cl, (%rbx,%rdx)
movb %al, -0x1(%rbx,%r15)
leaq 0x17b56(%rip), %rsi     # 0x78afd
movl $0x5, %edi
xorl %eax, %eax
callq 0x4d55d
movl $0x5, %edi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x53456
jmp 0x60dfb