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

/* Variables and functions */
int /*<<< orphan*/ REG_NEWLINE;
scalar_t__ regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ str_replace(char *, char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
short int shellpattern_is_valid(const char *pattern) {
  regex_t preg;
  char regexp[1024];
  strncpy(regexp, pattern, 1023);
  str_replace(regexp, "*", ".*", 1023);
  str_replace(regexp, "(", "\\(", 1023);
  str_replace(regexp, ")", "\\)", 1023);
  str_replace(regexp, "?", ".", 1023);
  str_replace(regexp, "[!", "[^", 1023);
  if (regcomp(&preg, regexp, REG_NEWLINE) != 0)
    return (0);
  return (1);
}


x86 assembly (clang, -O0):
<shellpattern_is_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe9f(%rip),%rsi
lea    0xe97(%rip),%rdx
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    -0x420(%rbp),%rdi
lea    0xe83(%rip),%rsi
lea    0xe7b(%rip),%rdx
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    -0x420(%rbp),%rdi
lea    0xe67(%rip),%rsi
lea    0xe5f(%rip),%rdx
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    -0x420(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    0xe45(%rip),%rdx
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    -0x420(%rbp),%rdi
lea    0xe2f(%rip),%rsi
lea    0xe2b(%rip),%rdx
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    -0x420(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rdi
call   1040 <regcomp@plt>
cmp    $0x0,%rax
je     121d <shellpattern_is_valid+0xed>
movw   $0x0,-0x2(%rbp)
jmp    1223 <shellpattern_is_valid+0xf3>
movw   $0x1,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shellpattern_is_valid>:
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0xead(%rip),%rsi
lea    0xea5(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    0xe95(%rip),%rsi
lea    0xe8d(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    0xe7d(%rip),%rsi
lea    0xe75(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    0xe64(%rip),%rsi
lea    0xe5f(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
lea    0xe4d(%rip),%rsi
lea    0xe49(%rip),%rdx
mov    %rbx,%rdi
mov    $0x3ff,%ecx
call   1050 <str_replace@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
call   1040 <regcomp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,%eax
add    $0x410,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"*\00"
@.str.1 = [3 x i8] c".*\00"
@.str.2 = [2 x i8] c"(\00"
@.str.3 = [3 x i8] c"\\(\00"
@.str.4 = [2 x i8] c")\00"
@.str.5 = [3 x i8] c"\\)\00"
@.str.6 = [2 x i8] c"?\00"
@.str.7 = [2 x i8] c".\00"
@.str.8 = [3 x i8] c"[!\00"
@.str.9 = [3 x i8] c"[^\00"
@REG_NEWLINE = global i32 0
define signext i16 @shellpattern_is_valid(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
store i8* %0, i8** %3
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = call i32 @strncpy(i8* %6, i8* %7, i32 1023)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = call i32 @str_replace(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1023)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = call i32 @str_replace(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1023)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i32 @str_replace(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 1023)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @str_replace(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1023)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = call i32 @str_replace(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 1023)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* @REG_NEWLINE
%21 = call i64 @regcomp(i32* %4, i8* %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %1
store i16 0, i16* %2
br label %25
24: ; preds = %1
store i16 1, i16* %2
br label %25
25: ; preds = %24, %23
%26 = load i16, i16* %2
ret i16 %26
}

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
@.str.1 = [3 x i8] c".*\00"
@.str.2 = [2 x i8] c"(\00"
@.str.3 = [3 x i8] c"\\(\00"
@.str.4 = [2 x i8] c")\00"
@.str.5 = [3 x i8] c"\\)\00"
@.str.6 = [2 x i8] c"?\00"
@.str.7 = [2 x i8] c".\00"
@.str.8 = [3 x i8] c"[!\00"
@.str.9 = [3 x i8] c"[^\00"
@REG_NEWLINE = global i32 0
define signext i16 @shellpattern_is_valid(i8* %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 1023)
%7 = call i32 @str_replace(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 1023)
%8 = call i32 @str_replace(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 1023)
%9 = call i32 @str_replace(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 1023)
%10 = call i32 @str_replace(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1023)
%11 = call i32 @str_replace(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i32 1023)
%12 = load i32, i32* @REG_NEWLINE
%13 = call i64 @regcomp(i32* %2, i8* %5, i32 %12)
%14 = icmp eq i64 %13, 0
%15 = zext i1 %14 to i16
ret i16 %15
}