#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*/ buf;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
char *strerror(int);
int strnlen(char *, int);
int /*<<< orphan*/ vsnprintf(char *, int, char *, int /*<<< orphan*/);
void err_doit(int errnoflag, int level, char *fmt, va_list ap) {
  int errno_save, n;
  char buf[4096];
  errno_save = errno;
  vsnprintf(buf, sizeof(buf), fmt, ap);
  n = strnlen(buf, sizeof(buf));
  if (errnoflag)
    snprintf(buf + n, sizeof(buf) - n, ": %s", strerror(errno_save));
  strcat(buf, "\n");
  fflush(stdout);
  fputs(buf, stderr);
  fflush(stderr);
}


x86 assembly (clang, -O0):
<err_doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x1000,%esi
call   1030 <vsnprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <strnlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1215 <err_doit+0xa5>
lea    -0x1020(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x1030(%rbp)
movslq -0x1c(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
mov    %eax,-0x1024(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x1030(%rbp),%rdi
mov    -0x1024(%rbp),%esi
mov    %rax,%rcx
lea    0xdf0(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1080 <strcat@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<err_doit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%ebp
mov    0x2e52(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
mov    $0x1000,%ebx
mov    %r15,%rdi
mov    $0x1000,%esi
call   1030 <vsnprintf@plt>
mov    %r15,%rdi
mov    $0x1000,%esi
call   1060 <strnlen@plt>
test   %ebp,%ebp
je     11d3 <err_doit+0x63>
cltq
lea    (%rsp,%rax,1),%rbp
sub    %eax,%ebx
mov    %r14d,%edi
call   1090 <strerror@plt>
lea    0xe3a(%rip),%rdx
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    0xe2b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2de6(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    0x0(%rbp),%edi
call   1070 <fflush@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@.str = [5 x i8] c": %s\00"
@.str.1 = [2 x i8] c"\0A\00"
@stdout = global i32 0
@stderr = global i32 0
define void @err_doit(i32 %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @errno
%8 = call i32 @vsnprintf(i8* %6, i32 4096, i8* %2, i32 %3)
%9 = call i32 @strnlen(i8* %6, i32 4096)
%10 = icmp eq i32 %0, 0
br i1 %10, label %17, label %11
11: ; preds = %4
%12 = sext i32 %9 to i64
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %12
%14 = sub i32 4096, %9
%15 = call i8* @strerror(i32 %7)
%16 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %11, %4
%18 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
%21 = load i32, i32* @stderr
%22 = call i32 @fputs(i8* %6, i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 @fflush(i32 %23)
ret void
}