#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 */

/* Variables and functions */
int atoi(char *);
int fetchResultRow(void *, int, char **);
int /*<<< orphan*/ free(char *);
void *issueQuery(char *, char *, char *, char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int ret;
  char *apikey = NULL;
  char *database = NULL;
  int num_of_select_col;
  char *query = NULL;
  char *endpoint = NULL;
  void *td_query_state = NULL;
  char **values;
  if (argc < 5) {
    printf("usage: %s apikey database num_of_select_col query [endpoint]\n",
           argv[0]);
    return 1;
  }
  apikey = argv[1];
  database = argv[2];
  num_of_select_col = atoi(argv[3]);
  query = argv[4];
  if (argc >= 6) {
    endpoint = argv[5];
  }
  td_query_state = issueQuery(apikey, endpoint, "presto", database, query);
  if (td_query_state == NULL) {
    printf("Failed to execute query\n");
    return 1;
  }
  while (1) {
    int i;
    values = (char **)malloc(sizeof(char *) * num_of_select_col);
    ret = fetchResultRow(td_query_state, num_of_select_col, values);
    printf("============================== ret: %d "
           "==================================\n",
           ret);
    if (ret != 0)
      break;
    for (i = 0; i < num_of_select_col; i++) {
      printf("i=%d, value=%s\n", i, values[i]);
      free(values[i]);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpl   $0x5,-0x8(%rbp)
jge    11c9 <main+0x69>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x6,-0x8(%rbp)
jl     1213 <main+0xb3>
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%r8
lea    0xe14(%rip),%rdx
call   1050 <issueQuery@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1258 <main+0xf8>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fe <main+0x19e>
jmp    125d <main+0xfd>
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x50(%rbp),%rdx
call   1080 <fetchResultRow@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     12a3 <main+0x143>
jmp    12f7 <main+0x197>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12f2 <main+0x192>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12aa <main+0x14a>
jmp    125d <main+0xfd>
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
mov    %rsi,%rbx
cmp    $0x4,%edi
jg     119c <main+0x2c>
mov    (%rbx),%rsi
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b2 <main+0x142>
mov    %edi,%ebp
mov    0x8(%rbx),%r15
mov    0x10(%rbx),%r14
mov    0x18(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r12d
mov    0x20(%rbx),%r8
cmp    $0x5,%ebp
jne    11bf <main+0x4f>
xor    %esi,%esi
jmp    11c3 <main+0x53>
mov    0x28(%rbx),%rsi
lea    0xe74(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rcx
call   1060 <issueQuery@plt>
test   %rax,%rax
je     12a6 <main+0x136>
mov    %rax,%rbx
lea    0x0(,%r12,8),%edi
mov    %edi,0xc(%rsp)
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   1090 <fetchResultRow@plt>
mov    %eax,%ebx
lea    0xe34(%rip),%rdi
xor    %r13d,%r13d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jne    12b8 <main+0x148>
mov    %r12d,%r14d
lea    0xe61(%rip),%r15
xor    %r13d,%r13d
jmp    1272 <main+0x102>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0xc(%rsp),%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    0x10(%rsp),%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   1090 <fetchResultRow@plt>
mov    %eax,%ebx
lea    0xde0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jne    12b8 <main+0x148>
test   %r12d,%r12d
jle    1240 <main+0xd0>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x0(%rbp,%rbx,8),%rdx
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1280 <main+0x110>
jmp    1240 <main+0xd0>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [62 x i8] c"usage: %s apikey database num_of_select_col query [endpoint]\0A\00"
@.str.1 = [7 x i8] c"presto\00"
@.str.2 = [25 x i8] c"Failed to execute query\0A\00"
@.str.3 = [75 x i8] c"============================== ret: %d ==================================\0A\00"
@.str.4 = [16 x i8] c"i=%d, value=%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8**
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %10
store i8* null, i8** %11
store i8* null, i8** %12
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 5
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %93
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
store i8* %28, i8** %8
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %9
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 4
%35 = load i8*, i8** %34
store i8* %35, i8** %10
%36 = load i32, i32* %4
%37 = icmp sge i32 %36, 6
br i1 %37, label %38, label %42
38: ; preds = %22
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 5
%41 = load i8*, i8** %40
store i8* %41, i8** %11
br label %42
42: ; preds = %38, %22
%43 = load i8*, i8** %7
%44 = load i8*, i8** %11
%45 = load i8*, i8** %8
%46 = load i8*, i8** %10
%47 = call i8* @issueQuery(i8* %43, i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %46)
store i8* %47, i8** %12
%48 = load i8*, i8** %12
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %93
52: ; preds = %42
br label %53
53: ; preds = %52, %91
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = mul i64 8, %55
%57 = trunc i64 %56 to i32
%58 = call i64 @malloc(i32 %57)
%59 = inttoptr i64 %58 to i8**
store i8** %59, i8*** %13
%60 = load i8*, i8** %12
%61 = load i32, i32* %9
%62 = load i8**, i8*** %13
%63 = call i32 @fetchResultRow(i8* %60, i32 %61, i8** %62)
store i32 %63, i32* %6
%64 = load i32, i32* %6
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.3, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %6
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %53
br label %92
69: ; preds = %53
store i32 0, i32* %14
br label %70
70: ; preds = %88, %69
%71 = load i32, i32* %14
%72 = load i32, i32* %9
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %91
74: ; preds = %70
%75 = load i32, i32* %14
%76 = load i8**, i8*** %13
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8*, i8** %76, i64 %78
%80 = load i8*, i8** %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %75, i8* %80)
%82 = load i8**, i8*** %13
%83 = load i32, i32* %14
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8*, i8** %82, i64 %84
%86 = load i8*, i8** %85
%87 = call i32 @free(i8* %86)
br label %88
88: ; preds = %74
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %70
91: ; preds = %70
br label %53
92: ; preds = %68
store i32 0, i32* %3
br label %93
93: ; preds = %92, %50, %17
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str = [62 x i8] c"usage: %s apikey database num_of_select_col query [endpoint]\0A\00"
@.str.1 = [7 x i8] c"presto\00"
@.str.3 = [75 x i8] c"============================== ret: %d ==================================\0A\00"
@.str.4 = [16 x i8] c"i=%d, value=%s\0A\00"
@str = [24 x i8] c"Failed to execute query\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 5
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %55
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 3
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 4
%16 = load i8*, i8** %15
%17 = icmp eq i32 %0, 5
br i1 %17, label %21, label %18
18: ; preds = %7
%19 = getelementptr inbounds i8*, i8** %1, i64 5
%20 = load i8*, i8** %19
br label %21
21: ; preds = %18, %7
%22 = phi i8* [ %20, %18 ], [ null, %7 ]
%23 = tail call i8* @issueQuery(i8* %9, i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %16)
%24 = icmp eq i8* %23, null
br i1 %24, label %35, label %25
25: ; preds = %21
%26 = shl i32 %14, 3
%27 = tail call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8**
%29 = tail call i32 @fetchResultRow(i8* %23, i32 %14, i8** %28)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %29, 0
br i1 %31, label %32, label %55
32: ; preds = %25
%33 = icmp sgt i32 %14, 0
%34 = zext i32 %14 to i64
br label %43
35: ; preds = %21
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %55
37: ; preds = %45, %43
%38 = tail call i64 @malloc(i32 %26)
%39 = inttoptr i64 %38 to i8**
%40 = tail call i32 @fetchResultRow(i8* %23, i32 %14, i8** %39)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = icmp eq i32 %40, 0
br i1 %42, label %43, label %55
43: ; preds = %32, %37
%44 = phi i8** [ %28, %32 ], [ %39, %37 ]
br i1 %33, label %45, label %37
45: ; preds = %43, %45
%46 = phi i64 [ %53, %45 ], [ 0, %43 ]
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = trunc i64 %46 to i32
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %49, i8* %48)
%51 = load i8*, i8** %47
%52 = tail call i32 @free(i8* %51)
%53 = add nuw nsw i64 %46, 1
%54 = icmp eq i64 %53, %34
br i1 %54, label %37, label %45
55: ; preds = %37, %25, %35, %4
%56 = phi i32 [ 1, %4 ], [ 1, %35 ], [ 0, %25 ], [ 0, %37 ]
ret i32 %56
}