#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 */
scalar_t__ archdep_default_logger(char const *, char *);
int /*<<< orphan*/ lib_free(char *);
char *lib_mvsprintf(char const *, int /*<<< orphan*/);
char *strchr(char *, char);
int strlen(char *);
int log_archdep(const char *logtxt, const char *fmt, va_list ap) {
  int rc = 0;
  char *txt = lib_mvsprintf(fmt, ap);
  char *beg = txt;
  char *end = txt + strlen(txt) + 1;
  while (beg < end) {
    char *eol = strchr(beg, '\n');
    if (eol)
      *eol = '\0';
    if (archdep_default_logger(*beg ? logtxt : "", beg) < 0) {
      rc = -1;
      break;
    }
    if (!eol)
      break;
    beg = eol + 1;
  }
  lib_free(txt);
  return rc;
}


x86 assembly (clang, -O0):
<log_archdep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <lib_mvsprintf@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    124a <log_archdep+0xfa>
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11d9 <log_archdep+0x89>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f6 <log_archdep+0xa6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1206 <log_archdep+0xb6>
lea    0xe03(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1206 <log_archdep+0xb6>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <archdep_default_logger@plt>
cmp    $0x0,%rax
jge    1229 <log_archdep+0xd9>
movl   $0xffffffff,-0x18(%rbp)
jmp    124a <log_archdep+0xfa>
cmpq   $0x0,-0x38(%rbp)
jne    1239 <log_archdep+0xe9>
jmp    124a <log_archdep+0xfa>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a7 <log_archdep+0x57>
mov    -0x20(%rbp),%rdi
call   1030 <lib_free@plt>
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_archdep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    %rsi,%rdi
mov    %edx,%esi
call   1060 <lib_mvsprintf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%r13
add    $0x1,%r13
xor    %r12d,%r12d
mov    $0xffffffff,%r14d
mov    %rbx,(%rsp)
nopw   0x0(%rax,%rax,1)
cmp    %r13,%rbx
jae    11d9 <log_archdep+0x89>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11ae <log_archdep+0x5e>
movb   $0x0,0x0(%rbp)
cmpb   $0x0,(%rbx)
mov    %r15,%rdi
lea    0xe45(%rip),%rax
cmove  %rax,%rdi
mov    %rbx,%rsi
call   1070 <archdep_default_logger@plt>
test   %rax,%rax
cmovs  %r14d,%r12d
js     11d9 <log_archdep+0x89>
lea    0x1(%rbp),%rbx
test   %rbp,%rbp
jne    1190 <log_archdep+0x40>
mov    (%rsp),%rdi
call   1030 <lib_free@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i32 @log_archdep(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i8* @lib_mvsprintf(i8* %12, i32 %13)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
store i8* %15, i8** %9
%16 = load i8*, i8** %8
%17 = load i8*, i8** %8
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %10
br label %22
22: ; preds = %51, %3
%23 = load i8*, i8** %9
%24 = load i8*, i8** %10
%25 = icmp ult i8* %23, %24
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = load i8*, i8** %9
%28 = call i8* @strchr(i8* %27, i8 signext 10)
store i8* %28, i8** %11
%29 = load i8*, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = load i8*, i8** %11
store i8 0, i8* %32
br label %33
33: ; preds = %31, %26
%34 = load i8*, i8** %9
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i8*, i8** %4
br label %41
40: ; preds = %33
br label %41
41: ; preds = %40, %38
%42 = phi i8* [ %39, %38 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %40 ]
%43 = load i8*, i8** %9
%44 = call i64 @archdep_default_logger(i8* %42, i8* %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %7
br label %54
47: ; preds = %41
%48 = load i8*, i8** %11
%49 = icmp ne i8* %48, null
br i1 %49, label %51, label %50
50: ; preds = %47
br label %54
51: ; preds = %47
%52 = load i8*, i8** %11
%53 = getelementptr inbounds i8, i8* %52, i64 1
store i8* %53, i8** %9
br label %22
54: ; preds = %50, %46, %22
%55 = load i8*, i8** %8
%56 = call i32 @lib_free(i8* %55)
%57 = load i32, i32* %7
ret i32 %57
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i32 @log_archdep(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @lib_mvsprintf(i8* %1, i32 %2)
%5 = tail call i32 @strlen(i8* %4)
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %4, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 1
br label %9
9: ; preds = %17, %3
%10 = phi i8* [ %4, %3 ], [ %23, %17 ]
%11 = phi i32 [ 0, %3 ], [ %25, %17 ]
%12 = icmp ult i8* %10, %8
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = tail call i8* @strchr(i8* dereferenceable(1) %10, i8 signext 10)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13
store i8 0, i8* %14
br label %17
17: ; preds = %16, %13
%18 = load i8, i8* %10
%19 = icmp eq i8 %18, 0
%20 = select i1 %19, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%21 = tail call i64 @archdep_default_logger(i8* %20, i8* %10)
%22 = icmp slt i64 %21, 0
%23 = getelementptr inbounds i8, i8* %14, i64 1
%24 = select i1 %22, i1 true, i1 %15
%25 = select i1 %22, i32 -1, i32 %11
br i1 %24, label %26, label %9
26: ; preds = %17, %9
%27 = phi i32 [ %25, %17 ], [ %11, %9 ]
%28 = tail call i32 @lib_free(i8* %4)
ret i32 %27
}