#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 write(int const, char *, size_t);
bool log_write(const int fd, char *buf, ssize_t len) {
  ssize_t out;
  if ('\n' != buf[len - 1]) {
    buf[len] = '\n';
    buf[1 + len] = '\0';
    ++len;
  }
  do {
    out = write(fd, buf, (size_t)len);
    if (-1 == out)
      return false;
    len -= out;
  } while (0 < len);
  return true;
}


x86 assembly (clang, -O0):
<log_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
je     1166 <log_write+0x56>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    116b <log_write+0x5b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    1198 <log_write+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11b5 <log_write+0xa5>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jl     116b <log_write+0x5b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_write>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
movslq %edx,%rax
cmpb   $0xa,-0x1(%rax,%rsi,1)
je     1140 <log_write+0x30>
movw   $0xa,(%rbx,%rax,1)
add    $0x1,%eax
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     115e <log_write+0x4e>
sub    %eax,%ebp
jg     1140 <log_write+0x30>
mov    $0x1,%eax
jmp    1160 <log_write+0x50>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @log_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = load i32, i32* %7
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 10, %15
br i1 %16, label %17, label %29
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 10, i8* %21
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = add nsw i32 1, %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %29
29: ; preds = %17, %3
br label %30
30: ; preds = %43, %29
%31 = load i32, i32* %5
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = call i32 @write(i32 %31, i8* %32, i64 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 -1, %36
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 0, i32* %4
br label %47
39: ; preds = %30
%40 = load i32, i32* %8
%41 = load i32, i32* %7
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %7
br label %43
43: ; preds = %39
%44 = load i32, i32* %7
%45 = icmp slt i32 0, %44
br i1 %45, label %30, label %46
46: ; preds = %43
store i32 1, i32* %4
br label %47
47: ; preds = %46, %38
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
define i32 @log_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = add nsw i32 %2, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 10
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = sext i32 %2 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 10, i8* %11
%12 = add nsw i32 %2, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %9, %3
%16 = phi i32 [ %2, %3 ], [ %12, %9 ]
br label %17
17: ; preds = %15, %22
%18 = phi i32 [ %23, %22 ], [ %16, %15 ]
%19 = sext i32 %18 to i64
%20 = tail call i32 @write(i32 %0, i8* %1, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = sub nsw i32 %18, %20
%24 = icmp sgt i32 %23, 0
br i1 %24, label %17, label %25
25: ; preds = %22, %17
%26 = phi i32 [ 0, %17 ], [ 1, %22 ]
ret i32 %26
}