#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int qio_write(int, char *, size_t, int);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
ssize_t qio_puts(int fd, const char *str, int timeoutms) {
  size_t strsize = strlen(str);
  char *newstr = (char *)malloc(strsize + 1 + 1);
  if (newstr == NULL)
    return -1;
  strncpy(newstr, str, strsize);
  newstr[strsize] = '\n';
  newstr[strsize + 1] = '\0';
  ssize_t ret = qio_write(fd, newstr, strsize + 1, timeoutms);
  free(newstr);
  return ret;
}


x86 assembly (clang, -O0):
<qio_puts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119b <qio_puts+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <qio_puts+0x9e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
mov    -0x14(%rbp),%ecx
call   1050 <qio_write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qio_puts>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%r15d
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
lea    0x2(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ba <qio_puts+0x6a>
mov    %rax,%rbx
mov    %rbp,%r13
add    $0x1,%r13
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
movw   $0xa,(%rbx,%rbp,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %r14d,%ecx
call   1050 <qio_write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    11bf <qio_puts+0x6f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @qio_puts(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = add i64 %13, 1
%15 = add i64 %14, 1
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %42
21: ; preds = %3
%22 = load i8*, i8** %9
%23 = load i8*, i8** %6
%24 = load i64, i64* %8
%25 = call i32 @strncpy(i8* %22, i8* %23, i64 %24)
%26 = load i8*, i8** %9
%27 = load i64, i64* %8
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 10, i8* %28
%29 = load i8*, i8** %9
%30 = load i64, i64* %8
%31 = add i64 %30, 1
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %5
%34 = load i8*, i8** %9
%35 = load i64, i64* %8
%36 = add i64 %35, 1
%37 = load i32, i32* %7
%38 = call i32 @qio_write(i32 %33, i8* %34, i64 %36, i32 %37)
store i32 %38, i32* %10
%39 = load i8*, i8** %9
%40 = call i32 @free(i8* %39)
%41 = load i32, i32* %10
store i32 %41, i32* %4
br label %42
42: ; preds = %21, %20
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
define i32 @qio_puts(i32 %0, i8* %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %4, 1
%6 = add i64 %4, 2
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = tail call i32 @strncpy(i8* %8, i8* %1, i64 %4)
%12 = getelementptr inbounds i8, i8* %8, i64 %4
store i8 10, i8* %12
%13 = getelementptr inbounds i8, i8* %8, i64 %5
store i8 0, i8* %13
%14 = tail call i32 @qio_write(i32 %0, i8* %8, i64 %5, i32 %2)
%15 = tail call i32 @free(i8* %8)
br label %16
16: ; preds = %3, %10
%17 = phi i32 [ %14, %10 ], [ -1, %3 ]
ret i32 %17
}