#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 const *, char const *, char *);
int strlen(char const *);
char *append_pid_filter(char *curr_filter, const char *events,
                        const char *field, char *pid) {
  char *filter;
  int len;
  len = strlen("(!=)&&") + strlen(field) + strlen(pid);
  if (!curr_filter) {
    filter = malloc_or_die(len);
    sprintf(filter, "%s:(%s==%s)", events, field, pid);
  } else {
    int indx = strlen(curr_filter);
    len += indx;
    filter = realloc(curr_filter, len + indx + 1);
    if (!filter)
      die("realloc");
    sprintf(filter, "%s||(%s==%s)", curr_filter, field, pid);
  }
  return filter;
}


x86 assembly (clang, -O0):
<append_pid_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe91(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d5 <append_pid_filter+0x85>
mov    -0x2c(%rbp),%edi
call   1040 <malloc_or_die@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe3c(%rip),%rsi
call   1070 <sprintf@plt>
jmp    1233 <append_pid_filter+0xe3>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    -0x30(%rbp),%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1217 <append_pid_filter+0xc7>
lea    0xe01(%rip),%rdi
call   1060 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xded(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_pid_filter>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
lea    0xe92(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    %ebx,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
add    %ebp,%ebx
test   %r12,%r12
je     11cb <append_pid_filter+0x7b>
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,2),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11bc <append_pid_filter+0x6c>
lea    0xe5c(%rip),%rdi
call   1060 <die@plt>
lea    0xe58(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
jmp    11e2 <append_pid_filter+0x92>
mov    %ebx,%edi
call   1040 <malloc_or_die@plt>
mov    %rax,%rbx
lea    0xe2b(%rip),%rsi
mov    %rax,%rdi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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