#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 *global_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 *globals_to_regex(const char globals[]) {
  char *final_regex = NULL;
  size_t final_regex_len = 0UL;
  char *globals_copy = strdup(globals);
  char *global = globals_copy, *state = NULL;
  while ((global = split_and_get(global, ',', &state)) != NULL) {
    void *p;
    char *regex;
    size_t new_len;
    regex = global_to_regex(global);
    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(globals_copy);
  return final_regex;
}


x86 assembly (clang, -O0):
<globals_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     1271 <globals_to_regex+0x101>
mov    -0x28(%rbp),%rdi
call   1060 <global_to_regex@plt>
mov    %rax,-0x40(%rbp)
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     1263 <globals_to_regex+0xf3>
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xdca(%rip),%rdx
lea    0xdc4(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
mov    -0x40(%rbp),%rcx
lea    0xdaa(%rip),%rsi
call   1080 <sprintf@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
jmp    11a9 <globals_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):
<globals_to_regex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
call   1090 <strdup@plt>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
mov    $0x2c,%esi
call   1040 <split_and_get@plt>
test   %rax,%rax
je     123d <globals_to_regex+0xcd>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0x8(%rsp),%r15
xor    %r12d,%r12d
jmp    11e0 <globals_to_regex+0x70>
nopl   0x0(%rax)
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     1240 <globals_to_regex+0xd0>
mov    %rbx,%rdi
call   1060 <global_to_regex@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x4,%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c0 <globals_to_regex+0x50>
mov    %rax,%r13
mov    %rax,%rdi
add    %r12,%rdi
test   %r12,%r12
lea    0xdec(%rip),%rdx
lea    0xde4(%rip),%rax
cmove  %rax,%rdx
lea    0xdd3(%rip),%rsi
mov    %rbp,%rcx
call   1080 <sprintf@plt>
add    %rax,%r12
mov    %r13,%r14
jmp    11c0 <globals_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* @globals_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 = %47, %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 %50
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = call i8* @global_to_regex(i8* %19)
store i8* %20, i8** %9
%21 = load i64, i64* %4
%22 = add i64 %21, 1
%23 = add i64 %22, 1
%24 = load i8*, i8** %9
%25 = call i64 @strlen(i8* %24)
%26 = add i64 %23, %25
%27 = add i64 %26, 1
store i64 %27, i64* %10
%28 = load i8*, i8** %3
%29 = load i64, i64* %10
%30 = add i64 %29, 1
%31 = call i8* @realloc(i8* %28, i64 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %47
34: ; preds = %18
%35 = load i8*, i8** %8
store i8* %35, i8** %3
%36 = load i8*, i8** %3
%37 = load i64, i64* %4
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i64, i64* %4
%40 = icmp ne i64 %39, 0
%41 = zext i1 %40 to i64
%42 = select i1 %40, 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)
%43 = load i8*, i8** %9
%44 = call i64 @sprintf(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %42, i8* %43)
%45 = load i64, i64* %4
%46 = add i64 %45, %44
store i64 %46, i64* %4
br label %47
47: ; preds = %34, %18
%48 = load i8*, i8** %9
%49 = call i32 @free(i8* %48)
br label %14
50: ; preds = %14
%51 = load i8*, i8** %5
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %3
ret i8* %53
}

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* @globals_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 %29, label %7
7: ; preds = %1, %23
%8 = phi i8* [ %27, %23 ], [ %5, %1 ]
%9 = phi i8* [ %25, %23 ], [ null, %1 ]
%10 = phi i64 [ %24, %23 ], [ 0, %1 ]
%11 = call i8* @global_to_regex(i8* %8)
%12 = call i64 @strlen(i8* dereferenceable(1) %11)
%13 = add i64 %10, 4
%14 = add i64 %13, %12
%15 = call i8* @realloc(i8* %9, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %7
%18 = getelementptr inbounds i8, i8* %15, i64 %10
%19 = icmp eq i64 %10, 0
%20 = select i1 %19, 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)
%21 = call i64 @sprintf(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %11)
%22 = add i64 %21, %10
br label %23
23: ; preds = %17, %7
%24 = phi i64 [ %22, %17 ], [ %10, %7 ]
%25 = phi i8* [ %15, %17 ], [ %9, %7 ]
%26 = call i32 @free(i8* %11)
%27 = call i8* @split_and_get(i8* %8, i8 signext 44, i8** %2)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %7
29: ; preds = %23, %1
%30 = phi i8* [ null, %1 ], [ %25, %23 ]
%31 = call i32 @free(i8* %3)
ret i8* %30
}