#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 */
char *bracket_end(char *);
int /*<<< orphan*/ lua_error(char *);
char *item_end(char *p) {
  switch (*p++) {
  case '\0':
    return p - 1;
  case '%':
    if (*p == 0)
      lua_error("incorrect pattern");
    return p + 1;
  case '[': {
    char *end = bracket_end(p);
    if (end == NULL)
      lua_error("incorrect pattern");
    return end + 1;
  }
  default:
    return p;
  }
}


x86 assembly (clang, -O0):
<item_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     1170 <item_end+0x50>
jmp    114e <item_end+0x2e>
mov    -0x1c(%rbp),%eax
sub    $0x25,%eax
je     1181 <item_end+0x61>
jmp    115f <item_end+0x3f>
mov    -0x1c(%rbp),%eax
sub    $0x5b,%eax
je     11ae <item_end+0x8e>
jmp    11e3 <item_end+0xc3>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    11eb <item_end+0xcb>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    119d <item_end+0x7d>
lea    0xe68(%rip),%rdi
call   1040 <lua_error@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11eb <item_end+0xcb>
mov    -0x10(%rbp),%rdi
call   1030 <bracket_end@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <item_end+0xb2>
lea    0xe33(%rip),%rdi
call   1040 <lua_error@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11eb <item_end+0xcb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<item_end>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movsbl (%rdi),%eax
test   %eax,%eax
je     1152 <item_end+0x32>
lea    0x1(%rbx),%r14
cmp    $0x5b,%eax
je     1157 <item_end+0x37>
cmp    $0x25,%eax
jne    1177 <item_end+0x57>
cmpb   $0x0,(%r14)
jne    114e <item_end+0x2e>
lea    0xeb7(%rip),%rdi
call   1040 <lua_error@plt>
add    $0x2,%rbx
mov    %rbx,%r14
jmp    1177 <item_end+0x57>
mov    %r14,%rdi
call   1030 <bracket_end@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1173 <item_end+0x53>
lea    0xe92(%rip),%rdi
call   1040 <lua_error@plt>
add    $0x1,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [18 x i8] c"incorrect pattern\00"
define i8* @item_end(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = getelementptr inbounds i8, i8* %5, i32 1
store i8* %6, i8** %3
%7 = load i8, i8* %5
%8 = sext i8 %7 to i32
switch i32 %8, label %32 [
i32 0, label %9
i32 37, label %12
i32 91, label %22
]
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 -1
store i8* %11, i8** %2
br label %34
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %2
br label %34
22: ; preds = %1
%23 = load i8*, i8** %3
%24 = call i8* @bracket_end(i8* %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8* %31, i8** %2
br label %34
32: ; preds = %1
%33 = load i8*, i8** %3
store i8* %33, i8** %2
br label %34
34: ; preds = %32, %29, %19, %9
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
@.str = [18 x i8] c"incorrect pattern\00"
define i8* @item_end(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 1
%3 = load i8, i8* %0
%4 = sext i8 %3 to i32
switch i32 %4, label %19 [
i32 0, label %20
i32 37, label %5
i32 91, label %12
]
5: ; preds = %1
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = getelementptr inbounds i8, i8* %0, i64 2
br label %20
12: ; preds = %1
%13 = tail call i8* @bracket_end(i8* %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @lua_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = getelementptr inbounds i8, i8* %13, i64 1
br label %20
19: ; preds = %1
br label %20
20: ; preds = %1, %19, %17, %10
%21 = phi i8* [ %2, %19 ], [ %18, %17 ], [ %11, %10 ], [ %0, %1 ]
ret i8* %21
}