#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 u_int;
typedef int ssize_t;
typedef int f;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ EPIPE;
int /*<<< orphan*/ errno;
size_t atomicio(ssize_t (*f)(int, void *, size_t), int fd, void *_s, size_t n) {
  char *s = _s;
  size_t pos = 0;
  ssize_t res;
  while (n > pos) {
    res = (f)(fd, s + pos, n - pos);
    switch (res) {
    case -1:
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return 0;
    case 0:
      errno = EPIPE;
      return pos;
    default:
      pos += (u_int)res;
    }
  }
  return (pos);
}


x86 assembly (clang, -O0):
<atomicio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    11e9 <atomicio+0xe9>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rcx
add    %rcx,%rsi
mov    -0x28(%rbp),%rdx
sub    %rcx,%rdx
call   *%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0xffffffff,%eax
je     1177 <atomicio+0x77>
jmp    1167 <atomicio+0x67>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     11b9 <atomicio+0xb9>
jmp    11d8 <atomicio+0xd8>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
je     11a7 <atomicio+0xa7>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%eax
jne    11ac <atomicio+0xac>
jmp    1127 <atomicio+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <atomicio+0xf1>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0f(%rip),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f1 <atomicio+0xf1>
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1127 <atomicio+0x27>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomicio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rcx,%rcx
je     11a5 <atomicio+0xa5>
mov    %esi,%r13d
mov    %rdi,%rbx
mov    0x2eb4(%rip),%r14
mov    0x2ea5(%rip),%r12
mov    %rcx,0x10(%rsp)
mov    %rcx,%rbp
mov    %rdx,0x8(%rsp)
mov    %rdx,%r15
xor    %eax,%eax
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   *%rbx
cmp    $0xffffffff,%eax
jne    1180 <atomicio+0x80>
mov    (%r14),%eax
cmp    (%r12),%eax
je     1150 <atomicio+0x50>
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%eax
je     1150 <atomicio+0x50>
jmp    11a5 <atomicio+0xa5>
cs nopw 0x0(%rax,%rax,1)
test   %eax,%eax
je     11a9 <atomicio+0xa9>
cltq
mov    (%rsp),%rcx
add    %rax,%rcx
mov    %rcx,%rax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,1),%r15
mov    0x10(%rsp),%rbp
sub    %rax,%rbp
ja     113d <atomicio+0x3d>
jmp    11b9 <atomicio+0xb9>
xor    %eax,%eax
jmp    11b9 <atomicio+0xb9>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    (%rsp),%rax
add    $0x18,%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
@EAGAIN = global i32 0
@EPIPE = global i32 0
define i64 @atomicio(i32 (i32, i8*, i64)* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i32 (i32, i8*, i64)*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 (i32, i8*, i64)* %0, i32 (i32, i8*, i64)** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i8*, i8** %8
store i8* %13, i8** %10
store i64 0, i64* %11
br label %14
14: ; preds = %47, %37, %4
%15 = load i64, i64* %9
%16 = load i64, i64* %11
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %48
18: ; preds = %14
%19 = load i32 (i32, i8*, i64)*, i32 (i32, i8*, i64)** %6
%20 = load i32, i32* %7
%21 = load i8*, i8** %10
%22 = load i64, i64* %11
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %9
%25 = load i64, i64* %11
%26 = sub i64 %24, %25
%27 = call i32 %19(i32 %20, i8* %23, i64 %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
switch i32 %28, label %42 [
i32 -1, label %29
i32 0, label %39
]
29: ; preds = %18
%30 = load i32, i32* @errno
%31 = load i32, i32* @EINTR
%32 = icmp eq i32 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = load i32, i32* @EAGAIN
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
br label %14
38: ; preds = %33
store i64 0, i64* %5
br label %50
39: ; preds = %18
%40 = load i32, i32* @EPIPE
store i32 %40, i32* @errno
%41 = load i64, i64* %11
store i64 %41, i64* %5
br label %50
42: ; preds = %18
%43 = load i32, i32* %12
%44 = sext i32 %43 to i64
%45 = load i64, i64* %11
%46 = add i64 %45, %44
store i64 %46, i64* %11
br label %47
47: ; preds = %42
br label %14
48: ; preds = %14
%49 = load i64, i64* %11
store i64 %49, i64* %5
br label %50
50: ; preds = %48, %39, %38
%51 = load i64, i64* %5
ret i64 %51
}

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