#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*/ free(char *);
char *malloc(unsigned int);
int /*<<< orphan*/ snprintf(char *, unsigned int, char const *, char *);
scalar_t__ strcmp(char const *, char *);
char *strdup(char const *);
int strlen(char const *);
char *strstr(char const *, char *);
char *glob_subst(const char *format, char *own_s) {
  if (strcmp(format, "%s") == 0) {
    return own_s;
  } else {
    char *token = strstr(format, "%s");
    if (token) {
      unsigned l = strlen(format) + strlen(own_s) - 1;
      char *n = malloc(l);
      snprintf(n, l, format, own_s);
      free(own_s);
      return n;
    } else {
      free(own_s);
      return strdup(format);
    }
  }
}


x86 assembly (clang, -O0):
<glob_subst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11a7 <glob_subst+0x37>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <glob_subst+0xc4>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     121e <glob_subst+0xae>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <glob_subst+0xc4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glob_subst>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe7c(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11e1 <glob_subst+0x71>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
test   %rax,%rax
je     11ef <glob_subst+0x7f>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%ebp
add    $0xffffffff,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strdup@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i8* @glob_subst(i8* %0, i8* %1) {
%3 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %19, label %5
5: ; preds = %2
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %16, label %8
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i32 @strlen(i8* %1)
%11 = add i32 %9, -1
%12 = add i32 %11, %10
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @snprintf(i8* %13, i32 %12, i8* %0, i8* %1)
%15 = tail call i32 @free(i8* %1)
br label %19
16: ; preds = %5
%17 = tail call i32 @free(i8* %1)
%18 = tail call i8* @strdup(i8* %0)
br label %19
19: ; preds = %8, %16, %2
%20 = phi i8* [ %1, %2 ], [ %13, %8 ], [ %18, %16 ]
ret i8* %20
}