#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 dstr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ dstr_copy(struct dstr *, char const *);
int /*<<< orphan*/ dstr_replace(struct dstr *, char *, char const *);
void dstr_safe_printf(struct dstr *dst, const char *format, const char *val1,
                      const char *val2, const char *val3, const char *val4) {
  dstr_copy(dst, format);
  if (val1)
    dstr_replace(dst, "$1", val1);
  if (val2)
    dstr_replace(dst, "$2", val2);
  if (val3)
    dstr_replace(dst, "$3", val3);
  if (val4)
    dstr_replace(dst, "$4", val4);
}


x86 assembly (clang, -O0):
<dstr_safe_printf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <dstr_copy@plt>
cmpq   $0x0,-0x18(%rbp)
je     116c <dstr_safe_printf+0x4c>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe99(%rip),%rsi
call   1030 <dstr_replace@plt>
cmpq   $0x0,-0x20(%rbp)
je     118b <dstr_safe_printf+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe7d(%rip),%rsi
call   1030 <dstr_replace@plt>
cmpq   $0x0,-0x28(%rbp)
je     11aa <dstr_safe_printf+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1030 <dstr_replace@plt>
cmpq   $0x0,-0x30(%rbp)
je     11c9 <dstr_safe_printf+0xa9>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe45(%rip),%rsi
call   1030 <dstr_replace@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dstr_safe_printf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %rdi,%r13
call   1040 <dstr_copy@plt>
test   %rbx,%rbx
je     1154 <dstr_safe_printf+0x34>
lea    0xeb7(%rip),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
call   1030 <dstr_replace@plt>
test   %r12,%r12
je     116b <dstr_safe_printf+0x4b>
lea    0xea3(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
call   1030 <dstr_replace@plt>
test   %r15,%r15
je     1182 <dstr_safe_printf+0x62>
lea    0xe8f(%rip),%rsi
mov    %r13,%rdi
mov    %r15,%rdx
call   1030 <dstr_replace@plt>
test   %r14,%r14
je     11a2 <dstr_safe_printf+0x82>
lea    0xe7b(%rip),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <dstr_replace@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dstr = type { i32 }
@.str = [3 x i8] c"$1\00"
@.str.1 = [3 x i8] c"$2\00"
@.str.2 = [3 x i8] c"$3\00"
@.str.3 = [3 x i8] c"$4\00"
define void @dstr_safe_printf(%struct.dstr* %0, i8* %1, i8* %2, i8* %3, i8* %4, i8* %5) {
%7 = alloca %struct.dstr*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store %struct.dstr* %0, %struct.dstr** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i8* %5, i8** %12
%13 = load %struct.dstr*, %struct.dstr** %7
%14 = load i8*, i8** %8
%15 = call i32 @dstr_copy(%struct.dstr* %13, i8* %14)
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %6
%19 = load %struct.dstr*, %struct.dstr** %7
%20 = load i8*, i8** %9
%21 = call i32 @dstr_replace(%struct.dstr* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %6
%23 = load i8*, i8** %10
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load %struct.dstr*, %struct.dstr** %7
%27 = load i8*, i8** %10
%28 = call i32 @dstr_replace(%struct.dstr* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %22
%30 = load i8*, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load %struct.dstr*, %struct.dstr** %7
%34 = load i8*, i8** %11
%35 = call i32 @dstr_replace(%struct.dstr* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %29
%37 = load i8*, i8** %12
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load %struct.dstr*, %struct.dstr** %7
%41 = load i8*, i8** %12
%42 = call i32 @dstr_replace(%struct.dstr* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %39, %36
ret void
}

LLVM IR (-O2):
%struct.dstr = type { i32 }
@.str = [3 x i8] c"$1\00"
@.str.1 = [3 x i8] c"$2\00"
@.str.2 = [3 x i8] c"$3\00"
@.str.3 = [3 x i8] c"$4\00"
define void @dstr_safe_printf(%struct.dstr* %0, i8* %1, i8* %2, i8* %3, i8* %4, i8* %5) {
%7 = tail call i32 @dstr_copy(%struct.dstr* %0, i8* %1)
%8 = icmp eq i8* %2, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @dstr_replace(%struct.dstr* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %11
11: ; preds = %9, %6
%12 = icmp eq i8* %3, null
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @dstr_replace(%struct.dstr* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %15
15: ; preds = %13, %11
%16 = icmp eq i8* %4, null
br i1 %16, label %19, label %17
17: ; preds = %15
%18 = tail call i32 @dstr_replace(%struct.dstr* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %19
19: ; preds = %17, %15
%20 = icmp eq i8* %5, null
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = tail call i32 @dstr_replace(%struct.dstr* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %23
23: ; preds = %21, %19
ret void
}