#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*/ free(char *);
char *glob_to_regex(char *);
void *realloc(char *, size_t);
char *split_and_get(char *, char, char **);
scalar_t__ sprintf(char *, char *, char *, char *);
char *strdup(char const *);
size_t strlen(char *);
char *globs_to_regex(const char globs[]) {
  char *final_regex = NULL;
  size_t final_regex_len = 0UL;
  char *globs_copy = strdup(globs);
  char *glob = globs_copy, *state = NULL;
  while ((glob = split_and_get(glob, ',', &state)) != NULL) {
    void *p;
    char *regex;
    size_t new_len;
    regex = glob_to_regex(glob);
    if (regex == NULL) {
      break;
    }
    new_len = final_regex_len + 1 + 1 + strlen(regex) + 1;
    p = realloc(final_regex, new_len + 1);
    if (p != NULL) {
      final_regex = p;
      final_regex_len += sprintf(final_regex + final_regex_len, "%s(%s)",
                                 (final_regex_len != 0UL) ? "|" : "", regex);
    }
    free(regex);
  }
  free(globs_copy);
  return final_regex;
}


x86 assembly (clang, -O0):
<globs_to_regex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2c,%esi
lea    -0x30(%rbp),%rdx
call   1040 <split_and_get@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1281 <globs_to_regex+0x111>
mov    -0x28(%rbp),%rdi
call   1060 <glob_to_regex@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11e6 <globs_to_regex+0x76>
jmp    1281 <globs_to_regex+0x111>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1273 <globs_to_regex+0x103>
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xdba(%rip),%rdx
lea    0xdb4(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
mov    -0x40(%rbp),%rcx
lea    0xd9a(%rip),%rsi
call   1080 <sprintf@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    11a9 <globs_to_regex+0x39>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<globs_to_regex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
call   1090 <strdup@plt>
mov    %rax,%rbp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    $0x2c,%esi
call   1040 <split_and_get@plt>
test   %rax,%rax
mov    %rbp,0x10(%rsp)
je     1245 <globs_to_regex+0xd5>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
xor    %r13d,%r13d
jmp    11e0 <globs_to_regex+0x70>
nop
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    $0x2c,%esi
mov    %r15,%rdx
call   1040 <split_and_get@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1248 <globs_to_regex+0xd8>
mov    %rbx,%rdi
call   1060 <glob_to_regex@plt>
test   %rax,%rax
je     1248 <globs_to_regex+0xd8>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r13,1),%rsi
add    $0x4,%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c0 <globs_to_regex+0x50>
mov    %rax,%r12
mov    %rax,%rdi
add    %r13,%rdi
test   %r13,%r13
lea    0xde7(%rip),%rdx
lea    0xddf(%rip),%rax
cmove  %rax,%rdx
lea    0xdce(%rip),%rsi
mov    %rbp,%rcx
call   1080 <sprintf@plt>
add    %rax,%r13
mov    %r12,%r14
jmp    11c0 <globs_to_regex+0x50>
xor    %r14d,%r14d
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s(%s)\00"
@.str.1 = [2 x i8] c"|\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @globs_to_regex(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %3
store i64 0, i64* %4
%11 = load i8*, i8** %2
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
store i8* %13, i8** %6
store i8* null, i8** %7
br label %14
14: ; preds = %51, %1
%15 = load i8*, i8** %6
%16 = call i8* @split_and_get(i8* %15, i8 signext 44, i8** %7)
store i8* %16, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %54
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = call i8* @glob_to_regex(i8* %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
br label %54
24: ; preds = %18
%25 = load i64, i64* %4
%26 = add i64 %25, 1
%27 = add i64 %26, 1
%28 = load i8*, i8** %9
%29 = call i64 @strlen(i8* %28)
%30 = add i64 %27, %29
%31 = add i64 %30, 1
store i64 %31, i64* %10
%32 = load i8*, i8** %3
%33 = load i64, i64* %10
%34 = add i64 %33, 1
%35 = call i8* @realloc(i8* %32, i64 %34)
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %51
38: ; preds = %24
%39 = load i8*, i8** %8
store i8* %39, i8** %3
%40 = load i8*, i8** %3
%41 = load i64, i64* %4
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i64, i64* %4
%44 = icmp ne i64 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%47 = load i8*, i8** %9
%48 = call i64 @sprintf(i8* %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %46, i8* %47)
%49 = load i64, i64* %4
%50 = add i64 %49, %48
store i64 %50, i64* %4
br label %51
51: ; preds = %38, %24
%52 = load i8*, i8** %9
%53 = call i32 @free(i8* %52)
br label %14
54: ; preds = %23, %14
%55 = load i8*, i8** %5
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s(%s)\00"
@.str.1 = [2 x i8] c"|\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @globs_to_regex(i8* nocapture %0) {
%2 = alloca i8*
%3 = tail call i8* @strdup(i8* %0)
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = call i8* @split_and_get(i8* %3, i8 signext 44, i8** %2)
%6 = icmp eq i8* %5, null
br i1 %6, label %31, label %7
7: ; preds = %1, %25
%8 = phi i8* [ %29, %25 ], [ %5, %1 ]
%9 = phi i8* [ %27, %25 ], [ null, %1 ]
%10 = phi i64 [ %26, %25 ], [ 0, %1 ]
%11 = call i8* @glob_to_regex(i8* %8)
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %7
%14 = call i64 @strlen(i8* %11)
%15 = add i64 %10, 4
%16 = add i64 %15, %14
%17 = call i8* @realloc(i8* %9, i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %25, label %19
19: ; preds = %13
%20 = getelementptr inbounds i8, i8* %17, i64 %10
%21 = icmp eq i64 %10, 0
%22 = select i1 %21, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%23 = call i64 @sprintf(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %11)
%24 = add i64 %23, %10
br label %25
25: ; preds = %13, %19
%26 = phi i64 [ %24, %19 ], [ %10, %13 ]
%27 = phi i8* [ %17, %19 ], [ %9, %13 ]
%28 = call i32 @free(i8* %11)
%29 = call i8* @split_and_get(i8* %8, i8 signext 44, i8** %2)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %7
31: ; preds = %25, %7, %1
%32 = phi i8* [ null, %1 ], [ %9, %7 ], [ %27, %25 ]
%33 = call i32 @free(i8* %3)
ret i8* %32
}