#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int write(int, void *, unsigned int);
int swrite(int fd, void *buf, unsigned int len) {
  ssize_t ret;
  while (len > 0 && (ret = write(fd, buf, len)) != 0) {
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      perror("Error writing fd");
      return -1;
    }
    buf += ret;
    len -= ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<swrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jbe    115c <swrite+0x3c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116c <swrite+0x4c>
jmp    11ce <swrite+0xae>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ad <swrite+0x8d>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <swrite+0x75>
jmp    1132 <swrite+0x12>
lea    0xe64(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <swrite+0xb5>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1132 <swrite+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
je     117f <swrite+0x5f>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e9f(%rip),%r12
mov    0x2e90(%rip),%r13
jmp    115a <swrite+0x3a>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1191 <swrite+0x71>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1150 <swrite+0x30>
test   %eax,%eax
je     117c <swrite+0x5c>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jne    115a <swrite+0x3a>
jmp    117f <swrite+0x5f>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    117f <swrite+0x5f>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [17 x i8] c"Error writing fd\00"
define i32 @swrite(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %30, %27, %3
%10 = load i32, i32* %7
%11 = icmp ugt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %12, %9
%19 = phi i1 [ false, %9 ], [ %17, %12 ]
br i1 %19, label %20, label %38
20: ; preds = %18
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %9
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
30: ; preds = %20
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = sext i32 %31 to i64
%34 = getelementptr i8, i8* %32, i64 %33
store i8* %34, i8** %6
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = sub i32 %36, %35
store i32 %37, i32* %7
br label %9
38: ; preds = %18
store i32 0, i32* %4
br label %39
39: ; preds = %38, %28
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [17 x i8] c"Error writing fd\00"
define i32 @swrite(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %16
%6 = phi i32 [ %19, %16 ], [ %2, %3 ]
%7 = phi i8* [ %18, %16 ], [ %1, %3 ]
br label %8
8: ; preds = %5, %10
%9 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
switch i32 %9, label %16 [
i32 0, label %21
i32 -1, label %10
]
10: ; preds = %8
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %8, label %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %21
16: ; preds = %8
%17 = sext i32 %9 to i64
%18 = getelementptr i8, i8* %7, i64 %17
%19 = sub i32 %6, %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %5
21: ; preds = %16, %8, %3, %14
%22 = phi i32 [ -1, %14 ], [ 0, %3 ], [ %9, %8 ], [ 0, %16 ]
ret i32 %22
}