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

/* Variables and functions */
void *POLLIN;
void *POLLOUT;
int poll(struct pollfd *, int, int);
int read(int, int /*<<< orphan*/ *, int);
int write(int, void *, int);
int suart_io(int fd, void *outbuf, void *inbuf, int tx, int rx, int *rxed,
             int timeout) {
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = POLLIN;
  while (poll(&pfd, 1, 1) != 0)
    if (read(fd, &((uint8_t *)inbuf)[0], 1) == 0)
      break;
  pfd.events = POLLOUT;
  int ok = poll(&pfd, 1, timeout);
  if (ok <= 0)
    return -9;
  int n = write(fd, outbuf, tx);
  if (n < tx)
    return -2;
  *rxed = 0;
  pfd.events = POLLIN;
  do {
    ok = poll(&pfd, 1, timeout);
    if (ok <= 0)
      break;
    n = read(fd, &((uint8_t *)inbuf)[*rxed], rx - (*rxed));
    (*rxed) += n;
  } while (*rxed < rx);
  return 0;
}


x86 assembly (clang, -O0):
<suart_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1050 <poll@plt>
cmp    $0x0,%eax
je     11a2 <suart_io+0x72>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%eax
jne    119d <suart_io+0x6d>
jmp    11a2 <suart_io+0x72>
jmp    1165 <suart_io+0x35>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x10(%rbp),%edx
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     11da <suart_io+0xaa>
movl   $0xfffffff7,-0x4(%rbp)
jmp    1286 <suart_io+0x156>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1204 <suart_io+0xd4>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1286 <suart_io+0x156>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x10(%rbp),%edx
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     123f <suart_io+0x10f>
jmp    127f <suart_io+0x14f>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rax
sub    (%rax),%edx
call   1040 <read@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
cmp    -0x20(%rbp),%eax
jl     121c <suart_io+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suart_io>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %r8d,%r14d
mov    %ecx,%r13d
mov    %rdx,%rbp
mov    %rsi,0x10(%rsp)
mov    %edi,%ebx
mov    %edi,(%rsp)
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     1199 <suart_io+0x69>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jne    1170 <suart_io+0x40>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    0x50(%rsp),%edx
call   1050 <poll@plt>
test   %eax,%eax
jle    123c <suart_io+0x10c>
mov    %ebx,%edi
mov    0x10(%rsp),%rsi
mov    %r13d,%edx
call   1030 <write@plt>
mov    %r13d,%ecx
mov    $0xfffffffe,%r13d
cmp    %ecx,%eax
jl     1242 <suart_io+0x112>
movl   $0x0,(%r15)
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    $0x1,%esi
mov    0x50(%rsp),%edx
call   1050 <poll@plt>
test   %eax,%eax
jle    1242 <suart_io+0x112>
movslq (%r15),%rax
lea    0x0(,%rax,4),%rsi
add    %rbp,%rsi
mov    %r14d,%edx
sub    %eax,%edx
mov    %ebx,%edi
call   1040 <read@plt>
add    (%r15),%eax
mov    %eax,(%r15)
cmp    %r14d,%eax
jl     1200 <suart_io+0xd0>
jmp    1242 <suart_io+0x112>
mov    $0xfffffff7,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i8* }
@POLLIN = global i8* null
@POLLOUT = global i8* null
define i32 @suart_io(i32 %0, i8* %1, i8* %2, i32 %3, i32 %4, i32* %5, i32 %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca %struct.pollfd
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %9
store i8* %1, i8** %10
store i8* %2, i8** %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32* %5, i32** %14
store i32 %6, i32* %15
%19 = load i32, i32* %9
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i8*, i8** @POLLIN
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i8* %21, i8** %22
br label %23
23: ; preds = %34, %7
%24 = call i32 @poll(%struct.pollfd* %16, i32 1, i32 1)
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23
%27 = load i32, i32* %9
%28 = load i8*, i8** %11
%29 = bitcast i8* %28 to i32*
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = call i32 @read(i32 %27, i32* %30, i32 1)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
br label %35
34: ; preds = %26
br label %23
35: ; preds = %33, %23
%36 = load i8*, i8** @POLLOUT
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i8* %36, i8** %37
%38 = load i32, i32* %15
%39 = call i32 @poll(%struct.pollfd* %16, i32 1, i32 %38)
store i32 %39, i32* %17
%40 = load i32, i32* %17
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 -9, i32* %8
br label %85
43: ; preds = %35
%44 = load i32, i32* %9
%45 = load i8*, i8** %10
%46 = load i32, i32* %12
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
store i32 %47, i32* %18
%48 = load i32, i32* %18
%49 = load i32, i32* %12
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %43
store i32 -2, i32* %8
br label %85
52: ; preds = %43
%53 = load i32*, i32** %14
store i32 0, i32* %53
%54 = load i8*, i8** @POLLIN
%55 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i8* %54, i8** %55
br label %56
56: ; preds = %79, %52
%57 = load i32, i32* %15
%58 = call i32 @poll(%struct.pollfd* %16, i32 1, i32 %57)
store i32 %58, i32* %17
%59 = load i32, i32* %17
%60 = icmp sle i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %56
br label %84
62: ; preds = %56
%63 = load i32, i32* %9
%64 = load i8*, i8** %11
%65 = bitcast i8* %64 to i32*
%66 = load i32*, i32** %14
%67 = load i32, i32* %66
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %65, i64 %68
%70 = load i32, i32* %13
%71 = load i32*, i32** %14
%72 = load i32, i32* %71
%73 = sub nsw i32 %70, %72
%74 = call i32 @read(i32 %63, i32* %69, i32 %73)
store i32 %74, i32* %18
%75 = load i32, i32* %18
%76 = load i32*, i32** %14
%77 = load i32, i32* %76
%78 = add nsw i32 %77, %75
store i32 %78, i32* %76
br label %79
79: ; preds = %62
%80 = load i32*, i32** %14
%81 = load i32, i32* %80
%82 = load i32, i32* %13
%83 = icmp slt i32 %81, %82
br i1 %83, label %56, label %84
84: ; preds = %79, %61
store i32 0, i32* %8
br label %85
85: ; preds = %84, %51, %42
%86 = load i32, i32* %8
ret i32 %86
}

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