#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ signal_check_intr();
size_t write(int, char *, size_t);
ssize_t lmsc_write_all(int fd, void *buf, size_t count) {
  ssize_t rem, tw;
  char *p = buf;
  for (rem = count; rem;) {
    errno = 0;
    tw = write(fd, &p[count - rem], rem);
    if (tw < 0) {
      if (errno == EAGAIN || errno == EAGAIN) {
        break;
      }
      if (errno == EINTR) {
        if (signal_check_intr()) {
          break;
        }
        errno = 0;
        continue;
      }
      return tw;
    }
    rem -= tw;
  }
  return count - rem;
}


x86 assembly (clang, -O0):
<lmsc_write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1223 <lmsc_write_all+0x103>
mov    0x2e8b(%rip),%rax
movq   $0x0,(%rax)
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    120f <lmsc_write_all+0xef>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     11ba <lmsc_write_all+0x9a>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <lmsc_write_all+0x9f>
jmp    1223 <lmsc_write_all+0x103>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <lmsc_write_all+0xe2>
mov    $0x0,%al
call   1040 <signal_check_intr@plt>
cmp    $0x0,%rax
je     11ef <lmsc_write_all+0xcf>
jmp    1223 <lmsc_write_all+0x103>
mov    0x2dea(%rip),%rax
movq   $0x0,(%rax)
jmp    1143 <lmsc_write_all+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122f <lmsc_write_all+0x10f>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1143 <lmsc_write_all+0x23>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lmsc_write_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbp
test   %rdx,%rdx
je     1151 <lmsc_write_all+0x41>
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2eb3(%rip),%r12
mov    %rbp,%rbx
movq   $0x0,(%r12)
mov    %rbp,%rsi
sub    %rbx,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1030 <write@plt>
sub    %rax,%rbx
jne    1130 <lmsc_write_all+0x20>
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @lmsc_write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %8
br label %13
13: ; preds = %47, %44, %3
%14 = load i64, i64* %8
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %13
store i64 0, i64* @errno
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = load i64, i64* %8
%24 = call i64 @write(i32 %17, i8* %22, i64 %23)
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = icmp ult i64 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31, %27
br label %51
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = call i64 (...) @signal_check_intr()
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %40
br label %51
44: ; preds = %40
store i64 0, i64* @errno
br label %13
45: ; preds = %36
%46 = load i64, i64* %9
store i64 %46, i64* %4
br label %55
47: ; preds = %16
%48 = load i64, i64* %9
%49 = load i64, i64* %8
%50 = sub i64 %49, %48
store i64 %50, i64* %8
br label %13
51: ; preds = %43, %35, %13
%52 = load i64, i64* %7
%53 = load i64, i64* %8
%54 = sub i64 %52, %53
store i64 %54, i64* %4
br label %55
55: ; preds = %51, %45
%56 = load i64, i64* %4
ret i64 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @lmsc_write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %12, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %10, %5 ], [ %2, %3 ]
store i64 0, i64* @errno
%7 = sub i64 %2, %6
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = tail call i64 @write(i32 %0, i8* %8, i64 %6)
%10 = sub i64 %6, %9
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %5, %3
ret i64 %2
}