#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;
typedef scalar_t__ buf;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int write(int, void const *, size_t);
ssize_t swrite(int fd, const void *buf, size_t n) {
  ssize_t rv;
  size_t rn = n;
  do
    switch (rv = write(fd, buf, n)) {
    case -1:
      if (errno == EINTR)
        continue;
      return -1;
    default:
      n -= rv;
      buf = (const char *)(buf) + rv;
      break;
    }
  while (n > 0);
  return rn;
}


x86 assembly (clang, -O0):
<swrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
jne    1175 <swrite+0x65>
jmp    114c <swrite+0x3c>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%eax
jne    1169 <swrite+0x59>
jmp    1198 <swrite+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <swrite+0x9a>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1198 <swrite+0x88>
cmpq   $0x0,-0x18(%rbp)
ja     112b <swrite+0x1b>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb5(%rip),%r12
mov    0x2ea6(%rip),%r13
mov    %rdx,%rbp
jmp    114d <swrite+0x3d>
nopw   0x0(%rax,%rax,1)
cltq
sub    %rax,%rbp
add    %rax,%rbx
test   %rbp,%rbp
je     1170 <swrite+0x60>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <swrite+0x30>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
je     1148 <swrite+0x38>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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