#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_INFO;
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int vsnprintf(char *, int, char *, int /*<<< orphan*/);
void doSyslog(char *format, va_list args) {
  int bufLen = 80;
  char *buf = malloc(bufLen);
  int ret;
  while (1) {
    ret = vsnprintf(buf, bufLen, format, args);
    if ((ret > -1) && (ret < bufLen)) {
      break;
    } else {
      bufLen += 80;
      buf = realloc(buf, bufLen);
    }
  }
  syslog(LOG_INFO, buf);
  free(buf);
}


x86 assembly (clang, -O0):
<doSyslog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x50,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jle    11a3 <doSyslog+0x53>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11a3 <doSyslog+0x53>
jmp    11c1 <doSyslog+0x71>
mov    -0x10(%rbp),%eax
add    $0x50,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    1172 <doSyslog+0x22>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1060 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doSyslog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x50,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x50,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
cmp    $0x50,%eax
jb     11b6 <doSyslog+0x66>
mov    $0xa0,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
lea    0x50(%rbp),%ecx
cmp    %ebp,%eax
mov    %ecx,%ebp
jae    1190 <doSyslog+0x40>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <syslog@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@LOG_INFO = global i32 0
define void @doSyslog(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 80, i32* %5
%8 = load i32, i32* %5
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %6
br label %10
10: ; preds = %2, %29
%11 = load i8*, i8** %6
%12 = load i32, i32* %5
%13 = load i8*, i8** %3
%14 = load i32, i32* %4
%15 = call i32 @vsnprintf(i8* %11, i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %10
%19 = load i32, i32* %7
%20 = load i32, i32* %5
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %30
23: ; preds = %18, %10
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 80
store i32 %25, i32* %5
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = call i8* @realloc(i8* %26, i32 %27)
store i8* %28, i8** %6
br label %29
29: ; preds = %23
br label %10
30: ; preds = %22
%31 = load i32, i32* @LOG_INFO
%32 = load i8*, i8** %6
%33 = call i32 @syslog(i32 %31, i8* %32)
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
ret void
}

LLVM IR (-O2):
@LOG_INFO = global i32 0
define void @doSyslog(i8* nocapture %0, i32 %1) {
%3 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%4 = tail call i32 @vsnprintf(i8* %3, i32 80, i8* %0, i32 %1)
%5 = icmp ult i32 %4, 80
br i1 %5, label %13, label %6
6: ; preds = %2, %6
%7 = phi i32 [ %9, %6 ], [ 80, %2 ]
%8 = phi i8* [ %10, %6 ], [ %3, %2 ]
%9 = add nuw nsw i32 %7, 80
%10 = tail call i8* @realloc(i8* %8, i32 %9)
%11 = tail call i32 @vsnprintf(i8* %10, i32 %9, i8* %0, i32 %1)
%12 = icmp ult i32 %11, %9
br i1 %12, label %13, label %6
13: ; preds = %6, %2
%14 = phi i8* [ %3, %2 ], [ %10, %6 ]
%15 = load i32, i32* @LOG_INFO
%16 = tail call i32 @syslog(i32 %15, i8* %14)
%17 = tail call i32 @free(i8* %14)
ret void
}