#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 /*<<< orphan*/ printf(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int check_sql_for_select(char *sql) {
  int check = 0;
  char replace1[] = "SELECT ";
  char replace2[] = " FROM ";
  char replace3[] = " AS ";
  char *sp = NULL;
  char *fp = NULL;
  char *ap = NULL;
  if (sql != NULL && strlen(sql) > 12) {
    if ((sp = strstr(sql, "select "))) {
      strncpy(sp, replace1, 7);
    }
    if ((sp = strstr(sql, "Select "))) {
      strncpy(sp, replace1, 7);
    }
    if ((fp = strstr(sql, " from "))) {
      strncpy(fp, replace2, 6);
    }
    if ((fp = strstr(sql, " From "))) {
      strncpy(fp, replace2, 6);
    }
    if ((ap = strstr(sql, " As "))) {
      strncpy(ap, replace3, 4);
    }
    if ((ap = strstr(sql, " as "))) {
      strncpy(ap, replace3, 4);
    }
  }
  if (sql != NULL && strlen(sql) > 12) {
    if (strstr(sql, "SELECT ") && strstr(sql, " FROM")) {
      check = 0;
    }
  } else {
    check = 1;
    printf("The SQL statement needs SELECT and FROM keywords with fields and "
           "tables.\n");
  }
  return check;
}


