#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int _imap4_vprintf(FILE *out, char const *format, va_list ap) {
  int rv;
  char *buff = malloc(512 + 1);
  rv = vsnprintf(buff, 512 + 1, format, ap);
  if (strlen(buff) < 2) {
    strcat(buff, "\r\n");
  } else if (strcmp(buff + strlen(buff) - 2, "\r\n") != 0) {
    strcpy(buff + strlen(buff) - 2, "\r\n");
  }
  fwrite(buff, strlen(buff), 1, out);
  return rv;
}


x86 assembly (clang, -O0):
<_imap4_vprintf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x201,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x201,%esi
call   1040 <vsnprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jge    11d0 <_imap4_vprintf+0x60>
mov    -0x20(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1080 <strcat@plt>
jmp    1233 <_imap4_vprintf+0xc3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe0b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     122e <_imap4_vprintf+0xbe>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xdd7(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1233 <_imap4_vprintf+0xc3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_imap4_vprintf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
mov    $0x201,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x201,%esi
mov    %rbp,%rdx
mov    %r15d,%ecx
call   1040 <vsnprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jg     11c0 <_imap4_vprintf+0x50>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
jmp    1201 <_imap4_vprintf+0x91>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe27(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1201 <_imap4_vprintf+0x91>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe04(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @_imap4_vprintf(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i8* @malloc(i32 513)
store i8* %9, i8** %8
%10 = load i8*, i8** %8
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = call i32 @vsnprintf(i8* %10, i32 513, i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %8
%15 = call i32 @strlen(i8* %14)
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %8
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %38
20: ; preds = %3
%21 = load i8*, i8** %8
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = getelementptr inbounds i8, i8* %25, i64 -2
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %20
%30 = load i8*, i8** %8
%31 = load i8*, i8** %8
%32 = call i32 @strlen(i8* %31)
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 -2
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %29, %20
br label %38
38: ; preds = %37, %17
%39 = load i8*, i8** %8
%40 = load i8*, i8** %8
%41 = call i32 @strlen(i8* %40)
%42 = load i32*, i32** %4
%43 = call i32 @fwrite(i8* %39, i32 %41, i32 1, i32* %42)
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
define i32 @_imap4_vprintf(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call dereferenceable_or_null(513) i8* @malloc(i32 513)
%5 = tail call i32 @vsnprintf(i8* %4, i32 513, i8* %1, i32 %2)
%6 = tail call i32 @strlen(i8* %4)
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %3
%11 = tail call i32 @strlen(i8* %4)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -2
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = tail call i32 @strlen(i8* %4)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %4, i64 %19
%21 = getelementptr inbounds i8, i8* %20, i64 -2
%22 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %10, %17, %8
%24 = tail call i32 @strlen(i8* %4)
%25 = tail call i32 @fwrite(i8* %4, i32 %24, i32 1, i32* %0)
ret i32 %5
}