// File: /workspace/github_c_makefile_star2_O3/sonicse[P]dbf2mysql/mysql2dbf.c
// Function: main
int main(int argc, char **argv) {
	int             i;
	MYSQL		*SQLsock,mysql;
	extern int      optind;
	extern char     *optarg;
	char            *query = NULL;

	dbhead          *dbh;
	field			*rec;
	MYSQL_RES		*qres;
	MYSQL_ROW			qrow;
	MYSQL_FIELD			*qfield;
	u_long			numfields, numrows, t;

	while ((i = getopt(argc, argv, "lucvq:h:d:t:p:U:P:")) != EOF) {
		switch(i) {
			case 'q':
				query = strdup(optarg);
				break;
			case 'v':
				verbose++;
				break;
			case 'c':
				create = 1;
				break;
			case 'l':
				if (upper) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				lower = 1;
				break;
			case 'u':
				if (lower) {
					usage();
					printf("Can't use -u and -l at the same time!\n");
					exit(1);
				}
				upper = 1;
				break;
			case 'h':
				host = (char *)strdup(optarg);
				break;
			case 'd':
				dbase = (char *)strdup(optarg);
				break;
			case 't':
				table = (char *)strdup(optarg);
				break;
		        case 'P':
				pass = (char *)strdup(optarg);
				break;
		        case 'U':
			        user = (char *)strdup(optarg);
				break;
			case ':':
				usage();
				printf("Missing argument!\n");
				exit(1);
			case '?':
				usage();
				printf("Unknown argument: %s\n", argv[0]);
				exit(1);
			default:
				break;
		}
	}

	argc -= optind;
	argv = &argv[optind];

	if (argc != 1) {
		usage();
		exit(1);
	}

	if ((dbase == NULL) || ((table == NULL) && (query == NULL))) {
		usage();
		printf("Dbase and table *must* be specified!\n");
		exit(1);
	}

	if (query == NULL) {
		query = (char *)malloc(14+strlen(table));
		sprintf(query, "SELECT * FROM %s", table);
	}

	if (verbose > 1) {
		printf("Opening %s for writing (previous contents will be lost)\n",
																	argv[0]);
	}

	if ((dbh = dbf_open_new(argv[0], O_WRONLY | O_CREAT | O_TRUNC)) == (dbhead *)DBF_ERROR) {
		fprintf(stderr, "Couldn't open xbase-file for writing: %s\n", argv[0]);
		exit(1);
	}

	if (verbose > 1) {
		printf("Making connection with mySQL-server\n");
	}

        mysql_init(&mysql);
        
    if (!(SQLsock = mysql_real_connect(&mysql,host,user,pass, NULL, 0, NULL, 0))) {
        fprintf(stderr, "Couldn't get a connection with the ");
        fprintf(stderr, "designated host!\n");
        fprintf(stderr, "Detailed report: %s\n", mysql_error(&mysql));
        close(dbh->db_fd);
        free(dbh);
        exit(1);
    }

    if (verbose > 1) {
        printf("Selecting database\n");
    }

    if ((mysql_select_db(SQLsock, dbase)) == -1) {
        fprintf(stderr, "Couldn't select database %s.\n", dbase);
        fprintf(stderr, "Detailed report: %s\n", mysql_error(SQLsock));
        close(dbh->db_fd);
        free(dbh);
        mysql_close(SQLsock);
        exit(1);
    }

	if (verbose > 1) {
		printf("Sending query\n");
	}
	

	if (mysql_query(SQLsock, query) == -1) {
		fprintf(stderr, "Error sending query.\nDetailed report: %s\n",
				mysql_error(SQLsock));
		if (verbose > 1) {
			fprintf(stderr, "%s\n", query);
		}
		mysql_close(SQLsock);
		close(dbh->db_fd);
		free(dbh);
		exit(1);
	}

	qres            = mysql_store_result(SQLsock);
	numfields       = mysql_num_fields(qres);
	numrows			= mysql_num_rows(qres);

	while ((qfield = mysql_fetch_field(qres))) {
	  
	  strtoupper(qfield->name);
	  switch (qfield->type) {
	    /* assume INT's have a max. of 10 digits (4^32 has 10 digits) */
	  case FIELD_TYPE_CHAR:
	  case FIELD_TYPE_SHORT:
	  case FIELD_TYPE_LONG:
	  
	    dbf_add_field(dbh, qfield->name, 'N', 10, 0);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_TYPE, %ld\n", qfield->name,
		     qfield->length);
	    
	    }
	    break;
				/* do we have to make the same assumption here? */
	  case FIELD_TYPE_DOUBLE:
	  case FIELD_TYPE_FLOAT:
	  case FIELD_TYPE_DECIMAL:
	    dbf_add_field(dbh, qfield->name, 'N', qfield->length,
			  qfield->decimals);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_REAL, %ld\n", qfield->name,
		     qfield->length);
	    }
	    break;
	  case FIELD_TYPE_STRING:
	  case FIELD_TYPE_VAR_STRING:
	  case FIELD_TYPE_BLOB:
	  case FIELD_TYPE_TINY_BLOB:
	  case FIELD_TYPE_MEDIUM_BLOB:
	  case FIELD_TYPE_LONG_BLOB:
	    dbf_add_field(dbh, qfield->name, 'C', qfield->length, 0);
	    if (verbose > 1) {
	      printf("Adding field: %s, INT_CHAR, %ld\n", qfield->name,
		     qfield->length);
	    }
	    break;
	  case FIELD_TYPE_DATETIME:
	  case FIELD_TYPE_TIMESTAMP:
	    dbf_add_field(dbh, qfield->name, 'T', qfield->length, 0);
	    if (verbose > 1) {
	     printf("Adding field: %s, FIELD_TYPE_DATETIME, %ld\n", qfield->name,
	        qfield->length);
	    }
	    break;
	  case FIELD_TYPE_DATE:
	  case FIELD_TYPE_NEWDATE:
	    //dbf_add_field(dbh, qfield->name, 'D', qfield->length, 0);
	    dbf_add_field(dbh, qfield->name, 'D', 8, 0); // date is 8 in dbf, Mysql returns 10 ?!
	    if (verbose > 1) {
	      printf("Adding field: %s, FIELD_TYPE_DATE, %d\n", qfield->name,8);
	      //qfield->length);
	    }
	    break;
	  default:
	    break;
	  }
	}
	
	if (verbose > 1) {
	  printf("Writing out header\n");
	}
	
	dbf_write_head(dbh);
	if (verbose > 1) {
	  printf("Writing out field-descriptions\n");
	}
	dbf_put_fields(dbh);
	if (verbose > 1) {
	  printf("Writing out data\n");
	}
	if (numrows) {
	  while ((qrow = mysql_fetch_row(qres)) != NULL) {

	    if ((rec = dbf_build_record(dbh)) != (field *)DBF_ERROR) {
	       
	      for (t = 0; t < numfields; t++) {
		//printf("ROW: %s\n",qrow[t]);
		if (qrow[t] != 0) {
		  strcpy(rec[t].db_contents, qrow[t]);
		} else {
		  rec[t].db_contents[0] = '\0';
		}
		if (upper) {
		  strtoupper(rec[t].db_contents);
		}
		if (lower) {
		  strtolower(rec[t].db_contents);
		}
	      }
	 
	      dbf_put_record(dbh, rec, dbh->db_records + 1);
	    
	      dbf_free_record(dbh, rec);
	  
	    }
	  }
	}
	
	if (verbose > 1) {
		printf("Writing out header\n");
	}

	dbf_write_head(dbh);

	if (verbose > 1) {
		printf("Closing up\n");
	}
				
	mysql_free_result(qres);
	mysql_close(SQLsock);
	close(dbh->db_fd);
	free(dbh);
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %r14
movl %edi, %ebx
xorl %r15d, %r15d
leaq 0x2b28(%rip), %r12      # 0x50a3
movq 0x4a46(%rip), %r13      # 0x6fc8
leaq 0x2a7b(%rip), %rbp      # 0x5004
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2080
cmpl $0x54, %eax
jle 0x25b9
leal -0x63(%rax), %ecx
cmpl $0x13, %ecx
ja 0x25d9
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movl $0x1, 0x4be5(%rip)      # 0x719c
jmp 0x2589
cmpl $0x4f, %eax
jle 0x2680
cmpl $0x50, %eax
jne 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4be1(%rip)      # 0x71b8
jmp 0x2589
cmpl $0x55, %eax
jne 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4bd2(%rip)      # 0x71c0
jmp 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, %r15
jmp 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4b92(%rip)      # 0x71a0
jmp 0x2589
cmpl $0x0, 0x4b7a(%rip)      # 0x7194
jne 0x285e
movl $0x1, 0x4b6e(%rip)      # 0x7198
jmp 0x2589
incl 0x4b5b(%rip)            # 0x7190
jmp 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4b5e(%rip)      # 0x71a8
jmp 0x2589
movq (%r13), %rdi
callq 0x22a0
movq %rax, 0x4b51(%rip)      # 0x71b0
jmp 0x2589
cmpl $0x0, 0x4b2d(%rip)      # 0x7198
jne 0x285e
movl $0x1, 0x4b19(%rip)      # 0x7194
jmp 0x2589
cmpl $-0x1, %eax
je 0x26b2
cmpl $0x3a, %eax
je 0x286c
cmpl $0x3f, %eax
jne 0x2589
callq 0x252c
movq (%r14), %rsi
leaq 0x2a10(%rip), %rdi      # 0x50b6
xorl %eax, %eax
callq 0x2040
jmp 0x2854
movq 0x4927(%rip), %rax      # 0x6fe0
movslq (%rax), %r12
subl %r12d, %ebx
cmpl $0x1, %ebx
jne 0x288d
cmpq $0x0, 0x4ad8(%rip)      # 0x71a8
je 0x287f
movq 0x4ad3(%rip), %rbx      # 0x71b0
movq %rbx, %rax
orq %r15, %rax
je 0x287f
testq %r15, %r15
jne 0x2716
movq %rbx, %rdi
callq 0x20b0
leaq 0xe(%rax), %rdi
callq 0x2220
movq %rax, %r15
leaq 0x29c3(%rip), %rsi      # 0x50cc
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2070
cmpl $0x2, 0x4a73(%rip)      # 0x7190
jl 0x2731
movq (%r14,%r12,8), %rsi
leaq 0x29b3(%rip), %rdi      # 0x50dd
xorl %eax, %eax
callq 0x2040
movq (%r14,%r12,8), %rdi
movl $0x241, %esi            # imm = 0x241
callq 0x35ec
cmpq $-0x1, %rax
je 0x2894
movq %rax, %rbx
cmpl $0x2, 0x4a3d(%rip)      # 0x7190
jl 0x2761
leaq 0x2b86(%rip), %rdi      # 0x52e2
callq 0x2260
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x20c0
movq 0x4a2b(%rip), %rsi      # 0x71a0
movq 0x4a44(%rip), %rdx      # 0x71c0
movq 0x4a35(%rip), %rcx      # 0x71b8
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d0
testq %rax, %rax
je 0x28b2
movq %rax, %r13
cmpl $0x2, 0x49e5(%rip)      # 0x7190
jl 0x27b9
leaq 0x2b52(%rip), %rdi      # 0x5306
callq 0x2260
movq 0x49e8(%rip), %rsi      # 0x71a8
movq %r13, %rdi
callq 0x2180
cmpl $-0x1, %eax
je 0x2911
cmpl $0x2, 0x49b8(%rip)      # 0x7190
jl 0x27e6
leaq 0x2b38(%rip), %rdi      # 0x5319
callq 0x2260
movq %r13, %rdi
movq %r15, %rsi
callq 0x2120
cmpl $-0x1, %eax
jne 0x296b
movq 0x47d7(%rip), %r14      # 0x6fd8
movq (%r14), %r12
movq %r13, %rdi
callq 0x20a0
leaq 0x2981(%rip), %rsi      # 0x5194
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
cmpl $0x2, 0x4969(%rip)      # 0x7190
jl 0x283d
movq (%r14), %rdi
leaq 0x2909(%rip), %rsi      # 0x513c
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
movq %r13, %rdi
callq 0x2230
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
movl $0x1, %edi
callq 0x2200
callq 0x252c
leaq 0x2b42(%rip), %rdi      # 0x53ac
jmp 0x2878
callq 0x252c
leaq 0x2b22(%rip), %rdi      # 0x539a
callq 0x2260
jmp 0x2854
callq 0x252c
leaq 0x2aea(%rip), %rdi      # 0x5375
jmp 0x2878
callq 0x252c
jmp 0x2854
movq 0x473d(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
movq (%r14,%r12,8), %rdx
leaq 0x286d(%rip), %rsi      # 0x5116
xorl %eax, %eax
callq 0x2240
jmp 0x2854
movq 0x471f(%rip), %r14      # 0x6fd8
movq (%r14), %rcx
leaq 0x287d(%rip), %rdi      # 0x5140
movl $0x23, %esi
movl $0x1, %edx
callq 0x22c0
movq (%r14), %rcx
leaq 0x2888(%rip), %rdi      # 0x5164
movl $0x11, %esi
movl $0x1, %edx
callq 0x22c0
movq (%r14), %r14
leaq 0x20(%rsp), %rdi
callq 0x20a0
leaq 0x28aa(%rip), %rsi      # 0x51a9
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
jmp 0x2845
movq 0x46c0(%rip), %r14      # 0x6fd8
movq (%r14), %rdi
movq 0x4886(%rip), %rdx      # 0x71a8
leaq 0x284d(%rip), %rsi      # 0x5176
xorl %eax, %eax
callq 0x2240
movq (%r14), %r15
movq %r13, %rdi
callq 0x20a0
leaq 0x2867(%rip), %rsi      # 0x51a9
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2240
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
movq %r13, %rdi
callq 0x2230
jmp 0x2854
movq %r13, 0x18(%rsp)
movq %r13, %rdi
callq 0x20f0
movq %rax, %r15
movq %rax, %rdi
callq 0x2270
movl %eax, %r14d
movq %r15, %rdi
callq 0x2170
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x2b0b
movq %rax, %rbp
leaq 0x2852(%rip), %r13      # 0x5200
leaq 0x269f(%rip), %r12      # 0x5054
movq (%rbp), %rdi
callq 0x4318
movl 0x70(%rbp), %eax
cmpq $0xe, %rax
ja 0x2a0b
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movzbl 0x68(%rbp), %r8d
movq %rbx, %rdi
movl $0x4e, %edx
callq 0x352b
cmpl $0x2, 0x479f(%rip)      # 0x7190
jl 0x2af7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x27d9(%rip), %rdi      # 0x51df
jmp 0x2af0
addl $0xffffff07, %eax       # imm = 0xFFFFFF07
cmpl $0x6, %eax
jae 0x2af7
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
movl $0x43, %edx
xorl %r8d, %r8d
callq 0x352b
cmpl $0x2, 0x4758(%rip)      # 0x7190
jl 0x2af7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
movq %r13, %rdi
jmp 0x2af0
movq (%rbp), %rsi
movq %rbx, %rdi
movl $0x4e, %edx
movl $0xa, %ecx
xorl %r8d, %r8d
callq 0x352b
cmpl $0x2, 0x4722(%rip)      # 0x7190
jl 0x2af7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x273b(%rip), %rdi      # 0x51be
jmp 0x2af0
movq (%rbp), %rsi
movq %rbx, %rdi
movl $0x44, %edx
movl $0x8, %ecx
xorl %r8d, %r8d
callq 0x352b
cmpl $0x2, 0x46eb(%rip)      # 0x7190
jl 0x2af7
movq (%rbp), %rsi
leaq 0x279b(%rip), %rdi      # 0x524d
movl $0x8, %edx
xorl %eax, %eax
callq 0x2040
jmp 0x2af7
movq (%rbp), %rsi
movzbl 0x38(%rbp), %ecx
movq %rbx, %rdi
movl $0x54, %edx
xorl %r8d, %r8d
callq 0x352b
cmpl $0x2, 0x46b1(%rip)      # 0x7190
jl 0x2af7
movq (%rbp), %rsi
movq 0x38(%rbp), %rdx
leaq 0x2731(%rip), %rdi      # 0x5221
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x2110
movq %rax, %rbp
testq %rax, %rax
jne 0x29b5
cmpl $0x2, 0x467e(%rip)      # 0x7190
jl 0x2b20
leaq 0x283c(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x3390
cmpl $0x2, 0x4661(%rip)      # 0x7190
jl 0x2b3d
leaq 0x27ef(%rip), %rdi      # 0x5327
callq 0x2260
movq %rbx, %rdi
callq 0x3454
cmpl $0x2, 0x4644(%rip)      # 0x7190
jl 0x2b5a
leaq 0x27f1(%rip), %rdi      # 0x5346
callq 0x2260
cmpq $0x0, 0x10(%rsp)
je 0x2c19
movq %r15, %rdi
callq 0x21c0
testq %rax, %rax
je 0x2c19
movq %rax, %r12
movl %r14d, %ebp
movq %rbp, 0x10(%rsp)
shlq $0x3, %rbp
movq %rbx, %rdi
callq 0x3f10
cmpq $-0x1, %rax
je 0x2c05
movq %rax, %r13
cmpl $0x0, 0x10(%rsp)
je 0x2be8
xorl %r14d, %r14d
movq (%r12,%r14), %rsi
movq 0x18(%r13,%r14,4), %rdi
testq %rsi, %rsi
je 0x2bb6
callq 0x21a0
jmp 0x2bb9
movb $0x0, (%rdi)
cmpl $0x0, 0x45d4(%rip)      # 0x7194
je 0x2bcc
movq 0x18(%r13,%r14,4), %rdi
callq 0x4318
cmpl $0x0, 0x45c5(%rip)      # 0x7198
je 0x2bdf
movq 0x18(%r13,%r14,4), %rdi
callq 0x434d
addq $0x8, %r14
cmpq %r14, %rbp
jne 0x2ba1
movq 0x20(%rbx), %rdx
incq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x40a6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x405f
movq %r15, %rdi
callq 0x21c0
movq %rax, %r12
testq %rax, %rax
jne 0x2b86
cmpl $0x2, 0x4570(%rip)      # 0x7190
jl 0x2c2e
leaq 0x272e(%rip), %rdi      # 0x5357
callq 0x2260
movq %rbx, %rdi
callq 0x3390
cmpl $0x2, 0x4553(%rip)      # 0x7190
jl 0x2c4b
leaq 0x2724(%rip), %rdi      # 0x536a
callq 0x2260
movq %r15, %rdi
callq 0x21b0
movq 0x18(%rsp), %rdi
callq 0x2230
movl (%rbx), %edi
callq 0x22d0
movq %rbx, %rdi
callq 0x21e0
xorl %edi, %edi
callq 0x2200
nop