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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sqlite3_close(int /*<<< orphan*/ *);
int sqlite3_exec(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                 int /*<<< orphan*/, int /*<<< orphan*/);
int sqlite3_open(char *, int /*<<< orphan*/ **);
int update_trip_data(char *database, int tripid) {
  int retval;
  sqlite3 *handle;
  retval = sqlite3_open(database, &handle);
  if (retval) {
    printf("Update trip data: Database connection failed: %d\n", retval);
    return -1;
  }
  char query[850];
  sprintf(query,
          "UPDATE Trip SET"
          " FirstData = (SELECT MIN(TimeStamp) FROM ("
          " SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID"
          " UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = "
          "Trip.Trip_ID"
          " UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = "
          "Trip.Trip_ID"
          " UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = "
          "Trip.Trip_ID"
          " )),"
          " LastData = (SELECT MAX(TimeStamp) FROM ("
          " SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID"
          " UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = "
          "Trip.Trip_ID"
          " UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = "
          "Trip.Trip_ID"
          " UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = "
          "Trip.Trip_ID"
          " )),"
          " CalculatedKilometers = (SELECT SUM(((Speed*1000)/60)/60)/1000 FROM "
          "GpsData"
          " WHERE GpsData.Trip_ID = Trip.Trip_ID)"
          " WHERE Trip_ID = %d",
          tripid);
  retval = sqlite3_exec(handle, query, 0, 0, 0);
  if (retval) {
    printf("Update trip data: Updating trips in DB Failed: %d\n", retval);
    printf("Query: %s\n", query);
    return -1;
  }
  sqlite3_close(handle);
  return 0;
}


x86 assembly (clang, -O0):
<update_trip_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x380,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <sqlite3_open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1199 <update_trip_data+0x49>
mov    -0x18(%rbp),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <update_trip_data+0xc7>
lea    -0x380(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe88(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x380(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1030 <sqlite3_exec@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1207 <update_trip_data+0xb7>
mov    -0x18(%rbp),%esi
lea    0x117e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x380(%rbp),%rsi
lea    0x119c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <update_trip_data+0xc7>
mov    -0x20(%rbp),%rdi
call   1060 <sqlite3_close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x380,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_trip_data>:
push   %r14
push   %rbx
sub    $0x368,%rsp
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <sqlite3_open@plt>
test   %eax,%eax
je     1181 <update_trip_data+0x31>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    11e5 <update_trip_data+0x95>
lea    0xeaa(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   1070 <sprintf@plt>
mov    0x8(%rsp),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <sqlite3_exec@plt>
test   %eax,%eax
je     11db <update_trip_data+0x8b>
lea    0x11a5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x11c8(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    11e5 <update_trip_data+0x95>
mov    0x8(%rsp),%rdi
call   1060 <sqlite3_close@plt>
mov    %ebx,%eax
add    $0x368,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Update trip data: Database connection failed: %d\0A\00"
@.str.1 = [811 x i8] c"UPDATE Trip SET FirstData = (SELECT MIN(TimeStamp) FROM ( SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = Trip.Trip_ID )), LastData = (SELECT MAX(TimeStamp) FROM ( SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = Trip.Trip_ID )), CalculatedKilometers = (SELECT SUM(((Speed*1000)/60)/60)/1000 FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID) WHERE Trip_ID = %d\00"
@.str.2 = [51 x i8] c"Update trip data: Updating trips in DB Failed: %d\0A\00"
@.str.3 = [11 x i8] c"Query: %s\0A\00"
define i32 @update_trip_data(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [850 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i32 @sqlite3_open(i8* %9, i32** %7)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* %6
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %3
br label %33
16: ; preds = %2
%17 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([811 x i8], [811 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %7
%21 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%22 = call i32 @sqlite3_exec(i32* %20, i8* %21, i32 0, i32 0, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i32, i32* %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [850 x i8], [850 x i8]* %8, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i32 -1, i32* %3
br label %33
30: ; preds = %16
%31 = load i32*, i32** %7
%32 = call i32 @sqlite3_close(i32* %31)
store i32 0, i32* %3
br label %33
33: ; preds = %30, %25, %13
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [50 x i8] c"Update trip data: Database connection failed: %d\0A\00"
@.str.1 = [811 x i8] c"UPDATE Trip SET FirstData = (SELECT MIN(TimeStamp) FROM ( SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = Trip.Trip_ID )), LastData = (SELECT MAX(TimeStamp) FROM ( SELECT Timestamp FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM GsmData WHERE GsmData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM ObdData WHERE ObdData.Trip_ID = Trip.Trip_ID UNION SELECT Timestamp FROM WiiData WHERE WiiData.Trip_ID = Trip.Trip_ID )), CalculatedKilometers = (SELECT SUM(((Speed*1000)/60)/60)/1000 FROM GpsData WHERE GpsData.Trip_ID = Trip.Trip_ID) WHERE Trip_ID = %d\00"
@.str.2 = [51 x i8] c"Update trip data: Updating trips in DB Failed: %d\0A\00"
@.str.3 = [11 x i8] c"Query: %s\0A\00"
define i32 @update_trip_data(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca [850 x i8]
%5 = bitcast i32** %3 to i8*
%6 = call i32 @sqlite3_open(i8* %0, i32** %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %24
10: ; preds = %2
%11 = getelementptr inbounds [850 x i8], [850 x i8]* %4, i64 0, i64 0
%12 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([811 x i8], [811 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%13 = load i32*, i32** %3
%14 = call i32 @sqlite3_exec(i32* %13, i8* %11, i32 0, i32 0, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %22
19: ; preds = %10
%20 = load i32*, i32** %3
%21 = call i32 @sqlite3_close(i32* %20)
br label %22
22: ; preds = %19, %16
%23 = phi i32 [ -1, %16 ], [ 0, %19 ]
br label %24
24: ; preds = %22, %8
%25 = phi i32 [ -1, %8 ], [ %23, %22 ]
ret i32 %25
}