#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 */
scalar_t__ json_getKey(char *, int, int *, int *, int *);
char *json_type_toString(int);
int /*<<< orphan*/ json_util_printSubstring(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
void test_json_getKey() {
  puts("Test json_getKey");
  puts("======================================================================="
       "=========");
  char *str[100] = {"[\"contents\"][0][\"productID\"][-123]",
                    "\"abc\"][-0][\"abc\"]", "[\"abc\"][-0][\"abc\"",
                    "[a][b][c][d][e][f]", "[]"};
  int i;
  for (i = 0; str[i] != NULL; i++) {
    printf("\n%s\n\n", str[i]);
    int counter = 1;
    int j = 0;
    while (str[i][j] != '\0') {
      int startIndex, endIndex, jsonType;
      if (json_getKey(str[i], j, &startIndex, &endIndex, &jsonType) != 0) {
        printf("%d. get key failure\n\n", counter++);
        break;
      }
      printf("%d. ", counter++);
      json_util_printSubstring(str[i], startIndex, endIndex);
      printf(" (%s)\n\n", json_type_toString(jsonType));
      j = endIndex + 2;
    }
    puts("---------------------------------------------------------------------"
         "-----------");
  }
  puts("======================================================================="
       "=========\n");
}


x86 assembly (clang, -O0):
<test_json_getKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
lea    -0x320(%rbp),%rdi
xor    %esi,%esi
mov    $0x320,%edx
call   1060 <memset@plt>
lea    0xec5(%rip),%rax
mov    %rax,-0x320(%rbp)
lea    0xeda(%rip),%rax
mov    %rax,-0x318(%rbp)
lea    0xede(%rip),%rax
mov    %rax,-0x310(%rbp)
lea    0xee2(%rip),%rax
mov    %rax,-0x308(%rbp)
lea    0xee7(%rip),%rax
mov    %rax,-0x300(%rbp)
movl   $0x0,-0x324(%rbp)
movslq -0x324(%rbp),%rax
cmpq   $0x0,-0x320(%rbp,%rax,8)
je     133a <test_json_getKey+0x1da>
movslq -0x324(%rbp),%rax
mov    -0x320(%rbp,%rax,8),%rsi
ff
lea    0xead(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x328(%rbp)
movl   $0x0,-0x32c(%rbp)
movslq -0x324(%rbp),%rax
mov    -0x320(%rbp,%rax,8),%rax
ff
movslq -0x32c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     131a <test_json_getKey+0x1ba>
movslq -0x324(%rbp),%rax
mov    -0x320(%rbp,%rax,8),%rdi
ff
mov    -0x32c(%rbp),%esi
lea    -0x330(%rbp),%rdx
lea    -0x334(%rbp),%rcx
lea    -0x338(%rbp),%r8
call   1080 <json_getKey@plt>
cmp    $0x0,%rax
je     12ac <test_json_getKey+0x14c>
mov    -0x328(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x328(%rbp)
movslq %eax,%rsi
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
jmp    131a <test_json_getKey+0x1ba>
mov    -0x328(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x328(%rbp)
movslq %eax,%rsi
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
movslq -0x324(%rbp),%rax
mov    -0x320(%rbp,%rax,8),%rdi
ff
mov    -0x330(%rbp),%esi
mov    -0x334(%rbp),%edx
call   1040 <json_util_printSubstring@plt>
mov    -0x338(%rbp),%edi
call   1070 <json_type_toString@plt>
mov    %rax,%rsi
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
mov    -0x334(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x32c(%rbp)
jmp    122b <test_json_getKey+0xcb>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
mov    -0x324(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x324(%rbp)
jmp    11e6 <test_json_getKey+0x86>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_json_getKey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x338,%rsp
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rdi
xor    %r12d,%r12d
mov    $0x300,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xebe(%rip),%rbp
mov    %rbp,0x10(%rsp)
lea    0xed5(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xedb(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xee1(%rip),%rax
mov    %rax,0x28(%rsp)
lea    0xee8(%rip),%rax
mov    %rax,0x30(%rsp)
lea    0x4(%rsp),%r13
lea    0x8(%rsp),%rbx
lea    0xef6(%rip),%r15
jmp    121d <test_json_getKey+0xbd>
nopl   0x0(%rax)
mov    %r14d,%esi
lea    0xecb(%rip),%rdi
call   1050 <printf@plt>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
mov    0x18(%rsp,%r12,8),%rbp
add    $0x1,%r12
test   %rbp,%rbp
je     12a4 <test_json_getKey+0x144>
lea    0xe9b(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
cmpb   $0x0,0x0(%rbp)
je     11ff <test_json_getKey+0x9f>
xor    %esi,%esi
mov    $0x1,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xc(%rsp),%rdx
mov    %r13,%rcx
mov    %rbx,%r8
call   1080 <json_getKey@plt>
test   %rax,%rax
jne    11f0 <test_json_getKey+0x90>
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    0xc(%rsp),%esi
mov    0x4(%rsp),%edx
mov    %rbp,%rdi
call   1040 <json_util_printSubstring@plt>
mov    0x8(%rsp),%edi
call   1070 <json_type_toString@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
movslq 0x4(%rsp),%rax
lea    0x2(%rax),%rsi
add    $0x1,%r14
cmpb   $0x0,0x2(%rbp,%rax,1)
jne    1240 <test_json_getKey+0xe0>
jmp    11ff <test_json_getKey+0x9f>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
add    $0x338,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Test json_getKey\00"
@.str.1 = [81 x i8] c"================================================================================\00"
@.str.2 = [35 x i8] c"[\22contents\22][0][\22productID\22][-123]\00"
@.str.3 = [18 x i8] c"\22abc\22][-0][\22abc\22]\00"
@.str.4 = [18 x i8] c"[\22abc\22][-0][\22abc\22\00"
@.str.5 = [19 x i8] c"[a][b][c][d][e][f]\00"
@.str.6 = [3 x i8] c"[]\00"
@.str.7 = [6 x i8] c"\0A%s\0A\0A\00"
@.str.8 = [22 x i8] c"%d. get key failure\0A\0A\00"
@.str.9 = [5 x i8] c"%d. \00"
@.str.10 = [8 x i8] c" (%s)\0A\0A\00"
@.str.11 = [81 x i8] c"--------------------------------------------------------------------------------\00"
@.str.12 = [82 x i8] c"================================================================================\0A\00"
define void @test_json_getKey() {
%1 = alloca [100 x i8*]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @puts(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0))
%10 = bitcast [100 x i8*]* %1 to i8*
%11 = bitcast i8* %10 to <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>*
%12 = getelementptr inbounds <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>, <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>* %11, i32 0, i32 0
store i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>, <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>* %11, i32 0, i32 1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>, <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>* %11, i32 0, i32 2
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>, <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>* %11, i32 0, i32 3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>, <{ i8*, i8*, i8*, i8*, i8*, [95 x i8*] }>* %11, i32 0, i32 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8** %16
store i32 0, i32* %2
br label %17
17: ; preds = %74, %0
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %77
23: ; preds = %17
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %27)
store i32 1, i32* %3
store i32 0, i32* %4
br label %29
29: ; preds = %54, %23
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %72
40: ; preds = %29
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %42
%44 = load i8*, i8** %43
%45 = load i32, i32* %4
%46 = call i64 @json_getKey(i8* %44, i32 %45, i32* %5, i32* %6, i32* %7)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %40
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
%51 = sext i32 %49 to i64
%52 = inttoptr i64 %51 to i8*
%53 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i8* %52)
br label %72
54: ; preds = %40
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
%57 = sext i32 %55 to i64
%58 = inttoptr i64 %57 to i8*
%59 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %2
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %61
%63 = load i8*, i8** %62
%64 = load i32, i32* %5
%65 = load i32, i32* %6
%66 = call i32 @json_util_printSubstring(i8* %63, i32 %64, i32 %65)
%67 = load i32, i32* %7
%68 = call i8* @json_type_toString(i32 %67)
%69 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 2
store i32 %71, i32* %4
br label %29
72: ; preds = %48, %29
%73 = call i32 @puts(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.11, i64 0, i64 0))
br label %74
74: ; preds = %72
%75 = load i32, i32* %2
%76 = add nsw i32 %75, 1
store i32 %76, i32* %2
br label %17
77: ; preds = %17
%78 = call i32 @puts(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.12, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"Test json_getKey\00"
@.str.1 = [81 x i8] c"================================================================================\00"
@.str.2 = [35 x i8] c"[\22contents\22][0][\22productID\22][-123]\00"
@.str.3 = [18 x i8] c"\22abc\22][-0][\22abc\22]\00"
@.str.4 = [18 x i8] c"[\22abc\22][-0][\22abc\22\00"
@.str.5 = [19 x i8] c"[a][b][c][d][e][f]\00"
@.str.6 = [3 x i8] c"[]\00"
@.str.7 = [6 x i8] c"\0A%s\0A\0A\00"
@.str.8 = [22 x i8] c"%d. get key failure\0A\0A\00"
@.str.9 = [5 x i8] c"%d. \00"
@.str.10 = [8 x i8] c" (%s)\0A\0A\00"
@.str.11 = [81 x i8] c"--------------------------------------------------------------------------------\00"
@.str.12 = [82 x i8] c"================================================================================\0A\00"
define void @test_json_getKey() {
%1 = alloca [100 x i8*]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0))
%7 = bitcast [100 x i8*]* %1 to i8*
%8 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 4
%9 = bitcast i8** %8 to i8*
%10 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 0
store i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 2
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %14
%15 = bitcast i32* %2 to i8*
%16 = bitcast i32* %3 to i8*
%17 = bitcast i32* %4 to i8*
br label %18
18: ; preds = %0, %49
%19 = phi i64 [ 0, %0 ], [ %51, %49 ]
%20 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), %0 ], [ %53, %49 ]
%21 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %20)
%22 = load i8, i8* %20
%23 = icmp eq i8 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %18, %33
%25 = phi i64 [ %44, %33 ], [ 1, %18 ]
%26 = phi i32 [ %43, %33 ], [ 0, %18 ]
%27 = call i64 @json_getKey(i8* %20, i32 %26, i32* %2, i32* %3, i32* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = and i64 %25, 4294967295
%31 = inttoptr i64 %30 to i8*
%32 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i8* %31)
br label %49
33: ; preds = %24
%34 = inttoptr i64 %25 to i8*
%35 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %2
%37 = load i32, i32* %3
%38 = call i32 @json_util_printSubstring(i8* %20, i32 %36, i32 %37)
%39 = load i32, i32* %4
%40 = call i8* @json_type_toString(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 2
%44 = add nuw i64 %25, 1
%45 = sext i32 %43 to i64
%46 = getelementptr inbounds i8, i8* %20, i64 %45
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 0
br i1 %48, label %49, label %24
49: ; preds = %33, %18, %29
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.11, i64 0, i64 0))
%51 = add nuw i64 %19, 1
%52 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 %51
%53 = load i8*, i8** %52
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %18
55: ; preds = %49
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @.str.12, i64 0, i64 0))
ret void
}