#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*/ die(char *);
char *malloc_or_die(int);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *, ...);
int strlen(char *);
char *append_pid_filter(char *curr_filter, char *pid) {
  char *filter;
  int len;
  len = strlen(".*:"
               "(common_pid=="
               ""
               ")||(pid=="
               ""
               ")||(next_pid=="
               ""
               ")") +
        strlen(pid) * 3 + 1;
  if (!curr_filter) {
    filter = malloc_or_die(len);
    sprintf(filter,
            ".*:"
            "(common_pid=="
            "%s"
            ")||(pid=="
            "%s"
            ")||(next_pid=="
            "%s"
            ")",
            pid, pid, pid);
  } else {
    len += strlen(curr_filter);
    filter = realloc(curr_filter, len);
    if (!filter)
      die("realloc");
    sprintf(filter,
            "%s||"
            "(common_pid=="
            "%s"
            ")||(pid=="
            "%s"
            ")||(next_pid=="
            "%s"
            ")",
            filter, pid, pid, pid);
  }
  return filter;
}


x86 assembly (clang, -O0):
<append_pid_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe99(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
imul   $0x3,%ecx,%ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c2 <append_pid_filter+0x72>
mov    -0x1c(%rbp),%edi
call   1040 <malloc_or_die@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    121a <append_pid_filter+0xca>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f8 <append_pid_filter+0xa8>
lea    0xe65(%rip),%rdi
call   1060 <die@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x10(%rbp),%r9
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_pid_filter>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9f(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rax,2),%eax
add    %eax,%ebp
add    $0x1,%ebp
test   %rbx,%rbx
je     11c4 <append_pid_filter+0x74>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %eax,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a5 <append_pid_filter+0x55>
lea    0xeb8(%rip),%rdi
call   1060 <die@plt>
lea    0xeb4(%rip),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %r14,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11e8 <append_pid_filter+0x98>
mov    %ebp,%edi
call   1040 <malloc_or_die@plt>
mov    %rax,%rbx
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r14,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c".*:(common_pid==)||(pid==)||(next_pid==)\00"
@.str.1 = [47 x i8] c".*:(common_pid==%s)||(pid==%s)||(next_pid==%s)\00"
@.str.2 = [8 x i8] c"realloc\00"
@.str.3 = [48 x i8] c"%s||(common_pid==%s)||(pid==%s)||(next_pid==%s)\00"
define i8* @append_pid_filter(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = call i32 @strlen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = mul nsw i32 %9, 3
%11 = add nsw i32 %7, %10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %23, label %15
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i8* @malloc_or_die(i32 %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = load i8*, i8** %4
%21 = load i8*, i8** %4
%22 = call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %20, i8* %21)
br label %42
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
%26 = load i32, i32* %6
%27 = add nsw i32 %26, %25
store i32 %27, i32* %6
%28 = load i8*, i8** %3
%29 = load i32, i32* %6
%30 = call i8* @realloc(i8* %28, i32 %29)
store i8* %30, i8** %5
%31 = load i8*, i8** %5
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %23
%36 = load i8*, i8** %5
%37 = load i8*, i8** %5
%38 = load i8*, i8** %4
%39 = load i8*, i8** %4
%40 = load i8*, i8** %4
%41 = call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %37, i8* %38, i8* %39, i8* %40)
br label %42
42: ; preds = %35, %15
%43 = load i8*, i8** %5
ret i8* %43
}

LLVM IR (-O2):
@.str = [41 x i8] c".*:(common_pid==)||(pid==)||(next_pid==)\00"
@.str.1 = [47 x i8] c".*:(common_pid==%s)||(pid==%s)||(next_pid==%s)\00"
@.str.2 = [8 x i8] c"realloc\00"
@.str.3 = [48 x i8] c"%s||(common_pid==%s)||(pid==%s)||(next_pid==%s)\00"
define i8* @append_pid_filter(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @strlen(i8* %1)
%5 = mul nsw i32 %4, 3
%6 = add i32 %3, 1
%7 = add i32 %6, %5
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i8* @malloc_or_die(i32 %7)
%11 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %1, i8* %1)
br label %21
12: ; preds = %2
%13 = tail call i32 @strlen(i8* %0)
%14 = add nsw i32 %13, %7
%15 = tail call i8* @realloc(i8* %0, i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %15, i8* %1, i8* %1, i8* %1)
br label %21
21: ; preds = %19, %9
%22 = phi i8* [ %15, %19 ], [ %10, %9 ]
ret i8* %22
}