#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;
  scalar_t__ revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ POLLIN;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t);
int poll(struct pollfd *, int, int);
int read(int, void *, size_t);
int /*<<< orphan*/ usleep(int);
int read_from_printer(int fd, void *buf, size_t bufsize, int nonblocking) {
  int status;
  int retry = 10;
  struct pollfd ufds;
  memset(buf, 0, bufsize);
  if (nonblocking) {
    fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
  }
  do {
    ufds.fd = fd;
    ufds.events = POLLIN;
    ufds.revents = 0;
    if ((status = poll(&ufds, 1, 1000)) < 0) {
      break;
    }
    status = read(fd, buf, bufsize - 1);
    if ((status == 0) || ((status < 0) && (errno == EAGAIN))) {
      usleep(2000);
      status = 0;
    }
  } while ((status == 0) && (--retry != 0));
  return status;
}


x86 assembly (clang, -O0):
<read_from_printer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xa,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11c9 <read_from_printer+0x79>
mov    -0x4(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x4c(%rbp),%edx
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
or     %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
jmp    11ce <read_from_printer+0x7e>
mov    -0x4(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1060 <poll@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    120c <read_from_printer+0xbc>
jmp    1293 <read_from_printer+0x143>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1251 <read_from_printer+0x101>
cmpl   $0x0,-0x20(%rbp)
jge    1262 <read_from_printer+0x112>
mov    0x2d8a(%rip),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <read_from_printer+0x112>
mov    $0x7d0,%edi
call   1070 <usleep@plt>
movl   $0x0,-0x20(%rbp)
jmp    1267 <read_from_printer+0x117>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x4d(%rbp)
jne    1288 <read_from_printer+0x138>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
test   $0x1,%al
jne    11ce <read_from_printer+0x7e>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_printer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
test   %r15d,%r15d
je     11b0 <read_from_printer+0x60>
mov    0x2e51(%rip),%rax
mov    (%rax),%r15d
mov    0x2e5f(%rip),%rax
mov    (%rax),%r12d
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
or     %eax,%r12d
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
add    $0xffffffffffffffff,%r13
mov    $0xfffffff6,%ebx
mov    0x2e20(%rip),%r12
mov    %rsp,%r15
jmp    11de <read_from_printer+0x8e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x7d0,%edi
call   1070 <usleep@plt>
inc    %ebx
je     1233 <read_from_printer+0xe3>
mov    %ebp,(%rsp)
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1060 <poll@plt>
test   %eax,%eax
js     1235 <read_from_printer+0xe5>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   1050 <read@plt>
test   %eax,%eax
je     11d0 <read_from_printer+0x80>
jns    1235 <read_from_printer+0xe5>
mov    0x2da6(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2d84(%rip),%rdx
cmp    (%rdx),%rcx
je     11d0 <read_from_printer+0x80>
jmp    1235 <read_from_printer+0xe5>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
@POLLIN = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @read_from_printer(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.pollfd
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 10, i32* %10
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i32 @memset(i8* %12, i32 0, i64 %13)
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %4
%18 = load i32, i32* %5
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @O_NONBLOCK
%21 = load i32, i32* %5
%22 = load i32, i32* @F_GETFL
%23 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %22)
%24 = or i32 %20, %23
%25 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %24)
br label %26
26: ; preds = %17, %4
br label %27
27: ; preds = %61, %26
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @POLLIN
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 1
store i64 0, i64* %32
%33 = call i32 @poll(%struct.pollfd* %11, i32 1, i32 1000)
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
br label %63
36: ; preds = %27
%37 = load i32, i32* %5
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = sub i64 %39, 1
%41 = call i32 @read(i32 %37, i8* %38, i64 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %36
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @EAGAIN
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %47, %36
%52 = call i32 @usleep(i32 2000)
store i32 0, i32* %9
br label %53
53: ; preds = %51, %47, %44
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* %10
%59 = add nsw i32 %58, -1
store i32 %59, i32* %10
%60 = icmp ne i32 %59, 0
br label %61
61: ; preds = %57, %54
%62 = phi i1 [ false, %54 ], [ %60, %57 ]
br i1 %62, label %27, label %63
63: ; preds = %61, %35
%64 = load i32, i32* %9
ret i32 %64
}

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