#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 */
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
typedef int ssize_t;
typedef int f;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ ENOBUFS;
int /*<<< orphan*/ EPIPE;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ POLLOUT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ poll(struct pollfd *, int, int);
ssize_t read(int, void *, size_t);
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;
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = f == read ? POLLIN : POLLOUT;
  while (n > pos) {
    res = (f)(fd, s + pos, n - pos);
    switch (res) {
    case -1:
      if (errno == EINTR)
        continue;
      if ((errno == EAGAIN) || (errno == ENOBUFS)) {
        (void)poll(&pfd, 1, -1);
        continue;
      }
      return 0;
    case 0:
      errno = EPIPE;
      return pos;
    default:
      pos += (size_t)res;
    }
  }
  return (pos);
}


x86 assembly (clang, -O0):
<atomicio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e7c(%rip),%rax
cmp    %rax,-0x10(%rbp)
jne    115f <atomicio+0x4f>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    116b <atomicio+0x5b>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    1263 <atomicio+0x153>
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,-0x50(%rbp)
sub    $0xffffffff,%eax
je     11c1 <atomicio+0xb1>
jmp    11b1 <atomicio+0xa1>
mov    -0x50(%rbp),%eax
test   %eax,%eax
je     1233 <atomicio+0x123>
jmp    1252 <atomicio+0x142>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
jne    11de <atomicio+0xce>
jmp    1171 <atomicio+0x61>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%eax
je     120e <atomicio+0xfe>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%eax
jne    1226 <atomicio+0x116>
lea    -0x48(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1030 <poll@plt>
jmp    1171 <atomicio+0x61>
movq   $0x0,-0x8(%rbp)
jmp    126b <atomicio+0x15b>
mov    0x2d8e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d75(%rip),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126b <atomicio+0x15b>
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1171 <atomicio+0x61>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomicio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,0x10(%rsp)
cmp    0x2e8f(%rip),%rdi
jne    113c <atomicio+0x2c>
mov    0x2e9e(%rip),%rax
jmp    1143 <atomicio+0x33>
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
test   %r15,%r15
je     11f6 <atomicio+0xe6>
mov    0x2e5f(%rip),%r13
mov    0x2e50(%rip),%r12
xor    %eax,%eax
mov    %r15,0x20(%rsp)
mov    %rdx,0x18(%rsp)
lea    (%rdx,%rax,1),%r14
mov    %rax,0x8(%rsp)
sub    %rax,%r15
jmp    1194 <atomicio+0x84>
nopw   0x0(%rax,%rax,1)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1030 <poll@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   *%rbx
cmp    $0xffffffff,%eax
jne    11d0 <atomicio+0xc0>
mov    0x0(%r13),%eax
cmp    (%r12),%eax
je     1194 <atomicio+0x84>
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
je     1180 <atomicio+0x70>
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
je     1180 <atomicio+0x70>
jmp    11f6 <atomicio+0xe6>
cs nopw 0x0(%rax,%rax,1)
nop
test   %eax,%eax
je     11fa <atomicio+0xea>
cltq
mov    0x8(%rsp),%rcx
add    %rax,%rcx
mov    %rcx,%rax
mov    0x20(%rsp),%r15
cmp    %r15,%rcx
mov    0x18(%rsp),%rdx
jb     116c <atomicio+0x5c>
jmp    120c <atomicio+0xfc>
xor    %eax,%eax
jmp    120c <atomicio+0xfc>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x0(%r13)
mov    0x8(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@ENOBUFS = 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
%13 = alloca %struct.pollfd
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
%14 = load i8*, i8** %8
store i8* %14, i8** %10
store i64 0, i64* %11
%15 = load i32, i32* %7
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32 (i32, i8*, i64)*, i32 (i32, i8*, i64)** %6
%18 = icmp eq i32 (i32, i8*, i64)* %17, @read
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* @POLLIN
br label %23
21: ; preds = %4
%22 = load i32, i32* @POLLOUT
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ %20, %19 ], [ %22, %21 ]
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 1
store i32 %24, i32* %25
br label %26
26: ; preds = %65, %54, %45, %23
%27 = load i64, i64* %9
%28 = load i64, i64* %11
%29 = icmp ugt i64 %27, %28
br i1 %29, label %30, label %66
30: ; preds = %26
%31 = load i32 (i32, i8*, i64)*, i32 (i32, i8*, i64)** %6
%32 = load i32, i32* %7
%33 = load i8*, i8** %10
%34 = load i64, i64* %11
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i64, i64* %9
%37 = load i64, i64* %11
%38 = sub i64 %36, %37
%39 = call i32 %31(i32 %32, i8* %35, i64 %38)
store i32 %39, i32* %12
%40 = load i32, i32* %12
switch i32 %40, label %60 [
i32 -1, label %41
i32 0, label %57
]
41: ; preds = %30
%42 = load i32, i32* @errno
%43 = load i32, i32* @EINTR
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
br label %26
46: ; preds = %41
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i32, i32* @errno
%52 = load i32, i32* @ENOBUFS
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %50, %46
%55 = call i32 @poll(%struct.pollfd* %13, i32 1, i32 -1)
br label %26
56: ; preds = %50
store i64 0, i64* %5
br label %68
57: ; preds = %30
%58 = load i32, i32* @EPIPE
store i32 %58, i32* @errno
%59 = load i64, i64* %11
store i64 %59, i64* %5
br label %68
60: ; preds = %30
%61 = load i32, i32* %12
%62 = sext i32 %61 to i64
%63 = load i64, i64* %11
%64 = add i64 %63, %62
store i64 %64, i64* %11
br label %65
65: ; preds = %60
br label %26
66: ; preds = %26
%67 = load i64, i64* %11
store i64 %67, i64* %5
br label %68
68: ; preds = %66, %57, %56
%69 = load i64, i64* %5
ret i64 %69
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@ENOBUFS = global i32 0
@EPIPE = global i32 0
define i64 @atomicio(i32 (i32, i8*, i64)* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %1, i32* %7
%8 = icmp eq i32 (i32, i8*, i64)* %0, @read
%9 = load i32, i32* @POLLIN
%10 = load i32, i32* @POLLOUT
%11 = select i1 %8, i32 %9, i32 %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = icmp eq i64 %3, 0
br i1 %13, label %39, label %14
14: ; preds = %4, %35
%15 = phi i64 [ %37, %35 ], [ 0, %4 ]
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = sub i64 %3, %15
br label %18
18: ; preds = %32, %14
%19 = call i32 %0(i32 %1, i8* %16, i64 %17)
switch i32 %19, label %35 [
i32 -1, label %20
i32 0, label %33
]
20: ; preds = %18
%21 = load i32, i32* @errno
%22 = load i32, i32* @EINTR
%23 = icmp eq i32 %21, %22
br i1 %23, label %32, label %24
24: ; preds = %20
%25 = load i32, i32* @EAGAIN
%26 = icmp eq i32 %21, %25
%27 = load i32, i32* @ENOBUFS
%28 = icmp eq i32 %21, %27
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %30, label %39
30: ; preds = %24
%31 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 -1)
br label %32
32: ; preds = %30, %20
br label %18
33: ; preds = %18
%34 = load i32, i32* @EPIPE
store i32 %34, i32* @errno
br label %39
35: ; preds = %18
%36 = sext i32 %19 to i64
%37 = add i64 %15, %36
%38 = icmp ult i64 %37, %3
br i1 %38, label %14, label %39
39: ; preds = %35, %24, %4, %33
%40 = phi i64 [ %15, %33 ], [ 0, %4 ], [ 0, %24 ], [ %37, %35 ]
ret i64 %40
}