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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strcat(char *, char const *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncat(char *, char const *, int);
char *strstr(char const *, char *);
int /*<<< orphan*/ vsnprintf(char *, int, char *, int /*<<< orphan*/);
char *verrprintf(const char *fmt, va_list ap) {
  char *e = strerror(errno);
  const char *p, *q;
  char fmtbuf[1024];
  static char errbuf[1024];
  *fmtbuf = 0;
  for (p = fmt, q = strstr(p, "%m"); q; p = q, q = strstr(p, "%m")) {
    strncat(fmtbuf, p, q - p);
    strcat(fmtbuf, e);
    q += 2;
  }
  strcat(fmtbuf, p);
  vsnprintf(errbuf, sizeof(errbuf), fmtbuf, ap);
  return errbuf;
}


x86 assembly (clang, -O0):
<verrprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x430(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fc <verrprintf+0xac>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncat@plt>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
jmp    1197 <verrprintf+0x47>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcat@plt>
lea    -0x430(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0x2e53(%rip),%rdi
mov    $0x400,%esi
call   1030 <vsnprintf@plt>
lea    0x2e42(%rip),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verrprintf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%r15
movb   $0x0,(%rsp)
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11d4 <verrprintf+0x84>
mov    %rax,%rbp
mov    %rsp,%r13
lea    0xe63(%rip),%r12
nopl   (%rax)
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <strncat@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1050 <strcat@plt>
mov    %rbp,%rbx
add    $0x2,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11a0 <verrprintf+0x50>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
lea    0x2e87(%rip),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rbp,%rdx
mov    %r14d,%ecx
call   1030 <vsnprintf@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@verrprintf.errbuf = internal global [1024 x i8] zeroinitializer
@.str = [3 x i8] c"%m\00"
define i8* @verrprintf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
store i8* %10, i8** %5
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8 0, i8* %11
%12 = load i8*, i8** %3
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = call i8* @strstr(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %7
br label %15
15: ; preds = %33, %2
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = load i8*, i8** %6
%23 = ptrtoint i8* %21 to i64
%24 = ptrtoint i8* %22 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @strncat(i8* %19, i8* %20, i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%29 = load i8*, i8** %5
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8* %32, i8** %7
br label %33
33: ; preds = %18
%34 = load i8*, i8** %7
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = call i8* @strstr(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %7
br label %15
37: ; preds = %15
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = load i8*, i8** %6
%40 = call i32 @strcat(i8* %38, i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = load i32, i32* %4
%43 = call i32 @vsnprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @verrprintf.errbuf, i64 0, i64 0), i32 1024, i8* %41, i32 %42)
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @verrprintf.errbuf, i64 0, i64 0)
}

LLVM IR (-O2):
@errno = global i32 0
@verrprintf.errbuf = internal global [1024 x i8] zeroinitializer
@.str = [3 x i8] c"%m\00"
define i8* @verrprintf(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = load i32, i32* @errno
%5 = tail call i8* @strerror(i32 %4)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 0, i8* %6
%7 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %21, label %9
9: ; preds = %2, %9
%10 = phi i8* [ %19, %9 ], [ %7, %2 ]
%11 = phi i8* [ %18, %9 ], [ %0, %2 ]
%12 = ptrtoint i8* %10 to i64
%13 = ptrtoint i8* %11 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @strncat(i8* %6, i8* %11, i32 %15)
%17 = call i32 @strcat(i8* %6, i8* %5)
%18 = getelementptr inbounds i8, i8* %10, i64 2
%19 = call i8* @strstr(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %9
21: ; preds = %9, %2
%22 = phi i8* [ %0, %2 ], [ %18, %9 ]
%23 = call i32 @strcat(i8* %6, i8* %22)
%24 = call i32 @vsnprintf(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @verrprintf.errbuf, i64 0, i64 0), i32 1024, i8* %6, i32 %1)
ret i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @verrprintf.errbuf, i64 0, i64 0)
}