#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 */
struct htrace_log {
  int dummy;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ pid_str;
typedef int /*<<< orphan*/ ip_str;

/* Variables and functions */
scalar_t__ asprintf(char **, char *, char *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ get_best_ip(struct htrace_log *, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ htrace_log(struct htrace_log *, char *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, long long);
scalar_t__ strcmp(char const *, char *);
int handle_process_subst_var(struct htrace_log *lg, char **out, const char *var,
                             const char *tname) {
  char *nout = NULL;
  if (strcmp(var, "%{tname}") == 0) {
    if (asprintf(&nout, "%s%s", *out, tname) < 0) {
      htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var);
      return 0;
    }
    free(*out);
    *out = nout;
  } else if (strcmp(var, "%{ip}") == 0) {
    char ip_str[256];
    get_best_ip(lg, ip_str, sizeof(ip_str));
    if (asprintf(&nout, "%s%s", *out, ip_str) < 0) {
      htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var);
      return 0;
    }
    free(*out);
    *out = nout;
  } else if (strcmp(var, "%{pid}") == 0) {
    char pid_str[64];
    pid_t pid = getpid();
    snprintf(pid_str, sizeof(pid_str), "%lld", (long long)pid);
    if (asprintf(&nout, "%s%s", *out, pid_str) < 0) {
      htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var);
      return 0;
    }
    free(*out);
    *out = nout;
  } else {
    htrace_log(lg,
               "handle_process_subst_var(var=%s): unknown process "
               "ID substitution variable.\n",
               var);
  }
  return 1;
}


