#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 */

/* Variables and functions */
int strlen(char *);
int write(int, char *, int);
int writeline(int s, char *buff) {
  int r;
  int clen;
  int size;
  clen = strlen(buff);
  size = clen;
  while (size) {
    r = write(s, buff + clen - size, size);
    if (r == -1) {
      return (-1);
    }
    size -= r;
  }
  return (0);
}


x86 assembly (clang, -O0):
<writeline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1196 <writeline+0x76>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
movslq -0x1c(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1186 <writeline+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <writeline+0x7d>
mov    -0x14(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <writeline+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     1172 <writeline+0x52>
mov    %eax,%ebp
cltq
add    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rdx
mov    %rbx,%rsi
sub    %rdx,%rsi
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     116c <writeline+0x4c>
sub    %eax,%ebp
jne    1150 <writeline+0x30>
jmp    1172 <writeline+0x52>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @writeline(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %1, i64 %4
%6 = icmp eq i32 %3, 0
br i1 %6, label %17, label %9
7: ; preds = %9
%8 = icmp eq i32 %16, 0
br i1 %8, label %17, label %9
9: ; preds = %2, %7
%10 = phi i32 [ %16, %7 ], [ %3, %2 ]
%11 = sext i32 %10 to i64
%12 = sub nsw i64 0, %11
%13 = getelementptr inbounds i8, i8* %5, i64 %12
%14 = tail call i32 @write(i32 %0, i8* %13, i32 %10)
%15 = icmp eq i32 %14, -1
%16 = sub nsw i32 %10, %14
br i1 %15, label %17, label %7
17: ; preds = %9, %7, %2
%18 = phi i32 [ 0, %2 ], [ 0, %7 ], [ -1, %9 ]
ret i32 %18
}