#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 const *);
int write(int, char const *, int);
int dputline(int fd, const char *buf) {
  int len = strlen(buf);
  int n, count = 0;
  while (count < len) {
    if ((n = write(fd, buf + count, len - count)) < 0)
      return n;
    count += n;
  }
  return count;
}


x86 assembly (clang, -O0):
<dputline>:
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,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <dputline+0x6c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    117e <dputline+0x5e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <dputline+0x72>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1142 <dputline+0x22>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dputline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    115f <dputline+0x3f>
mov    %eax,%ebx
xor    %ebp,%ebp
nopl   (%rax)
movslq %ebp,%rax
lea    (%r15,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1030 <write@plt>
test   %eax,%eax
js     1163 <dputline+0x43>
add    %eax,%ebp
cmp    %ebp,%ebx
jg     1140 <dputline+0x20>
jmp    1165 <dputline+0x45>
xor    %ebp,%ebp
jmp    1165 <dputline+0x45>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dputline(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* %6
store i32 0, i32* %8
br label %11
11: ; preds = %28, %2
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %6
%22 = load i32, i32* %8
%23 = sub nsw i32 %21, %22
%24 = call i32 @write(i32 %16, i8* %20, i32 %23)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = load i32, i32* %7
store i32 %27, i32* %3
br label %34
28: ; preds = %15
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = add nsw i32 %30, %29
store i32 %31, i32* %8
br label %11
32: ; preds = %11
%33 = load i32, i32* %8
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %26
%35 = load i32, i32* %3
ret i32 %35
}

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