#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 write;
typedef int uint8_t;
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
#define EAGAIN 131
#define ECONNRESET 130
#define EINTR 129
#define EPIPE 128
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ perror(char *);
ssize_t atomic_write(int fd, const void *buf, size_t count, uint8_t *hup) {
  do {
    ssize_t nbytes_tot = 0;
    ssize_t nbytes_cur = 0;
    uint8_t done = 0;
    if (NULL != hup) {
      *(hup) = 0;
    }
    while (!done) {
      nbytes_cur = (write)((fd), (buf) + nbytes_tot, count - nbytes_tot);
      if (-1 == nbytes_cur) {
        switch (errno) {
        case EAGAIN:
          done = 1;
          break;
        case EINTR:
          break;
        case EPIPE:
          if (NULL != hup) {
            *(hup) = 1;
          }
          done = 1;
          break;
        case ECONNRESET:
          if (NULL != hup) {
            *(hup) = 1;
          }
          done = 1;
          break;
        default:
          perror("atomic_io");
          assert(0);
          break;
        }
      } else if (0 == nbytes_cur) {
        if (NULL != hup) {
          *(hup) = 1;
        }
        done = 1;
      } else {
        nbytes_tot += nbytes_cur;
        done = (nbytes_tot == count);
      }
    }
    return nbytes_tot;
  } while (0);
  ;
}


x86 assembly (clang, -O0):
<atomic_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1164 <atomic_write+0x44>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
jmp    1169 <atomic_write+0x49>
cmpl   $0x0,-0x34(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    117f <atomic_write+0x5f>
jmp    128f <atomic_write+0x16f>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x30(%rbp),%rax
jne    1238 <atomic_write+0x118>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x3,%eax
ja     1220 <atomic_write+0x100>
mov    -0x40(%rbp),%rax
lea    0xe3e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x34(%rbp)
jmp    1233 <atomic_write+0x113>
jmp    1233 <atomic_write+0x113>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     11f2 <atomic_write+0xd2>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x34(%rbp)
jmp    1233 <atomic_write+0x113>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1214 <atomic_write+0xf4>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x34(%rbp)
jmp    1233 <atomic_write+0x113>
lea    0xde9(%rip),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1030 <assert@plt>
jmp    128a <atomic_write+0x16a>
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    1266 <atomic_write+0x146>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     125a <atomic_write+0x13a>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x1,-0x34(%rbp)
jmp    1285 <atomic_write+0x165>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
jmp    128a <atomic_write+0x16a>
jmp    1169 <atomic_write+0x49>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomic_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
test   %rcx,%rcx
je     113a <atomic_write+0x1a>
movl   $0x0,(%r14)
mov    0x2e9f(%rip),%r12
lea    0xeb8(%rip),%r15
nopl   0x0(%rax,%rax,1)
cmp    $0xffffffffffffffff,%rbx
jne    1187 <atomic_write+0x67>
mov    (%r12),%eax
cmp    $0x81,%eax
je     1150 <atomic_write+0x30>
cmp    $0x83,%eax
je     1198 <atomic_write+0x78>
cmp    $0x82,%eax
je     118c <atomic_write+0x6c>
cmp    $0x80,%eax
je     118c <atomic_write+0x6c>
mov    %r15,%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1030 <assert@plt>
jmp    1150 <atomic_write+0x30>
test   %rbx,%rbx
jne    119a <atomic_write+0x7a>
test   %r14,%r14
je     1198 <atomic_write+0x78>
movl   $0x1,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [10 x i8] c"atomic_io\00"
define i64 @atomic_write(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
br label %12
12: ; preds = %4
store i64 0, i64* %9
store i64 0, i64* %10
store i32 0, i32* %11
%13 = load i32*, i32** %8
%14 = icmp ne i32* null, %13
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32*, i32** %8
store i32 0, i32* %16
br label %17
17: ; preds = %15, %12
br label %18
18: ; preds = %70, %17
%19 = load i32, i32* %11
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %71
22: ; preds = %18
%23 = load i32, i32* %5
%24 = load i8*, i8** %6
%25 = load i64, i64* %9
%26 = getelementptr i8, i8* %24, i64 %25
%27 = load i64, i64* %7
%28 = load i64, i64* %9
%29 = sub i64 %27, %28
store i64 %29, i64* %10
%30 = load i64, i64* %10
%31 = icmp eq i64 -1, %30
br i1 %31, label %32, label %52
32: ; preds = %22
%33 = load i32, i32* @errno
switch i32 %33, label %48 [
i32 131, label %34
i32 129, label %35
i32 128, label %36
i32 130, label %42
]
34: ; preds = %32
store i32 1, i32* %11
br label %51
35: ; preds = %32
br label %51
36: ; preds = %32
%37 = load i32*, i32** %8
%38 = icmp ne i32* null, %37
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32*, i32** %8
store i32 1, i32* %40
br label %41
41: ; preds = %39, %36
store i32 1, i32* %11
br label %51
42: ; preds = %32
%43 = load i32*, i32** %8
%44 = icmp ne i32* null, %43
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load i32*, i32** %8
store i32 1, i32* %46
br label %47
47: ; preds = %45, %42
store i32 1, i32* %11
br label %51
48: ; preds = %32
%49 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%50 = call i32 @assert(i32 0)
br label %51
51: ; preds = %48, %47, %41, %35, %34
br label %70
52: ; preds = %22
%53 = load i64, i64* %10
%54 = icmp eq i64 0, %53
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i32*, i32** %8
%57 = icmp ne i32* null, %56
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = load i32*, i32** %8
store i32 1, i32* %59
br label %60
60: ; preds = %58, %55
store i32 1, i32* %11
br label %69
61: ; preds = %52
%62 = load i64, i64* %10
%63 = load i64, i64* %9
%64 = add i64 %63, %62
store i64 %64, i64* %9
%65 = load i64, i64* %9
%66 = load i64, i64* %7
%67 = icmp eq i64 %65, %66
%68 = zext i1 %67 to i32
store i32 %68, i32* %11
br label %69
69: ; preds = %61, %60
br label %70
70: ; preds = %69, %51
br label %18
71: ; preds = %18
%72 = load i64, i64* %9
br label %73
73: ; preds = %71
ret i64 %72
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [10 x i8] c"atomic_io\00"
define i64 @atomic_write(i32 %0, i8* nocapture readnone %1, i64 %2, i32* writeonly %3) {
%5 = icmp eq i32* %3, null
br i1 %5, label %7, label %6
6: ; preds = %4
store i32 0, i32* %3
br label %7
7: ; preds = %6, %4
br label %8
8: ; preds = %11, %7
switch i64 %2, label %19 [
i64 -1, label %9
i64 0, label %17
]
9: ; preds = %8
%10 = load i32, i32* @errno
switch i32 %10, label %14 [
i32 131, label %19
i32 129, label %11
i32 128, label %12
i32 130, label %13
]
11: ; preds = %9, %14
br label %8
12: ; preds = %9
br i1 %5, label %19, label %18
13: ; preds = %9
br i1 %5, label %19, label %18
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @assert(i32 0)
br label %11
17: ; preds = %8
br i1 %5, label %19, label %18
18: ; preds = %17, %13, %12
store i32 1, i32* %3
br label %19
19: ; preds = %9, %8, %18, %12, %13, %17
%20 = phi i64 [ 0, %17 ], [ 0, %13 ], [ 0, %12 ], [ 0, %18 ], [ 0, %9 ], [ %2, %8 ]
ret i64 %20
}