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

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int const);
int strlen(char const *);
char *sql_rowcount(const char *str) {
  static const char *ins = "COUNT(*)";
  static const unsigned offset = (sizeof(ins) / sizeof(*ins));
  char *out = (char *)malloc(sizeof(char) * strlen(str) + offset + 1);
  unsigned i, j = 0;
  for (i = 0; str[i]; i++) {
    if (str[i] == '*') {
      memcpy(&out[j], ins, offset);
      j += offset;
    } else
      out[j++] = str[i];
  }
  out[j] = '\0';
  return out;
}


x86 assembly (clang, -O0):
<sql_rowcount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cltq
shl    $0x0,%rax
add    $0x8,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
cmpb   $0x0,(%rax,%rcx,1)
je     11eb <sql_rowcount+0xbb>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2a,%eax
jne    11ba <sql_rowcount+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
add    %rax,%rdi
mov    0x2e96(%rip),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x18(%rbp)
jmp    11d8 <sql_rowcount+0xa8>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    11dd <sql_rowcount+0xad>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <sql_rowcount+0x3c>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sql_rowcount>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x9(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
xor    %ebp,%ebp
lea    0xeac(%rip),%r15
xor    %ebx,%ebx
jmp    1178 <sql_rowcount+0x48>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
add    %r12,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
add    $0x8,%ebx
add    $0x1,%ebp
mov    %ebp,%eax
movzbl (%r14,%rax,1),%eax
cmp    $0x2a,%al
je     1160 <sql_rowcount+0x30>
mov    %ebx,%ecx
test   %al,%al
je     1195 <sql_rowcount+0x65>
add    $0x1,%ebx
mov    %al,(%r12,%rcx,1)
add    $0x1,%ebp
jmp    1178 <sql_rowcount+0x48>
movb   $0x0,(%r12,%rcx,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@sql_rowcount.ins = internal global i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0)
@.str = [9 x i8] c"COUNT(*)\00"
@sql_rowcount.offset = internal constant i32 8
define i8* @sql_rowcount(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = add i64 %9, 8
%11 = add i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %3
store i32 0, i32* %5
store i32 0, i32* %4
br label %15
15: ; preds = %51, %1
%16 = load i8*, i8** %2
%17 = load i32, i32* %4
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %54
22: ; preds = %15
%23 = load i8*, i8** %2
%24 = load i32, i32* %4
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 42
br i1 %29, label %30, label %39
30: ; preds = %22
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8*, i8** @sql_rowcount.ins
%36 = call i32 @memcpy(i8* %34, i8* %35, i32 8)
%37 = load i32, i32* %5
%38 = add i32 %37, 8
store i32 %38, i32* %5
br label %50
39: ; preds = %22
%40 = load i8*, i8** %2
%41 = load i32, i32* %4
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = load i8*, i8** %3
%46 = load i32, i32* %5
%47 = add i32 %46, 1
store i32 %47, i32* %5
%48 = zext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 %44, i8* %49
br label %50
50: ; preds = %39, %30
br label %51
51: ; preds = %50
%52 = load i32, i32* %4
%53 = add i32 %52, 1
store i32 %53, i32* %4
br label %15
54: ; preds = %15
%55 = load i8*, i8** %3
%56 = load i32, i32* %5
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@.str = [9 x i8] c"COUNT(*)\00"
define i8* @sql_rowcount(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add i32 %2, 9
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
br label %6
6: ; preds = %21, %1
%7 = phi i32 [ 0, %1 ], [ %23, %21 ]
%8 = phi i32 [ 0, %1 ], [ %22, %21 ]
%9 = zext i32 %7 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
switch i8 %11, label %17 [
i8 0, label %24
i8 42, label %12
]
12: ; preds = %6
%13 = zext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = tail call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%16 = add i32 %8, 8
br label %21
17: ; preds = %6
%18 = add i32 %8, 1
%19 = zext i32 %8 to i64
%20 = getelementptr inbounds i8, i8* %5, i64 %19
store i8 %11, i8* %20
br label %21
21: ; preds = %12, %17
%22 = phi i32 [ %16, %12 ], [ %18, %17 ]
%23 = add i32 %7, 1
br label %6
24: ; preds = %6
%25 = zext i32 %8 to i64
%26 = getelementptr inbounds i8, i8* %5, i64 %25
store i8 0, i8* %26
ret i8* %5
}