#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*/ strcat(char *, char const *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
void flat_format_output(const char *arg, char *string, char *format) {
  char *pos1, *pos2, buffer[1024];
  pos1 = &format[0];
  strncpy(buffer, string, 1024);
  string[0] = '\0';
  while ((pos2 = strstr(pos1, "%")) != NULL) {
    strncat(string, pos1, (size_t)(pos2 - pos1));
    if (*(pos2 + 1) == '1')
      strcat(string, arg);
    else if (*(pos2 + 1) == '2')
      strcat(string, buffer);
    else if (*(pos2 + 1) == '%')
      strcat(string, "%");
    pos1 = pos2 + 2;
  }
  strcat(string, pos1);
}


x86 assembly (clang, -O0):
<flat_format_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1236 <flat_format_output+0xf6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
call   1040 <strncat@plt>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x31,%eax
jne    11d4 <flat_format_output+0x94>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcat@plt>
jmp    1225 <flat_format_output+0xe5>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x32,%eax
jne    11fa <flat_format_output+0xba>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1050 <strcat@plt>
jmp    1220 <flat_format_output+0xe0>
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x25,%eax
jne    121b <flat_format_output+0xdb>
mov    -0x10(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   1050 <strcat@plt>
jmp    1220 <flat_format_output+0xe0>
jmp    1225 <flat_format_output+0xe5>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    117b <flat_format_output+0x3b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flat_format_output>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x0(%r13)
mov    %rbp,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11e8 <flat_format_output+0xa8>
mov    %rax,%rbx
lea    0xe78(%rip),%r15
mov    %rsp,%r12
jmp    11b7 <flat_format_output+0x77>
nopl   (%rax)
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rbp
add    $0x2,%rbp
mov    %rbp,%rdi
mov    $0x25,%esi
call   1040 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e8 <flat_format_output+0xa8>
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <strncat@plt>
movzbl 0x1(%rbx),%eax
cmp    $0x25,%al
je     11e0 <flat_format_output+0xa0>
cmp    $0x32,%al
je     1190 <flat_format_output+0x50>
cmp    $0x31,%al
jne    119b <flat_format_output+0x5b>
mov    %r13,%rdi
mov    %r14,%rsi
jmp    1196 <flat_format_output+0x56>
mov    %r13,%rdi
mov    %r15,%rsi
jmp    1196 <flat_format_output+0x56>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1060 <strcat@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"%\00"
define void @flat_format_output(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8* %11, i8** %7
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 @strncpy(i8* %12, i8* %13, i32 1024)
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
br label %17
17: ; preds = %60, %3
%18 = load i8*, i8** %7
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %63
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = load i8*, i8** %7
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = call i32 @strncat(i8* %22, i8* %23, i64 %28)
%30 = load i8*, i8** %8
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 49
br i1 %34, label %35, label %39
35: ; preds = %21
%36 = load i8*, i8** %5
%37 = load i8*, i8** %4
%38 = call i32 @strcat(i8* %36, i8* %37)
br label %60
39: ; preds = %21
%40 = load i8*, i8** %8
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 50
br i1 %44, label %45, label %49
45: ; preds = %39
%46 = load i8*, i8** %5
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = call i32 @strcat(i8* %46, i8* %47)
br label %59
49: ; preds = %39
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i64 1
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 37
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i8*, i8** %5
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %58
58: ; preds = %55, %49
br label %59
59: ; preds = %58, %45
br label %60
60: ; preds = %59, %35
%61 = load i8*, i8** %8
%62 = getelementptr inbounds i8, i8* %61, i64 2
store i8* %62, i8** %7
br label %17
63: ; preds = %17
%64 = load i8*, i8** %5
%65 = load i8*, i8** %7
%66 = call i32 @strcat(i8* %64, i8* %65)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"%\00"
define void @flat_format_output(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %1, i32 1024)
store i8 0, i8* %1
%7 = call i8* @strchr(i8* dereferenceable(1) %2, i32 37)
%8 = icmp eq i8* %7, null
br i1 %8, label %28, label %9
9: ; preds = %3, %24
%10 = phi i8* [ %26, %24 ], [ %7, %3 ]
%11 = phi i8* [ %25, %24 ], [ %2, %3 ]
%12 = ptrtoint i8* %10 to i64
%13 = ptrtoint i8* %11 to i64
%14 = sub i64 %12, %13
%15 = call i32 @strncat(i8* %1, i8* %11, i64 %14)
%16 = getelementptr inbounds i8, i8* %10, i64 1
%17 = load i8, i8* %16
switch i8 %17, label %24 [
i8 49, label %18
i8 50, label %20
i8 37, label %22
]
18: ; preds = %9
%19 = call i32 @strcat(i8* %1, i8* %0)
br label %24
20: ; preds = %9
%21 = call i32 @strcat(i8* %1, i8* %5)
br label %24
22: ; preds = %9
%23 = call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %9, %20, %22, %18
%25 = getelementptr inbounds i8, i8* %10, i64 2
%26 = call i8* @strchr(i8* dereferenceable(1) %25, i32 37)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %9
28: ; preds = %24, %3
%29 = phi i8* [ %2, %3 ], [ %25, %24 ]
%30 = call i32 @strcat(i8* %1, i8* %29)
ret void
}