#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*/ isalpha(char);
scalar_t__ isdigit(char);
int /*<<< orphan*/ sqlite3_keyword_check(char const *, int);
char *sqlite3_mprintf(char *, ...);
char *safeId(const char *zId) {
  int i, x;
  char c;
  if (zId[0] == 0)
    return sqlite3_mprintf("\"\"");
  for (i = x = 0; (c = zId[i]) != 0; i++) {
    if (!isalpha(c) && c != '_') {
      if (i > 0 && isdigit(c)) {
        x++;
      } else {
        return sqlite3_mprintf("\"%w\"", zId);
      }
    }
  }
  if (x || !sqlite3_keyword_check(zId, i)) {
    return sqlite3_mprintf("%s", zId);
  }
  return sqlite3_mprintf("\"%w\"", zId);
}


x86 assembly (clang, -O0):
<safeId>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1173 <safeId+0x33>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1060 <sqlite3_mprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1268 <safeId+0x128>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     1218 <safeId+0xd8>
movsbl -0x19(%rbp),%edi
call   1040 <isalpha@plt>
cmp    $0x0,%eax
jne    1205 <safeId+0xc5>
movsbl -0x19(%rbp),%eax
cmp    $0x5f,%eax
je     1205 <safeId+0xc5>
cmpl   $0x0,-0x14(%rbp)
jle    11e5 <safeId+0xa5>
movsbl -0x19(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11e5 <safeId+0xa5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1200 <safeId+0xc0>
mov    -0x10(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <sqlite3_mprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1268 <safeId+0x128>
jmp    1205 <safeId+0xc5>
jmp    120a <safeId+0xca>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1181 <safeId+0x41>
cmpl   $0x0,-0x18(%rbp)
jne    1237 <safeId+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <sqlite3_keyword_check@plt>
cmp    $0x0,%eax
jne    1252 <safeId+0x112>
mov    -0x10(%rbp),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1060 <sqlite3_mprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    1268 <safeId+0x128>
mov    -0x10(%rbp),%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <sqlite3_mprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeId>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    (%rdi),%bl
test   %bl,%bl
je     119b <safeId+0x5b>
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %r15d,%r15d
jmp    116e <safeId+0x2e>
nopl   0x0(%rax,%rax,1)
movzbl 0x1(%r14,%rbp,1),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     11b1 <safeId+0x71>
movsbl %bl,%r12d
mov    %r12d,%edi
call   1040 <isalpha@plt>
test   %eax,%eax
jne    1160 <safeId+0x20>
cmp    $0x5f,%bl
je     1160 <safeId+0x20>
test   %rbp,%rbp
je     11cd <safeId+0x8d>
mov    %r12d,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11cd <safeId+0x8d>
add    $0x1,%r15d
jmp    1160 <safeId+0x20>
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sqlite3_mprintf@plt>
test   %r15d,%r15d
je     11bf <safeId+0x7f>
lea    0xe4b(%rip),%rdi
jmp    11d4 <safeId+0x94>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <sqlite3_keyword_check@plt>
test   %eax,%eax
je     11b6 <safeId+0x76>
lea    0xe2f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <sqlite3_mprintf@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"\22\22\00"
@.str.1 = [5 x i8] c"\22%w\22\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @safeId(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %2
br label %63
14: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %4
br label %15
15: ; preds = %46, %14
%16 = load i8*, i8** %3
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
store i8 %20, i8* %6
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %49
23: ; preds = %15
%24 = load i8, i8* %6
%25 = call i32 @isalpha(i8 signext %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %45, label %27
27: ; preds = %23
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 95
br i1 %30, label %31, label %45
31: ; preds = %27
%32 = load i32, i32* %4
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i8, i8* %6
%36 = call i64 @isdigit(i8 signext %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %44
41: ; preds = %34, %31
%42 = load i8*, i8** %3
%43 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %42)
store i8* %43, i8** %2
br label %63
44: ; preds = %38
br label %45
45: ; preds = %44, %27, %23
br label %46
46: ; preds = %45
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %15
49: ; preds = %15
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %49
%53 = load i8*, i8** %3
%54 = load i32, i32* %4
%55 = call i32 @sqlite3_keyword_check(i8* %53, i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %52, %49
%58 = load i8*, i8** %3
%59 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %58)
store i8* %59, i8** %2
br label %63
60: ; preds = %52
%61 = load i8*, i8** %3
%62 = call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %61)
store i8* %62, i8** %2
br label %63
63: ; preds = %60, %57, %41, %12
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [3 x i8] c"\22\22\00"
@.str.1 = [5 x i8] c"\22%w\22\00"
@.str.2 = [3 x i8] c"%s\00"
define i8* @safeId(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %39
6: ; preds = %1, %23
%7 = phi i64 [ %25, %23 ], [ 0, %1 ]
%8 = phi i8 [ %27, %23 ], [ %2, %1 ]
%9 = phi i32 [ %24, %23 ], [ 0, %1 ]
%10 = tail call i32 @isalpha(i8 signext %8)
%11 = icmp eq i32 %10, 0
%12 = icmp ne i8 %8, 95
%13 = and i1 %11, %12
br i1 %13, label %14, label %23
14: ; preds = %6
%15 = icmp eq i64 %7, 0
br i1 %15, label %21, label %16
16: ; preds = %14
%17 = tail call i64 @isdigit(i8 signext %8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = add nsw i32 %9, 1
br label %23
21: ; preds = %16, %14
%22 = tail call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
23: ; preds = %6, %19
%24 = phi i32 [ %20, %19 ], [ %9, %6 ]
%25 = add nuw i64 %7, 1
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %6
29: ; preds = %23
%30 = icmp eq i32 %24, 0
br i1 %30, label %31, label %35
31: ; preds = %29
%32 = trunc i64 %25 to i32
%33 = tail call i32 @sqlite3_keyword_check(i8* %0, i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31, %29
%36 = tail call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %39
37: ; preds = %31
%38 = tail call i8* (i8*, ...) @sqlite3_mprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
39: ; preds = %37, %35, %21, %4
%40 = phi i8* [ %5, %4 ], [ %22, %21 ], [ %36, %35 ], [ %38, %37 ]
ret i8* %40
}