#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef char **MYSQL_ROW;
typedef int /*<<< orphan*/ MYSQL_RES;
typedef int /*<<< orphan*/ MYSQL;

/* Variables and functions */
int /*<<< orphan*/ mysql_close(int /*<<< orphan*/ *);
char **mysql_fetch_row(int /*<<< orphan*/ *);
int /*<<< orphan*/ mysql_free_result(int /*<<< orphan*/ *);
int /*<<< orphan*/ mysql_init(int /*<<< orphan*/ *);
int mysql_num_fields(int /*<<< orphan*/ *);
int mysql_num_rows(int /*<<< orphan*/ *);
int mysql_query(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *mysql_real_connect(int /*<<< orphan*/ *, char *, char *,
                                       char *, char *, int,
                                       int /*<<< orphan*/ *,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ *mysql_store_result(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  MYSQL db;
  MYSQL *pdb;
  mysql_init(&db);
  pdb = mysql_real_connect(&db, "localhost", "root", "123", "class306", 3306,
                           NULL, 0);
  if (pdb == NULL) {
    perror("connect fail");
    return -1;
  } else {
    printf("connect ok");
    printf("\n");
  }
  printf("&db = %p,pdb = %p\n", &db, pdb);
  char *buff = "set names utf8;";
  int ret;
  ret = mysql_query(pdb, buff);
  char *sql = "select * from students;";
  ret = mysql_query(pdb, sql);
  if (ret == 0) {
    printf("connect ok");
    printf("\n");
  } else {
    perror("connect fail");
    return -1;
  }
  MYSQL_RES *res;
  res = mysql_store_result(pdb);
  int rows;
  int column;
  rows = mysql_num_rows(res);
  if (rows == 0) {
    return 0;
  }
  column = mysql_num_fields(res);
  int i;
  int j;
  MYSQL_ROW result;
  for (i = 0; i < rows; i++) {
    result = mysql_fetch_row(res);
    for (j = 0; j < column; j++) {
      printf("%s ", result[j]);
    }
    printf("\n");
  }
  mysql_free_result(res);
  mysql_close(&db);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1090 <mysql_init@plt>
lea    -0x8(%rbp),%rdi
lea    0xe2d(%rip),%rsi
lea    0xe30(%rip),%rdx
lea    0xe2e(%rip),%rcx
lea    0xe2b(%rip),%r8
mov    $0xcea,%r9d
xor    %eax,%eax
xor    %eax,%eax
movq   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1070 <mysql_real_connect@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122e <main+0x7e>
lea    0xdff(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <main+0x1f4>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdx
lea    0xddf(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xde0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <mysql_query@plt>
mov    %eax,-0x1c(%rbp)
lea    0xdd5(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <mysql_query@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12c1 <main+0x111>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12d9 <main+0x129>
lea    0xd54(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <main+0x1f4>
mov    -0x10(%rbp),%rdi
call   10d0 <mysql_store_result@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10c0 <mysql_num_rows@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1308 <main+0x158>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <main+0x1f4>
mov    -0x30(%rbp),%rdi
call   1050 <mysql_num_fields@plt>
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    138b <main+0x1db>
mov    -0x30(%rbp),%rdi
call   1040 <mysql_fetch_row@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    136f <main+0x1bf>
mov    -0x48(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    133b <main+0x18b>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    131b <main+0x16b>
mov    -0x30(%rbp),%rdi
call   10a0 <mysql_free_result@plt>
lea    -0x8(%rbp),%rdi
call   1080 <mysql_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <mysql_init@plt>
lea    0xe1e(%rip),%rsi
lea    0xe21(%rip),%rdx
lea    0xe1f(%rip),%rcx
lea    0xe1c(%rip),%r8
mov    %rbx,%rdi
mov    $0xcea,%r9d
push   $0x0
push   $0x0
call   1080 <mysql_real_connect@plt>
add    $0x10,%rsp
test   %rax,%rax
je     1351 <main+0x191>
mov    %rax,%rbx
lea    0xe09(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdf9(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdf6(%rip),%rsi
mov    %rbx,%rdi
call   1040 <mysql_query@plt>
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdi
call   1040 <mysql_query@plt>
test   %eax,%eax
jne    1351 <main+0x191>
lea    0xdb2(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   10e0 <mysql_store_result@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   10d0 <mysql_num_rows@plt>
test   %eax,%eax
je     133f <main+0x17f>
mov    %eax,%ebp
mov    %r12,%rdi
call   1060 <mysql_num_fields@plt>
mov    %eax,0xc(%rsp)
test   %ebp,%ebp
jle    132a <main+0x16a>
mov    0xc(%rsp),%r14d
lea    0xdae(%rip),%rbx
xor    %r15d,%r15d
mov    %ebp,0x14(%rsp)
jmp    12ea <main+0x12a>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r15d
mov    0x14(%rsp),%ebp
cmp    %ebp,%r15d
mov    %r13,%r12
je     132a <main+0x16a>
mov    %r12,%r13
mov    %r12,%rdi
call   1050 <mysql_fetch_row@plt>
cmpl   $0x0,0xc(%rsp)
jle    12d0 <main+0x110>
mov    %rax,%rbp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp,%r12,8),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%r12
cmp    %r12,%r14
jne    1310 <main+0x150>
jmp    12d0 <main+0x110>
mov    %r12,%rdi
call   10b0 <mysql_free_result@plt>
lea    0x10(%rsp),%rdi
call   1090 <mysql_close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc4(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    133f <main+0x17f>

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [4 x i8] c"123\00"
@.str.3 = [9 x i8] c"class306\00"
@.str.4 = [13 x i8] c"connect fail\00"
@.str.5 = [11 x i8] c"connect ok\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [19 x i8] c"&db = %p,pdb = %p\0A\00"
@.str.8 = [16 x i8] c"set names utf8;\00"
@.str.9 = [24 x i8] c"select * from students;\00"
@.str.10 = [4 x i8] c"%s \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8**
store i32 0, i32* %1
%13 = call i32 @mysql_init(i32* %2)
%14 = call i32* @mysql_real_connect(i32* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 3306, i32* null, i32 0)
store i32* %14, i32** %3
%15 = load i32*, i32** %3
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %22
22: ; preds = %19
%23 = load i32*, i32** %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32* %2, i32* %23)
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8** %4
%25 = load i32*, i32** %3
%26 = load i8*, i8** %4
%27 = call i32 @mysql_query(i32* %25, i8* %26)
store i32 %27, i32* %5
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0), i8** %6
%28 = load i32*, i32** %3
%29 = load i8*, i8** %6
%30 = call i32 @mysql_query(i32* %28, i8* %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %38
36: ; preds = %22
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %79
38: ; preds = %33
%39 = load i32*, i32** %3
%40 = call i32* @mysql_store_result(i32* %39)
store i32* %40, i32** %7
%41 = load i32*, i32** %7
%42 = call i32 @mysql_num_rows(i32* %41)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %38
store i32 0, i32* %1
br label %79
46: ; preds = %38
%47 = load i32*, i32** %7
%48 = call i32 @mysql_num_fields(i32* %47)
store i32 %48, i32* %9
store i32 0, i32* %10
br label %49
49: ; preds = %72, %46
%50 = load i32, i32* %10
%51 = load i32, i32* %8
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %75
53: ; preds = %49
%54 = load i32*, i32** %7
%55 = call i8** @mysql_fetch_row(i32* %54)
store i8** %55, i8*** %12
store i32 0, i32* %11
br label %56
56: ; preds = %67, %53
%57 = load i32, i32* %11
%58 = load i32, i32* %9
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %70
60: ; preds = %56
%61 = load i8**, i8*** %12
%62 = load i32, i32* %11
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = load i8*, i8** %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %60
%68 = load i32, i32* %11
%69 = add nsw i32 %68, 1
store i32 %69, i32* %11
br label %56
70: ; preds = %56
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %70
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 1
store i32 %74, i32* %10
br label %49
75: ; preds = %49
%76 = load i32*, i32** %7
%77 = call i32 @mysql_free_result(i32* %76)
%78 = call i32 @mysql_close(i32* %2)
store i32 0, i32* %1
br label %79
79: ; preds = %75, %45, %36, %17
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"root\00"
@.str.2 = [4 x i8] c"123\00"
@.str.3 = [9 x i8] c"class306\00"
@.str.4 = [13 x i8] c"connect fail\00"
@.str.5 = [11 x i8] c"connect ok\00"
@.str.7 = [19 x i8] c"&db = %p,pdb = %p\0A\00"
@.str.8 = [16 x i8] c"set names utf8;\00"
@.str.9 = [24 x i8] c"select * from students;\00"
@.str.10 = [4 x i8] c"%s \00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @mysql_init(i32* %1)
%4 = call i32* @mysql_real_connect(i32* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 3306, i32* null, i32 0)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %46
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%10 = call i32 @putchar(i32 10)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32* %1, i32* %4)
%12 = call i32 @mysql_query(i32* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%13 = call i32 @mysql_query(i32* %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.9, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %8
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%17 = call i32 @putchar(i32 10)
%18 = call i32* @mysql_store_result(i32* %4)
%19 = call i32 @mysql_num_rows(i32* %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %46, label %23
21: ; preds = %8
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %46
23: ; preds = %15
%24 = call i32 @mysql_num_fields(i32* %18)
%25 = icmp sgt i32 %19, 0
br i1 %25, label %26, label %43
26: ; preds = %23
%27 = icmp sgt i32 %24, 0
%28 = zext i32 %24 to i64
br label %29
29: ; preds = %26, %39
%30 = phi i32 [ 0, %26 ], [ %41, %39 ]
%31 = call i8** @mysql_fetch_row(i32* %18)
br i1 %27, label %32, label %39
32: ; preds = %29, %32
%33 = phi i64 [ %37, %32 ], [ 0, %29 ]
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %35)
%37 = add nuw nsw i64 %33, 1
%38 = icmp eq i64 %37, %28
br i1 %38, label %39, label %32
39: ; preds = %32, %29
%40 = call i32 @putchar(i32 10)
%41 = add nuw nsw i32 %30, 1
%42 = icmp eq i32 %41, %19
br i1 %42, label %43, label %29
43: ; preds = %39, %23
%44 = call i32 @mysql_free_result(i32* %18)
%45 = call i32 @mysql_close(i32* %1)
br label %46
46: ; preds = %21, %15, %43, %6
%47 = phi i32 [ -1, %6 ], [ -1, %21 ], [ 0, %15 ], [ 0, %43 ]
ret i32 %47
}