#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 *);
char *get_table_name(char *, char *);
int /*<<< orphan*/ piece(char *, char, char *, int);
char *pipe2string(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int get_latest_measurements_per_day(char *application, char *station,
                                    char *datatype) {
  char buffer[1024];
  char *table_name;
  char *results;
  char latest_begin_measurement_date[16];
  char latest_begin_measurement_time[16];
  char measurement_time_where[128];
  table_name = get_table_name(application, "data_collection_frequency");
  sprintf(buffer,
          "echo \"	select	begin_measurement_date,		 "
          "			begin_measurement_time		 "
          "		from %s					 "
          "		where station = '%s'			 "
          "		  and datatype = '%s';\"		|"
          "sql '^'						|"
          "sort							|"
          "tail -1						 ",
          table_name, station, datatype);
  if (!(results = pipe2string(buffer)))
    return 0;
  piece(latest_begin_measurement_date, '^', results, 0);
  piece(latest_begin_measurement_time, '^', results, 1);
  if (!*latest_begin_measurement_time) {
    strcpy(measurement_time_where, "(begin_measurement_time = ''"
                                   "or begin_measurement_time is null)");
  } else {
    sprintf(measurement_time_where, "begin_measurement_time = '%s'",
            latest_begin_measurement_time);
  }
  sprintf(buffer,
          "echo \"	select expected_count 			 "
          "		from %s					 "
          "		where station = '%s'			 "
          "		  and datatype = '%s'			 "
          "		  and begin_measurement_date = '%s'	 "
          "and %s;\"						|"
          "sql							 ",
          table_name, station, datatype, latest_begin_measurement_date,
          measurement_time_where);
  results = pipe2string(buffer);
  if (!results)
    return 0;
  else
    return atoi(results);
}


x86 assembly (clang, -O0):
<get_latest_measurements_per_day>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1040 <get_table_name@plt>
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <pipe2string@plt>
mov    %rax,-0x430(%rbp)
cmp    $0x0,%rax
jne    11db <get_latest_measurements_per_day+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <get_latest_measurements_per_day+0x165>
lea    -0x440(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    $0x5e,%esi
xor    %ecx,%ecx
call   1080 <piece@plt>
lea    -0x450(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    $0x5e,%esi
mov    $0x1,%ecx
call   1080 <piece@plt>
cmpb   $0x0,-0x450(%rbp)
jne    1237 <get_latest_measurements_per_day+0xd7>
lea    -0x4d0(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1253 <get_latest_measurements_per_day+0xf3>
lea    -0x4d0(%rbp),%rdi
lea    -0x450(%rbp),%rdx
lea    0xebf(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    -0x440(%rbp),%r9
lea    -0x4d0(%rbp),%rax
lea    0xeab(%rip),%rsi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <pipe2string@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    12b6 <get_latest_measurements_per_day+0x156>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <get_latest_measurements_per_day+0x165>
mov    -0x430(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_latest_measurements_per_day>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4b0,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe84(%rip),%rsi
call   1040 <get_table_name@plt>
mov    %rax,%r12
lea    0xe8f(%rip),%rsi
xor    %ebp,%ebp
lea    0xb0(%rsp),%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <pipe2string@plt>
test   %rax,%rax
je     1262 <get_latest_measurements_per_day+0x102>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x5e,%esi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1080 <piece@plt>
lea    0x10(%rsp),%rdi
mov    $0x5e,%esi
mov    %rbx,%rdx
mov    $0x1,%ecx
call   1080 <piece@plt>
cmpb   $0x0,0x10(%rsp)
je     1208 <get_latest_measurements_per_day+0xa8>
lea    0xf16(%rip),%rsi
lea    0x30(%rsp),%rdi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    1219 <get_latest_measurements_per_day+0xb9>
lea    0xebd(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0x30(%rsp),%rax
mov    %rax,(%rsp)
lea    0xf00(%rip),%rsi
xor    %ebp,%ebp
lea    0xb0(%rsp),%rbx
lea    0x20(%rsp),%r9
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <pipe2string@plt>
test   %rax,%rax
je     1262 <get_latest_measurements_per_day+0x102>
mov    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x4b0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"data_collection_frequency\00"
@.str.1 = [178 x i8] c"echo \22\09select\09begin_measurement_date,\09\09 \09\09\09begin_measurement_time\09\09 \09\09from %s\09\09\09\09\09 \09\09where station = '%s'\09\09\09 \09\09 and datatype = '%s';\22\09\09|sql '^'\09\09\09\09\09\09|sort\09\09\09\09\09\09\09|tail -1\09\09\09\09\09\09 \00"
@.str.2 = [63 x i8] c"(begin_measurement_time = ''or begin_measurement_time is null)\00"
@.str.3 = [30 x i8] c"begin_measurement_time = '%s'\00"
@.str.4 = [167 x i8] c"echo \22\09select expected_count \09\09\09 \09\09from %s\09\09\09\09\09 \09\09where station = '%s'\09\09\09 \09\09 and datatype = '%s'\09\09\09 \09\09 and begin_measurement_date = '%s'\09 and %s;\22\09\09\09\09\09\09|sql\09\09\09\09\09\09\09 \00"
define i32 @get_latest_measurements_per_day(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [16 x i8]
%12 = alloca [16 x i8]
%13 = alloca [128 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i8*, i8** %5
%15 = call i8* @get_table_name(i8* %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %9
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%17 = load i8*, i8** %9
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %18, i8* %19)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%22 = call i8* @pipe2string(i8* %21)
store i8* %22, i8** %10
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %3
store i32 0, i32* %4
br label %58
25: ; preds = %3
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%27 = load i8*, i8** %10
%28 = call i32 @piece(i8* %26, i8 signext 94, i8* %27, i32 0)
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%30 = load i8*, i8** %10
%31 = call i32 @piece(i8* %29, i8 signext 94, i8* %30, i32 1)
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %25
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%37 = call i32 @strcpy(i8* %36, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
br label %42
38: ; preds = %25
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%41 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38, %35
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i8*, i8** %9
%45 = load i8*, i8** %6
%46 = load i8*, i8** %7
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([167 x i8], [167 x i8]* @.str.4, i64 0, i64 0), i8* %44, i8* %45, i8* %46, i8* %47, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = call i8* @pipe2string(i8* %50)
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = icmp ne i8* %52, null
br i1 %53, label %55, label %54
54: ; preds = %42
store i32 0, i32* %4
br label %58
55: ; preds = %42
%56 = load i8*, i8** %10
%57 = call i32 @atoi(i8* %56)
store i32 %57, i32* %4
br label %58
58: ; preds = %55, %54, %24
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [26 x i8] c"data_collection_frequency\00"
@.str.1 = [178 x i8] c"echo \22\09select\09begin_measurement_date,\09\09 \09\09\09begin_measurement_time\09\09 \09\09from %s\09\09\09\09\09 \09\09where station = '%s'\09\09\09 \09\09 and datatype = '%s';\22\09\09|sql '^'\09\09\09\09\09\09|sort\09\09\09\09\09\09\09|tail -1\09\09\09\09\09\09 \00"
@.str.2 = [63 x i8] c"(begin_measurement_time = ''or begin_measurement_time is null)\00"
@.str.3 = [30 x i8] c"begin_measurement_time = '%s'\00"
@.str.4 = [167 x i8] c"echo \22\09select expected_count \09\09\09 \09\09from %s\09\09\09\09\09 \09\09where station = '%s'\09\09\09 \09\09 and datatype = '%s'\09\09\09 \09\09 and begin_measurement_date = '%s'\09 and %s;\22\09\09\09\09\09\09|sql\09\09\09\09\09\09\09 \00"
define i32 @get_latest_measurements_per_day(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = alloca [128 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%12 = tail call i8* @get_table_name(i8* %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([178 x i8], [178 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %1, i8* %2)
%14 = call i8* @pipe2string(i8* %8)
%15 = icmp eq i8* %14, null
br i1 %15, label %31, label %16
16: ; preds = %3
%17 = call i32 @piece(i8* %9, i8 signext 94, i8* %14, i32 0)
%18 = call i32 @piece(i8* %10, i8 signext 94, i8* %14, i32 1)
%19 = load i8, i8* %10
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
br label %25
23: ; preds = %16
%24 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %25
25: ; preds = %23, %21
%26 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([167 x i8], [167 x i8]* @.str.4, i64 0, i64 0), i8* %12, i8* %1, i8* %2, i8* %9, i8* %11)
%27 = call i8* @pipe2string(i8* %8)
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @atoi(i8* %27)
br label %31
31: ; preds = %25, %3, %29
%32 = phi i32 [ %30, %29 ], [ 0, %3 ], [ 0, %25 ]
ret i32 %32
}