#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_DEBUG;
int /*<<< orphan*/ S_IFSOCK;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
int isfdtype(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ vfprintf(int /*<<< orphan*/, char const *,
                            int /*<<< orphan*/);
int /*<<< orphan*/ vsyslog(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
void sanei_debug_msg(int level, int max_level, const char *be, const char *fmt,
                     va_list ap) {
  char *msg;
  if (max_level >= level) {
    if (1 == isfdtype(fileno(stderr), S_IFSOCK)) {
      msg = (char *)malloc(sizeof(char) * (strlen(be) + strlen(fmt) + 4));
      if (msg == NULL) {
        syslog(LOG_DEBUG, "[sanei_debug] malloc() failed\n");
        vsyslog(LOG_DEBUG, fmt, ap);
      } else {
        sprintf(msg, "[%s] %s", be, fmt);
        vsyslog(LOG_DEBUG, msg, ap);
        free(msg);
      }
    } else {
      fprintf(stderr, "[%s] ", be);
      vfprintf(stderr, fmt, ap);
    }
  }
}


x86 assembly (clang, -O0):
<sanei_debug_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     12cb <sanei_debug_msg+0x12b>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1090 <isfdtype@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1298 <sanei_debug_msg+0xf8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x4,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125d <sanei_debug_msg+0xbd>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
call   1080 <syslog@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10b0 <vsyslog@plt>
jmp    1293 <sanei_debug_msg+0xf3>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdaf(%rip),%rsi
call   10c0 <sprintf@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10b0 <vsyslog@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    12c6 <sanei_debug_msg+0x126>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd7b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10a0 <vfprintf@plt>
jmp    12cb <sanei_debug_msg+0x12b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sanei_debug_msg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    %edi,%esi
jge    11b6 <sanei_debug_msg+0x16>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%rbp
mov    0x2e12(%rip),%rbx
mov    (%rbx),%edi
call   1060 <fileno@plt>
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <isfdtype@plt>
cmp    $0x1,%eax
jne    1242 <sanei_debug_msg+0xa2>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x4,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     126a <sanei_debug_msg+0xca>
mov    %rax,%rbx
lea    0xe11(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r15,%rcx
call   10c0 <sprintf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   10b0 <vsyslog@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    (%rbx),%edi
lea    0xddc(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <vfprintf@plt>
mov    0x2d5f(%rip),%rbx
mov    (%rbx),%edi
lea    0xd86(%rip),%rsi
call   1080 <syslog@plt>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <vsyslog@plt>

LLVM IR (-O0):
@stderr = global i32 0
@S_IFSOCK = global i32 0
@LOG_DEBUG = global i32 0
@.str = [31 x i8] c"[sanei_debug] malloc() failed\0A\00"
@.str.1 = [8 x i8] c"[%s] %s\00"
@.str.2 = [6 x i8] c"[%s] \00"
define void @sanei_debug_msg(i32 %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp sge i32 %12, %13
br i1 %14, label %15, label %63
15: ; preds = %5
%16 = load i32, i32* @stderr
%17 = call i32 @fileno(i32 %16)
%18 = load i32, i32* @S_IFSOCK
%19 = call i32 @isfdtype(i32 %17, i32 %18)
%20 = icmp eq i32 1, %19
br i1 %20, label %21, label %54
21: ; preds = %15
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
%24 = load i8*, i8** %9
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %23, %25
%27 = add nsw i32 %26, 4
%28 = sext i32 %27 to i64
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %11
%33 = load i8*, i8** %11
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %42
35: ; preds = %21
%36 = load i32, i32* @LOG_DEBUG
%37 = call i32 @syslog(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* @LOG_DEBUG
%39 = load i8*, i8** %9
%40 = load i32, i32* %10
%41 = call i32 @vsyslog(i32 %38, i8* %39, i32 %40)
br label %53
42: ; preds = %21
%43 = load i8*, i8** %11
%44 = load i8*, i8** %8
%45 = load i8*, i8** %9
%46 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i32, i32* @LOG_DEBUG
%48 = load i8*, i8** %11
%49 = load i32, i32* %10
%50 = call i32 @vsyslog(i32 %47, i8* %48, i32 %49)
%51 = load i8*, i8** %11
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %42, %35
br label %62
54: ; preds = %15
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %8
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %9
%60 = load i32, i32* %10
%61 = call i32 @vfprintf(i32 %58, i8* %59, i32 %60)
br label %62
62: ; preds = %54, %53
br label %63
63: ; preds = %62, %5
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@S_IFSOCK = global i32 0
@LOG_DEBUG = global i32 0
@.str = [31 x i8] c"[sanei_debug] malloc() failed\0A\00"
@.str.1 = [8 x i8] c"[%s] %s\00"
@.str.2 = [6 x i8] c"[%s] \00"
define void @sanei_debug_msg(i32 %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = icmp slt i32 %1, %0
br i1 %6, label %36, label %7
7: ; preds = %5
%8 = load i32, i32* @stderr
%9 = tail call i32 @fileno(i32 %8)
%10 = load i32, i32* @S_IFSOCK
%11 = tail call i32 @isfdtype(i32 %9, i32 %10)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %31
13: ; preds = %7
%14 = tail call i32 @strlen(i8* %2)
%15 = tail call i32 @strlen(i8* %3)
%16 = add i32 %14, 4
%17 = add i32 %16, %15
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32, i32* @LOG_DEBUG
%23 = tail call i32 @syslog(i32 %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @LOG_DEBUG
%25 = tail call i32 @vsyslog(i32 %24, i8* %3, i32 %4)
br label %36
26: ; preds = %13
%27 = tail call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %3)
%28 = load i32, i32* @LOG_DEBUG
%29 = tail call i32 @vsyslog(i32 %28, i8* %19, i32 %4)
%30 = tail call i32 @free(i8* %19)
br label %36
31: ; preds = %7
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%34 = load i32, i32* @stderr
%35 = tail call i32 @vfprintf(i32 %34, i8* %3, i32 %4)
br label %36
36: ; preds = %31, %26, %21, %5
ret void
}