x86 assembly (clang, -O0):
<check_sql_for_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0xea6(%rip),%rax
mov    %rax,-0x14(%rbp)
mov    0xea4(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe9e(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    0xe96(%rip),%al
mov    %al,-0x15(%rbp)
mov    0xe8e(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe89(%rip),%al
mov    %al,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12e7 <check_sql_for_select+0x1a7>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xc,%eax
jle    12e7 <check_sql_for_select+0x1a7>
mov    -0x8(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f2 <check_sql_for_select+0xb2>
mov    -0x28(%rbp),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1222 <check_sql_for_select+0xe2>
mov    -0x28(%rbp),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1252 <check_sql_for_select+0x112>
mov    -0x30(%rbp),%rdi
lea    -0x1b(%rbp),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xdce(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1282 <check_sql_for_select+0x142>
mov    -0x30(%rbp),%rdi
lea    -0x1b(%rbp),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12b2 <check_sql_for_select+0x172>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12e2 <check_sql_for_select+0x1a2>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
jmp    12e7 <check_sql_for_select+0x1a7>
cmpq   $0x0,-0x8(%rbp)
je     1344 <check_sql_for_select+0x204>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xc,%eax
jle    1344 <check_sql_for_select+0x204>
mov    -0x8(%rbp),%rdi
lea    0xcf1(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     133f <check_sql_for_select+0x1ff>
mov    -0x8(%rbp),%rdi
lea    0xd13(%rip),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
je     133f <check_sql_for_select+0x1ff>
movl   $0x0,-0xc(%rbp)
jmp    1357 <check_sql_for_select+0x217>
movl   $0x1,-0xc(%rbp)
lea    0xcf0(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_sql_for_select>:
push   %rbx
sub    $0x20,%rsp
movabs $0x205443454c4553,%rax
mov    %rax,0x18(%rsp)
movl   $0x204d4f,0x13(%rsp)
movl   $0x4f524620,0x10(%rsp)
4f
movb   $0x0,0xc(%rsp)
movl   $0x20534120,0x8(%rsp)
20
test   %rdi,%rdi
je     1280 <check_sql_for_select+0x140>
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    $0xd,%eax
jl     126f <check_sql_for_select+0x12f>
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11b1 <check_sql_for_select+0x71>
lea    0x18(%rsp),%rsi
mov    %rax,%rdi
mov    $0x7,%edx
call   1030 <strncpy@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11d7 <check_sql_for_select+0x97>
lea    0x18(%rsp),%rsi
mov    %rax,%rdi
mov    $0x7,%edx
call   1030 <strncpy@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11fd <check_sql_for_select+0xbd>
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0x6,%edx
call   1030 <strncpy@plt>
lea    0xe1f(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     1223 <check_sql_for_select+0xe3>
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0x6,%edx
call   1030 <strncpy@plt>
lea    0xe00(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     1249 <check_sql_for_select+0x109>
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0xddf(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     126f <check_sql_for_select+0x12f>
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xc,%ecx
jg     1291 <check_sql_for_select+0x151>
lea    0xdad(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.check_sql_for_select.replace1 = [8 x i8] c"SELECT \00"
@__const.check_sql_for_select.replace2 = [7 x i8] c" FROM \00"
@__const.check_sql_for_select.replace3 = [5 x i8] c" AS \00"
@.str = [8 x i8] c"select \00"
@.str.1 = [8 x i8] c"Select \00"
@.str.2 = [7 x i8] c" from \00"
@.str.3 = [7 x i8] c" From \00"
@.str.4 = [5 x i8] c" As \00"
@.str.5 = [5 x i8] c" as \00"
@.str.6 = [8 x i8] c"SELECT \00"
@.str.7 = [6 x i8] c" FROM\00"
@.str.8 = [74 x i8] c"The SQL statement needs SELECT and FROM keywords with fields and tables.\0A\00"
define i32 @check_sql_for_select(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [8 x i8]
%5 = alloca [7 x i8]
%6 = alloca [5 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
%10 = bitcast [8 x i8]* %4 to i8*
%11 = bitcast [7 x i8]* %5 to i8*
%12 = bitcast [5 x i8]* %6 to i8*
store i8* null, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %68
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 @strlen(i8* %16)
%18 = icmp sgt i32 %17, 12
br i1 %18, label %19, label %68
19: ; preds = %15
%20 = load i8*, i8** %2
%21 = call i8* @strstr(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i8*, i8** %7
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%26 = call i32 @strncpy(i8* %24, i8* %25, i32 7)
br label %27
27: ; preds = %23, %19
%28 = load i8*, i8** %2
%29 = call i8* @strstr(i8* %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %29, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8*, i8** %7
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%34 = call i32 @strncpy(i8* %32, i8* %33, i32 7)
br label %35
35: ; preds = %31, %27
%36 = load i8*, i8** %2
%37 = call i8* @strstr(i8* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %37, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i8*, i8** %8
%41 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%42 = call i32 @strncpy(i8* %40, i8* %41, i32 6)
br label %43
43: ; preds = %39, %35
%44 = load i8*, i8** %2
%45 = call i8* @strstr(i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i8* %45, i8** %8
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i8*, i8** %8
%49 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%50 = call i32 @strncpy(i8* %48, i8* %49, i32 6)
br label %51
51: ; preds = %47, %43
%52 = load i8*, i8** %2
%53 = call i8* @strstr(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8* %53, i8** %9
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i8*, i8** %9
%57 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%58 = call i32 @strncpy(i8* %56, i8* %57, i32 4)
br label %59
59: ; preds = %55, %51
%60 = load i8*, i8** %2
%61 = call i8* @strstr(i8* %60, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i8* %61, i8** %9
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i8*, i8** %9
%65 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%66 = call i32 @strncpy(i8* %64, i8* %65, i32 4)
br label %67
67: ; preds = %63, %59
br label %68
68: ; preds = %67, %15, %1
%69 = load i8*, i8** %2
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %85
71: ; preds = %68
%72 = load i8*, i8** %2
%73 = call i32 @strlen(i8* %72)
%74 = icmp sgt i32 %73, 12
br i1 %74, label %75, label %85
75: ; preds = %71
%76 = load i8*, i8** %2
%77 = call i8* @strstr(i8* %76, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %84
79: ; preds = %75
%80 = load i8*, i8** %2
%81 = call i8* @strstr(i8* %80, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%82 = icmp ne i8* %81, null
br i1 %82, label %83, label %84
83: ; preds = %79
store i32 0, i32* %3
br label %84
84: ; preds = %83, %79, %75
br label %87
85: ; preds = %71, %68
store i32 1, i32* %3
%86 = call i32 @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.8, i64 0, i64 0))
br label %87
87: ; preds = %85, %84
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@__const.check_sql_for_select.replace2 = [7 x i8] c" FROM \00"
@__const.check_sql_for_select.replace3 = [5 x i8] c" AS \00"
@.str = [8 x i8] c"select \00"
@.str.1 = [8 x i8] c"Select \00"
@.str.2 = [7 x i8] c" from \00"
@.str.3 = [7 x i8] c" From \00"
@.str.4 = [5 x i8] c" As \00"
@.str.5 = [5 x i8] c" as \00"
@str = [73 x i8] c"The SQL statement needs SELECT and FROM keywords with fields and tables.\00"
define i32 @check_sql_for_select(i8* %0) {
%2 = alloca i64
%3 = alloca [7 x i8]
%4 = alloca [5 x i8]
%5 = bitcast i64* %2 to i8*
store i64 9099847156909395, i64* %2
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = icmp eq i8* %0, null
br i1 %8, label %45, label %9
9: ; preds = %1
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp sgt i32 %10, 12
br i1 %11, label %12, label %42
12: ; preds = %9
%13 = tail call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @strncpy(i8* %13, i8* %5, i32 7)
br label %17
17: ; preds = %15, %12
%18 = call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @strncpy(i8* %18, i8* %5, i32 7)
br label %22
22: ; preds = %20, %17
%23 = call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @strncpy(i8* %23, i8* %6, i32 6)
br label %27
27: ; preds = %25, %22
%28 = call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = call i32 @strncpy(i8* %28, i8* %6, i32 6)
br label %32
32: ; preds = %30, %27
%33 = call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @strncpy(i8* %33, i8* %7, i32 4)
br label %37
37: ; preds = %35, %32
%38 = call i8* @strstr(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%39 = icmp eq i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @strncpy(i8* %38, i8* %7, i32 4)
br label %42
42: ; preds = %9, %40, %37
%43 = call i32 @strlen(i8* %0)
%44 = icmp sgt i32 %43, 12
br i1 %44, label %47, label %45
45: ; preds = %1, %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @str, i64 0, i64 0))
br label %47
47: ; preds = %42, %45
%48 = phi i32 [ 1, %45 ], [ 0, %42 ]
ret i32 %48
}