x86 assembly (clang, -O0):
<handle_process_subst_var>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    120e <handle_process_subst_var+0x9e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    11f2 <handle_process_subst_var+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1090 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1371 <handle_process_subst_var+0x201>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    136a <handle_process_subst_var+0x1fa>
mov    -0x20(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12a1 <handle_process_subst_var+0x131>
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0x100,%edx
call   1080 <get_best_ip@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    -0x130(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xdb3(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    1285 <handle_process_subst_var+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd9a(%rip),%rsi
call   1090 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1371 <handle_process_subst_var+0x201>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1365 <handle_process_subst_var+0x1f5>
mov    -0x20(%rbp),%rdi
lea    0xd8e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    134c <handle_process_subst_var+0x1dc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x178(%rbp)
lea    -0x170(%rbp),%rdi
mov    -0x178(%rbp),%rcx
mov    $0x40,%esi
lea    0xd5e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    -0x170(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xd08(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0x0,%rax
jge    1330 <handle_process_subst_var+0x1c0>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xcef(%rip),%rsi
call   1090 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1371 <handle_process_subst_var+0x201>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1360 <handle_process_subst_var+0x1f0>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xceb(%rip),%rsi
call   1090 <htrace_log@plt>
jmp    1365 <handle_process_subst_var+0x1f5>
jmp    136a <handle_process_subst_var+0x1fa>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_process_subst_var>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
lea    0xe65(%rip),%rsi
mov    %rdx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f9 <handle_process_subst_var+0x89>
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e4 <handle_process_subst_var+0x74>
lea    0xe77(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1242 <handle_process_subst_var+0xd2>
lea    0xe6f(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1090 <htrace_log@plt>
jmp    1225 <handle_process_subst_var+0xb5>
lea    0x10(%rsp),%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1080 <get_best_ip@plt>
mov    (%r14),%rdx
lea    0xe06(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbp,%rcx
call   1070 <asprintf@plt>
test   %rax,%rax
js     122c <handle_process_subst_var+0xbc>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
mov    $0x1,%ebp
jmp    1295 <handle_process_subst_var+0x125>
lea    0xddb(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1090 <htrace_log@plt>
xor    %ebp,%ebp
jmp    1295 <handle_process_subst_var+0x125>
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdef(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x40,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    (%r14),%rdx
lea    0xd98(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r12,%rcx
call   1070 <asprintf@plt>
test   %rax,%rax
jns    1215 <handle_process_subst_var+0xa5>
lea    0xd84(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1090 <htrace_log@plt>
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.htrace_log = type { i32 }
@.str = [9 x i8] c"%{tname}\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [38 x i8] c"handle_process_subst_var(var=%s): OOM\00"
@.str.3 = [6 x i8] c"%{ip}\00"
@.str.4 = [7 x i8] c"%{pid}\00"
@.str.5 = [5 x i8] c"%lld\00"
@.str.6 = [77 x i8] c"handle_process_subst_var(var=%s): unknown process ID substitution variable.\0A\00"
define i32 @handle_process_subst_var(%struct.htrace_log* %0, i8** %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.htrace_log*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [256 x i8]
%12 = alloca [64 x i8]
%13 = alloca i64
store %struct.htrace_log* %0, %struct.htrace_log** %6
store i8** %1, i8*** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* null, i8** %10
%14 = load i8*, i8** %8
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %4
%18 = load i8**, i8*** %7
%19 = load i8*, i8** %18
%20 = load i8*, i8** %9
%21 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load %struct.htrace_log*, %struct.htrace_log** %6
%25 = load i8*, i8** %8
%26 = call i32 @htrace_log(%struct.htrace_log* %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 0, i32* %5
br label %87
27: ; preds = %17
%28 = load i8**, i8*** %7
%29 = load i8*, i8** %28
%30 = call i32 @free(i8* %29)
%31 = load i8*, i8** %10
%32 = load i8**, i8*** %7
store i8* %31, i8** %32
br label %86
33: ; preds = %4
%34 = load i8*, i8** %8
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %56
37: ; preds = %33
%38 = load %struct.htrace_log*, %struct.htrace_log** %6
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%40 = call i32 @get_best_ip(%struct.htrace_log* %38, i8* %39, i32 256)
%41 = load i8**, i8*** %7
%42 = load i8*, i8** %41
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%44 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %37
%47 = load %struct.htrace_log*, %struct.htrace_log** %6
%48 = load i8*, i8** %8
%49 = call i32 @htrace_log(%struct.htrace_log* %47, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %48)
store i32 0, i32* %5
br label %87
50: ; preds = %37
%51 = load i8**, i8*** %7
%52 = load i8*, i8** %51
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %10
%55 = load i8**, i8*** %7
store i8* %54, i8** %55
br label %85
56: ; preds = %33
%57 = load i8*, i8** %8
%58 = call i64 @strcmp(i8* %57, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %80
60: ; preds = %56
%61 = call i64 (...) @getpid()
store i64 %61, i64* %13
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%63 = load i64, i64* %13
%64 = call i32 @snprintf(i8* %62, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 %63)
%65 = load i8**, i8*** %7
%66 = load i8*, i8** %65
%67 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%68 = call i64 @asprintf(i8** %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %66, i8* %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %60
%71 = load %struct.htrace_log*, %struct.htrace_log** %6
%72 = load i8*, i8** %8
%73 = call i32 @htrace_log(%struct.htrace_log* %71, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %72)
store i32 0, i32* %5
br label %87
74: ; preds = %60
%75 = load i8**, i8*** %7
%76 = load i8*, i8** %75
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %10
%79 = load i8**, i8*** %7
store i8* %78, i8** %79
br label %84
80: ; preds = %56
%81 = load %struct.htrace_log*, %struct.htrace_log** %6
%82 = load i8*, i8** %8
%83 = call i32 @htrace_log(%struct.htrace_log* %81, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.6, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %80, %74
br label %85
85: ; preds = %84, %50
br label %86
86: ; preds = %85, %27
store i32 1, i32* %5
br label %87
87: ; preds = %86, %70, %46, %23
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
%struct.htrace_log = type { i32 }
@.str = [9 x i8] c"%{tname}\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [38 x i8] c"handle_process_subst_var(var=%s): OOM\00"
@.str.3 = [6 x i8] c"%{ip}\00"
@.str.4 = [7 x i8] c"%{pid}\00"
@.str.5 = [5 x i8] c"%lld\00"
@.str.6 = [77 x i8] c"handle_process_subst_var(var=%s): unknown process ID substitution variable.\0A\00"
define i32 @handle_process_subst_var(%struct.htrace_log* %0, i8** nocapture %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [64 x i8]
%8 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%9 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %4
%12 = load i8*, i8** %1
%13 = call i64 @asprintf(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %54
17: ; preds = %11
%18 = load i8*, i8** %1
%19 = call i32 @free(i8* %18)
%20 = load i8*, i8** %5
store i8* %20, i8** %1
br label %54
21: ; preds = %4
%22 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%26 = call i32 @get_best_ip(%struct.htrace_log* %0, i8* %25, i32 256)
%27 = load i8*, i8** %1
%28 = call i64 @asprintf(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %25)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %54
32: ; preds = %24
%33 = load i8*, i8** %1
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %5
store i8* %35, i8** %1
br label %54
36: ; preds = %21
%37 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%41 = tail call i64 (...) @getpid()
%42 = call i32 @snprintf(i8* %40, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 %41)
%43 = load i8*, i8** %1
%44 = call i64 @asprintf(i8** %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %43, i8* %40)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %54
48: ; preds = %39
%49 = load i8*, i8** %1
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %5
store i8* %51, i8** %1
br label %54
52: ; preds = %36
%53 = tail call i32 @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.6, i64 0, i64 0), i8* %2)
br label %54
54: ; preds = %17, %52, %32, %48, %46, %30, %15
%55 = phi i32 [ 0, %15 ], [ 0, %30 ], [ 0, %46 ], [ 1, %48 ], [ 1, %32 ], [ 1, %52 ], [ 1, %17 ]
ret i32 %55
}