#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 /*<<< orphan*/ perror(char *);
int strlen(char *);
int write(int, char *, int);
void write_line(int fd, char *line) {
  char *p = line;
  int written = 0;
  int result;
  while (written < strlen(line)) {
    result = write(fd, p, strlen(p));
    if (result == -1) {
      perror("Write error");
      break;
    }
    written = written + result;
    p = p + result;
  }
}


x86 assembly (clang, -O0):
<write_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    11ca <write_line+0x9a>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ad <write_line+0x7d>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
jmp    11ca <write_line+0x9a>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114e <write_line+0x1e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_line>:
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    117d <write_line+0x4d>
xor    %ebp,%ebp
mov    %r15,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1188 <write_line+0x58>
add    %eax,%ebp
cltq
add    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    %eax,%ebp
jl     1150 <write_line+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe71(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Write error\00"
define void @write_line(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
store i8* %8, i8** %5
store i32 0, i32* %6
br label %9
9: ; preds = %24, %2
%10 = load i32, i32* %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = icmp slt i32 %10, %12
br i1 %13, label %14, label %32
14: ; preds = %9
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 %15, i8* %16, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %32
24: ; preds = %14
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = add nsw i32 %25, %26
store i32 %27, i32* %6
%28 = load i8*, i8** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8* %31, i8** %5
br label %9
32: ; preds = %22, %9
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"Write error\00"
define void @write_line(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %2, %13
%6 = phi i8* [ %16, %13 ], [ %1, %2 ]
%7 = phi i32 [ %14, %13 ], [ 0, %2 ]
%8 = tail call i32 @strlen(i8* %6)
%9 = tail call i32 @write(i32 %0, i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
13: ; preds = %5
%14 = add nsw i32 %9, %7
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds i8, i8* %6, i64 %15
%17 = tail call i32 @strlen(i8* %1)
%18 = icmp slt i32 %14, %17
br i1 %18, label %5, label %19
19: ; preds = %13, %2, %11
ret void
}