#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 int /*<<< orphan*/ *MYSQL_ROW;
typedef int /*<<< orphan*/ MYSQL_RES;
typedef int /*<<< orphan*/ MYSQL;

/* Variables and functions */
int atoi(int /*<<< orphan*/);
int mysql_errno(int /*<<< orphan*/ *);
char *mysql_error(int /*<<< orphan*/ *);
int /*<<< orphan*/ *mysql_fetch_row(int /*<<< orphan*/ *);
scalar_t__ mysql_query(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *mysql_store_result(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int lastId(MYSQL *connection, char *tablename) {
  MYSQL_RES *result;
  MYSQL_ROW row;
  char *chunk = "SELECT max(id) FROM benchmark.%s";
  char query[100];
  snprintf(query, 100, chunk, tablename);
  if (mysql_query(connection, query)) {
    printf("lastId::Error %u: %s\n", mysql_errno(connection),
           mysql_error(connection));
  }
  result = mysql_store_result(connection);
  row = mysql_fetch_row(result);
  return atoi(row[0]);
}


x86 assembly (clang, -O0):
<lastId>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe66(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x64,%esi
call   1060 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1030 <mysql_query@plt>
cmp    $0x0,%rax
je     11fe <lastId+0x7e>
mov    -0x8(%rbp),%rdi
call   1070 <mysql_errno@plt>
mov    %eax,-0x94(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <mysql_error@plt>
mov    -0x94(%rbp),%esi
mov    %rax,%rdx
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <mysql_store_result@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <mysql_fetch_row@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lastId>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%rcx
mov    %rdi,%rbx
lea    0xe6b(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x64,%esi
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <mysql_query@plt>
test   %rax,%rax
je     11d8 <lastId+0x58>
mov    %rbx,%rdi
call   1070 <mysql_errno@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <mysql_error@plt>
lea    0xe53(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   10a0 <mysql_store_result@plt>
mov    %rax,%rdi
call   1040 <mysql_fetch_row@plt>
mov    (%rax),%edi
call   1090 <atoi@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"SELECT max(id) FROM benchmark.%s\00"
@.str.1 = [22 x i8] c"lastId::Error %u: %s\0A\00"
define i32 @lastId(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [100 x i8]
store i32* %0, i32** %3
store i8* %1, i8** %4
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%10 = load i8*, i8** %7
%11 = load i8*, i8** %4
%12 = call i32 @snprintf(i8* %9, i32 100, i8* %10, i8* %11)
%13 = load i32*, i32** %3
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%15 = call i64 @mysql_query(i32* %13, i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32*, i32** %3
%19 = call i32 @mysql_errno(i32* %18)
%20 = load i32*, i32** %3
%21 = call i8* @mysql_error(i32* %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %21)
br label %23
23: ; preds = %17, %2
%24 = load i32*, i32** %3
%25 = call i32* @mysql_store_result(i32* %24)
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = call i32* @mysql_fetch_row(i32* %26)
store i32* %27, i32** %6
%28 = load i32*, i32** %6
%29 = getelementptr inbounds i32, i32* %28, i64 0
%30 = load i32, i32* %29
%31 = call i32 @atoi(i32 %30)
ret i32 %31
}

LLVM IR (-O2):
@.str = [33 x i8] c"SELECT max(id) FROM benchmark.%s\00"
@.str.1 = [22 x i8] c"lastId::Error %u: %s\0A\00"
define i32 @lastId(i32* %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = call i64 @mysql_query(i32* %0, i8* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = call i32 @mysql_errno(i32* %0)
%10 = call i8* @mysql_error(i32* %0)
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %10)
br label %12
12: ; preds = %8, %2
%13 = call i32* @mysql_store_result(i32* %0)
%14 = call i32* @mysql_fetch_row(i32* %13)
%15 = load i32, i32* %14
%16 = call i32 @atoi(i32 %15)
ret i32 %16
}