#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 Vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int errno;
scalar_t__ out_get_errormsg();
int /*<<< orphan*/ out_snprintf(char *, int, char *, char const *,
                                char const *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int);
void out_error(const char *file, int line, const char *func, const char *suffix,
               const char *fmt, va_list ap) {
  int oerrno = errno;
  unsigned cc = 0;
  int ret;
  const char *sep = "";
  const char *errstr = "";
  char *errormsg = (char *)out_get_errormsg();
  if (fmt) {
    if (*fmt == '!') {
      fmt++;
      sep = ": ";
      errstr = strerror(errno);
    }
    ret = Vsnprintf(&errormsg[cc], 8192, fmt, ap);
    if (ret < 0) {
      strcpy(errormsg, "Vsnprintf failed");
      goto end;
    }
    cc += (unsigned)ret;
    out_snprintf(&errormsg[cc], 8192 - cc, "%s%s", sep, errstr);
  }
end:
  errno = oerrno;
}


x86 assembly (clang, -O0):
<out_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <out_get_errormsg@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1253 <out_error+0x103>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    11e7 <out_error+0x97>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
lea    0xe2f(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x2000,%esi
call   1060 <Vsnprintf@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1224 <out_error+0xd4>
mov    -0x50(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1258 <out_error+0x108>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    %rax,%rdi
mov    $0x2000,%esi
sub    -0x34(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    -0x48(%rbp),%r8
lea    0xdc6(%rip),%rdx
call   1040 <out_snprintf@plt>
jmp    1258 <out_error+0x108>
mov    -0x30(%rbp),%ecx
mov    0x2d7e(%rip),%rax
mov    %ecx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<out_error>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%rbp
mov    0x2e78(%rip),%rbx
mov    (%rbx),%r12d
xor    %eax,%eax
call   1050 <out_get_errormsg@plt>
test   %rbp,%rbp
je     11f6 <out_error+0xa6>
mov    %rax,%r13
cmpb   $0x21,0x0(%rbp)
jne    1198 <out_error+0x48>
add    $0x1,%rbp
mov    (%rbx),%edi
call   1070 <strerror@plt>
mov    %rax,(%rsp)
lea    0xe6a(%rip),%r15
jmp    11a3 <out_error+0x53>
lea    0xe63(%rip),%r15
mov    %r15,(%rsp)
mov    $0x2000,%ebx
mov    %r13,%rdi
mov    $0x2000,%esi
mov    %rbp,%rdx
mov    %r14d,%ecx
call   1060 <Vsnprintf@plt>
test   %eax,%eax
js     11e0 <out_error+0x90>
mov    %eax,%ecx
add    %rcx,%r13
sub    %eax,%ebx
lea    0xe47(%rip),%rdx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
mov    (%rsp),%r8
call   1040 <out_snprintf@plt>
jmp    11ef <out_error+0x9f>
lea    0xe1c(%rip),%rsi
mov    %r13,%rdi
call   1030 <strcpy@plt>
mov    0x2dea(%rip),%rbx
mov    %r12d,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c": \00"
@.str.2 = [17 x i8] c"Vsnprintf failed\00"
@.str.3 = [5 x i8] c"%s%s\00"
define void @out_error(i8* nocapture readnone %0, i32 %1, i8* nocapture readnone %2, i8* nocapture readnone %3, i8* %4, i32 %5) {
%7 = load i32, i32* @errno
%8 = tail call i64 (...) @out_get_errormsg()
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i8* %4, null
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 33
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %4, i64 1
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
br label %18
18: ; preds = %14, %11
%19 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %11 ]
%20 = phi i8* [ %17, %14 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %11 ]
%21 = phi i8* [ %15, %14 ], [ %4, %11 ]
%22 = tail call i32 @Vsnprintf(i8* %9, i32 8192, i8* %21, i32 %5)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = tail call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %31
26: ; preds = %18
%27 = zext i32 %22 to i64
%28 = getelementptr inbounds i8, i8* %9, i64 %27
%29 = sub nsw i32 8192, %22
%30 = tail call i32 @out_snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %19, i8* %20)
br label %31
31: ; preds = %6, %26, %24
store i32 %7, i32* @errno
ret void
}