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


x86 assembly (clang, -O0):
<append_pid_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe78(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c3 <append_pid_filter+0x63>
mov    -0x24(%rbp),%edi
call   1050 <malloc_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe5c(%rip),%rsi
call   1080 <sprintf@plt>
jmp    1223 <append_pid_filter+0xc3>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    -0x28(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1205 <append_pid_filter+0xa5>
lea    0xe0b(%rip),%rdi
call   1070 <die@plt>
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdf5(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_pid_filter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe88(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
mov    %r15,%rcx
mov    %r14d,%r8d
call   1040 <snprintf@plt>
mov    %eax,%ebx
test   %rbp,%rbp
je     11cb <append_pid_filter+0x6b>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    (%rbx,%r12,2),%esi
add    $0x1,%esi
mov    %rbp,%rdi
call   1060 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11bc <append_pid_filter+0x5c>
lea    0xe54(%rip),%rdi
call   1070 <die@plt>
movslq %r12d,%rdi
add    %rbp,%rdi
lea    0xe4a(%rip),%rsi
jmp    11df <append_pid_filter+0x7f>
mov    %ebx,%edi
call   1050 <malloc_or_die@plt>
mov    %rax,%rbp
lea    0xe39(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1080 <sprintf@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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