#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*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strerror(scalar_t__);
int strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ vsprintf(char *, char const *, int /*<<< orphan*/);
void log_message(int status, const char *mode, const char *fmt, va_list ap) {
  char buf[4096];
  vsprintf(buf, fmt, ap);
  if (errno == 0)
    sprintf(buf + strlen(buf), "\n");
  else
    sprintf(buf + strlen(buf), ": %s\n", strerror(errno));
  syslog(LOG_ERR, "%s: %s", mode, buf);
  if (status >= 0) {
    exit(status);
  }
  return;
}


x86 assembly (clang, -O0):
<log_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <vsprintf@plt>
mov    0x2e4d(%rip),%rax
cmpq   $0x0,(%rax)
jne    11d6 <log_message+0x76>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1028(%rbp),%rdi
cltq
add    %rax,%rdi
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    122d <log_message+0xcd>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1038(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x1038(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x1030(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x1030(%rbp),%rdi
mov    %rax,%rdx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    -0x1020(%rbp),%rcx
lea    0xdbe(%rip),%rsi
call   1040 <syslog@plt>
cmpl   $0x0,-0x4(%rbp)
jl     125f <log_message+0xff>
mov    -0x4(%rbp),%edi
call   1070 <exit@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_message>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1050 <vsprintf@plt>
mov    0x2e54(%rip),%r12
mov    (%r12),%rbx
mov    %rbp,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbp
test   %rbx,%rbx
je     11c2 <log_message+0x62>
mov    (%r12),%rdi
call   1080 <strerror@plt>
lea    0xe4d(%rip),%rsi
mov    %rbp,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
jmp    11c8 <log_message+0x68>
movw   $0xa,0x0(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %rsp,%rcx
mov    %r15,%rdx
call   1040 <syslog@plt>
test   %r14d,%r14d
jns    11f8 <log_message+0x98>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@errno = global i64 0
@.str.1 = [6 x i8] c": %s\0A\00"
@LOG_ERR = global i32 0
@.str.2 = [7 x i8] c"%s: %s\00"
define void @log_message(i32 %0, i8* %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 = call i32 @vsprintf(i8* %6, i8* %2, i32 %3)
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
%10 = call i32 @strlen(i8* %6)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %11
br i1 %9, label %13, label %15
13: ; preds = %4
%14 = bitcast i8* %12 to i16*
store i16 10, i16* %14
br label %19
15: ; preds = %4
%16 = load i64, i64* @errno
%17 = call i8* @strerror(i64 %16)
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %13
%20 = load i32, i32* @LOG_ERR
%21 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %6)
%22 = icmp sgt i32 %0, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @exit(i32 %0)
unreachable
25: ; preds = %19
ret void
}