#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*/ crash(char *);
int write(int, char const *, int);
void write_chars(int chars_rest, const char *buf) {
  int total_chars = chars_rest;
  while (chars_rest > 0) {
    int chars_write = write(1, buf + total_chars - chars_rest, chars_rest);
    if (chars_write == -1) {
      crash("An I\\O error occured while writing");
    }
    chars_rest -= chars_write;
  }
}


x86 assembly (clang, -O0):
<write_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    118c <write_chars+0x6c>
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
movslq -0x4(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    117c <write_chars+0x5c>
lea    0xe89(%rip),%rdi
call   1030 <crash@plt>
mov    -0x18(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1135 <write_chars+0x15>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_chars>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
jle    1169 <write_chars+0x49>
mov    %rsi,%r15
mov    %edi,%ebp
mov    %edi,%eax
add    %rax,%r15
lea    0xec4(%rip),%r14
jmp    1144 <write_chars+0x24>
xchg   %ax,%ax
sub    %ebx,%ebp
jle    1169 <write_chars+0x49>
mov    %ebp,%eax
mov    %r15,%rsi
sub    %rax,%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1140 <write_chars+0x20>
mov    %r14,%rdi
call   1030 <crash@plt>
jmp    1140 <write_chars+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [35 x i8] c"An I\\O error occured while writing\00"
define void @write_chars(i32 %0, i8* nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %19
4: ; preds = %2
%5 = zext i32 %0 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
br label %7
7: ; preds = %4, %16
%8 = phi i32 [ %0, %4 ], [ %17, %16 ]
%9 = zext i32 %8 to i64
%10 = sub nsw i64 0, %9
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = tail call i32 @write(i32 1, i8* %11, i32 %8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @crash(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %7
%17 = sub nsw i32 %8, %12
%18 = icmp sgt i32 %17, 0
br i1 %18, label %7, label %19
19: ; preds = %16, %2
ret void
}