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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
scalar_t__ write(int, char *, scalar_t__);
int atomic_write(int fd, char *buf, ssize_t len) {
  while (len > 0) {
    ssize_t res = write(fd, buf, len);
    if (res == 0 || (res == -1 && errno != EAGAIN))
      return res;
    buf += res;
    len -= res;
  }
  return 1;
}


x86 assembly (clang, -O0):
<atomic_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    119e <atomic_write+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1172 <atomic_write+0x62>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    117e <atomic_write+0x6e>
mov    0x2e81(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     117e <atomic_write+0x6e>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11a5 <atomic_write+0x95>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1123 <atomic_write+0x13>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomic_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %rdx,%rdx
jle    1176 <atomic_write+0x66>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2eaa(%rip),%r12
mov    0x2e93(%rip),%r13
jmp    1152 <atomic_write+0x42>
nop
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
jne    1170 <atomic_write+0x60>
add    %rax,%rbp
sub    %rax,%rbx
jle    1176 <atomic_write+0x66>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1140 <atomic_write+0x30>
test   %rax,%rax
jne    114a <atomic_write+0x3a>
xor    %r14d,%r14d
jmp    1176 <atomic_write+0x66>
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 i64 0
@EAGAIN = global i64 0
define i32 @atomic_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %29, %3
%10 = load i64, i64* %7
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i64 @write(i32 %13, i8* %14, i64 %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %12
%20 = load i64, i64* %8
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22, %12
%27 = load i64, i64* %8
%28 = trunc i64 %27 to i32
store i32 %28, i32* %4
br label %37
29: ; preds = %22, %19
%30 = load i64, i64* %8
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 %30
store i8* %32, i8** %6
%33 = load i64, i64* %8
%34 = load i64, i64* %7
%35 = sub nsw i64 %34, %33
store i64 %35, i64* %7
br label %9
36: ; preds = %9
store i32 1, i32* %4
br label %37
37: ; preds = %36, %26
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @atomic_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp sgt i64 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3, %13
%6 = phi i64 [ %15, %13 ], [ %2, %3 ]
%7 = phi i8* [ %14, %13 ], [ %1, %3 ]
%8 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
switch i64 %8, label %13 [
i64 0, label %17
i64 -1, label %9
]
9: ; preds = %5
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %9, %5
%14 = getelementptr inbounds i8, i8* %7, i64 %8
%15 = sub nsw i64 %6, %8
%16 = icmp sgt i64 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %13, %5, %9, %3
%18 = phi i32 [ 1, %3 ], [ 1, %13 ], [ 0, %5 ], [ -1, %9 ]
ret i32 %18
}