#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 */
typedef int /*<<< orphan*/ va_list;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
size_t strlen(char *);
int vsnprintf(char *, size_t, char *, int /*<<< orphan*/);
int ext_logger_sprintf(char *buffer, size_t buffer_size, const char *format,
                       va_list args) {
  char *n_format = strdup(format);
  int i = 0;
  size_t f_size = strlen(n_format);
  while (i < f_size) {
    if (n_format[i] == '%') {
      i++;
      if (n_format[i] == 'z') {
        n_format[i] = 'l';
      }
    }
    i++;
  }
  int n = vsnprintf(buffer, buffer_size, n_format, args);
  free(n_format);
  return n;
}


x86 assembly (clang, -O0):
<ext_logger_sprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    11d8 <ext_logger_sprintf+0x98>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x25,%eax
jne    11ca <ext_logger_sprintf+0x8a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x7a,%eax
jne    11c5 <ext_logger_sprintf+0x85>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x6c,(%rax,%rcx,1)
jmp    11ca <ext_logger_sprintf+0x8a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1178 <ext_logger_sprintf+0x38>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ext_logger_sprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1060 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     1195 <ext_logger_sprintf+0x55>
xor    %edx,%edx
xor    %ecx,%ecx
jmp    117b <ext_logger_sprintf+0x3b>
xchg   %ax,%ax
add    $0x1,%ecx
movslq %ecx,%rdx
cmp    %rdx,%rax
jbe    1195 <ext_logger_sprintf+0x55>
cmpb   $0x25,(%rbx,%rdx,1)
jne    1170 <ext_logger_sprintf+0x30>
movslq %ecx,%rdx
lea    0x1(%rdx),%rcx
cmpb   $0x7a,0x1(%rbx,%rdx,1)
jne    1170 <ext_logger_sprintf+0x30>
movb   $0x6c,(%rbx,%rcx,1)
jmp    1170 <ext_logger_sprintf+0x30>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ext_logger_sprintf(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i8*, i8** %7
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %9
store i32 0, i32* %10
%15 = load i8*, i8** %9
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %11
br label %17
17: ; preds = %46, %4
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = load i64, i64* %11
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %49
22: ; preds = %17
%23 = load i8*, i8** %9
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 37
br i1 %29, label %30, label %46
30: ; preds = %22
%31 = load i32, i32* %10
%32 = add nsw i32 %31, 1
store i32 %32, i32* %10
%33 = load i8*, i8** %9
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 122
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = load i8*, i8** %9
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 108, i8* %44
br label %45
45: ; preds = %40, %30
br label %46
46: ; preds = %45, %22
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %17
49: ; preds = %17
%50 = load i8*, i8** %5
%51 = load i64, i64* %6
%52 = load i8*, i8** %9
%53 = load i32, i32* %8
%54 = call i32 @vsnprintf(i8* %50, i64 %51, i8* %52, i32 %53)
store i32 %54, i32* %12
%55 = load i8*, i8** %9
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %12
ret i32 %57
}

LLVM IR (-O2):
define i32 @ext_logger_sprintf(i8* nocapture %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i8* @strdup(i8* %2)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %4, %21
%9 = phi i64 [ %24, %21 ], [ 0, %4 ]
%10 = phi i32 [ %23, %21 ], [ 0, %4 ]
%11 = getelementptr inbounds i8, i8* %5, i64 %9
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 37
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = add nsw i32 %10, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %5, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 122
br i1 %19, label %20, label %21
20: ; preds = %14
store i8 108, i8* %17
br label %21
21: ; preds = %14, %20, %8
%22 = phi i32 [ %15, %20 ], [ %15, %14 ], [ %10, %8 ]
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = icmp ugt i64 %6, %24
br i1 %25, label %8, label %26
26: ; preds = %21, %4
%27 = tail call i32 @vsnprintf(i8* %0, i64 %1, i8* %5, i32 %3)
%28 = tail call i32 @free(i8* %5)
ret i32 %27
}