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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int log_err(char *fcn, char *str, int errno_set) {
  char *out;
  char *err_msg = NULL;
  int len = 0;
  if (1 == errno_set) {
    err_msg = strerror(errno);
    len = strlen(err_msg);
  }
  len += strlen(fcn) + strlen(str) + strlen("TC2.0") + 10;
  if (NULL == (out = (char *)malloc(sizeof(char) * len))) {
    fprintf(stderr, "log_err: Error in malloc, cannot log error.\n");
    return (0);
  }
  if (1 == errno_set) {
    sprintf(out, "[%s] %s: %s, %s.\n", "TC2.0", fcn, str, err_msg);
  } else {
    sprintf(out, "[%s] %s: %s.\n", "TC2.0", fcn, str);
  }
  fputs(out, stderr);
  free(out);
  return (1);
}


x86 assembly (clang, -O0):
<log_err>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jne    11be <log_err+0x4e>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x38(%rbp)
lea    0xe1c(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0xa,%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    123b <log_err+0xcb>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <log_err+0x146>
mov    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jne    1273 <log_err+0x103>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x30(%rbp),%r9
lea    0xdd3(%rip),%rsi
lea    0xd99(%rip),%rdx
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    1294 <log_err+0x124>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xdbf(%rip),%rsi
lea    0xd73(%rip),%rdx
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_err>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%r12
cmp    $0x1,%edx
jne    11aa <log_err+0x3a>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,(%rsp)
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
add    $0xa,%ebx
jmp    11b5 <log_err+0x45>
mov    $0xa,%ebx
xor    %eax,%eax
mov    %rax,(%rsp)
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0xe2f(%rip),%rdi
call   1040 <strlen@plt>
add    %ebx,%ebp
add    %r15d,%ebp
add    %eax,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1216 <log_err+0xa6>
mov    %rax,%rbx
cmp    $0x1,%r13d
jne    122f <log_err+0xbf>
lea    0xe3a(%rip),%rsi
lea    0xe00(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
mov    %r14,%r8
mov    (%rsp),%r9
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    124d <log_err+0xdd>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    126b <log_err+0xfb>
lea    0xe0f(%rip),%rsi
lea    0xdc3(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@.str = [6 x i8] c"TC2.0\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"log_err: Error in malloc, cannot log error.\0A\00"
@.str.2 = [18 x i8] c"[%s] %s: %s, %s.\0A\00"
@.str.3 = [14 x i8] c"[%s] %s: %s.\0A\00"
define i32 @log_err(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 1
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @strlen(i8* %7)
%9 = add i32 %8, 10
br label %10
10: ; preds = %5, %3
%11 = phi i8* [ %7, %5 ], [ null, %3 ]
%12 = phi i32 [ %9, %5 ], [ 10, %3 ]
%13 = tail call i32 @strlen(i8* %0)
%14 = tail call i32 @strlen(i8* %1)
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = add i32 %12, %13
%17 = add i32 %16, %14
%18 = add i32 %17, %15
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %34
25: ; preds = %10
br i1 %4, label %26, label %28
26: ; preds = %25
%27 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* %20, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %11)
br label %30
28: ; preds = %25
%29 = tail call i32 (i8*, i8*, i8*, i8*, i8*, ...) @sprintf(i8* %20, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %30
30: ; preds = %28, %26
%31 = load i32, i32* @stderr
%32 = tail call i32 @fputs(i8* %20, i32 %31)
%33 = tail call i32 @free(i8* %20)
br label %34
34: ; preds = %30, %22
%35 = phi i32 [ 0, %22 ], [ 1, %30 ]
ret i32 %35
}