#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*/ errno;
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int write_data(int fd, void *buf, int len) {
  int bytes = 0;
  int ret = 0;
  while (len > 0) {
    ret = write(fd, (unsigned char *)buf + bytes, len);
    ;
    if (ret < 1) {
      do {
        printf("write error %s\n", strerror(errno));
      } while (0);
      break;
    }
    bytes += ret;
    len = len - ret;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11bb <write_data+0x8b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    11a4 <write_data+0x74>
jmp    1182 <write_data+0x52>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
jmp    11bb <write_data+0x8b>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <write_data+0x20>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    116c <write_data+0x3c>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1170 <write_data+0x40>
add    %eax,%ebx
sub    %eax,%ebp
jg     1150 <write_data+0x20>
jmp    118d <write_data+0x5d>
xor    %ebx,%ebx
jmp    118d <write_data+0x5d>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe7b(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"write error %s\0A\00"
@errno = global i32 0
define i32 @write_data(i32 %0, i8* %1, i32 %2) {
%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
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %28, %3
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i32, i32* %6
%19 = call i32 @write(i32 %13, i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %28
22: ; preds = %12
br label %23
23: ; preds = %22
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %23
br label %35
28: ; preds = %12
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = add nsw i32 %30, %29
store i32 %31, i32* %7
%32 = load i32, i32* %6
%33 = load i32, i32* %8
%34 = sub nsw i32 %32, %33
store i32 %34, i32* %6
br label %9
35: ; preds = %27, %9
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
@.str = [16 x i8] c"write error %s\0A\00"
@errno = global i32 0
define i32 @write_data(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %20
5: ; preds = %3, %16
%6 = phi i32 [ %17, %16 ], [ 0, %3 ]
%7 = phi i32 [ %18, %16 ], [ %2, %3 ]
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i32 @write(i32 %0, i8* %9, i32 %7)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %5
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %20
16: ; preds = %5
%17 = add nsw i32 %10, %6
%18 = sub nsw i32 %7, %10
%19 = icmp sgt i32 %18, 0
br i1 %19, label %5, label %20
20: ; preds = %16, %3, %12
%21 = phi i32 [ %6, %12 ], [ 0, %3 ], [ %17, %16 ]
ret i32 %21
}