#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 */

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ STDOUT_FILENO;
int fcntl(int, int /*<<< orphan*/, ...);
int read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void nonblocking_cat(int fd) {
  char buffer[4096];
  int n;
  char *prompt = "|/-\\";
  int i = 0;
  fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
  while ((n = read(fd, buffer, 4096)) != 0) {
    if (n > 0) {
      write(STDOUT_FILENO, buffer, n);
      continue;
    }
    write(STDOUT_FILENO, &(prompt[i]), 1);
    write(STDOUT_FILENO, "\b", 1);
    i++;
    if (i == 4)
      i = 0;
    usleep(100000);
  }
}


x86 assembly (clang, -O0):
<nonblocking_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeab(%rip),%rax
mov    %rax,-0x1020(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x102c(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x102c(%rbp),%edi
mov    -0x1028(%rbp),%esi
mov    %eax,%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
je     1272 <nonblocking_cat+0x132>
cmpl   $0x0,-0x1014(%rbp)
jle    11ff <nonblocking_cat+0xbf>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1040 <write@plt>
jmp    11af <nonblocking_cat+0x6f>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%rsi
movslq -0x1024(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
cmpl   $0x4,-0x1024(%rbp)
jne    1263 <nonblocking_cat+0x123>
movl   $0x0,-0x1024(%rbp)
mov    $0x186a0,%edi
call   1060 <usleep@plt>
jmp    11af <nonblocking_cat+0x6f>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nonblocking_cat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%ebp
mov    0x2e76(%rip),%rax
mov    (%rax),%ebx
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e6d(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1226 <nonblocking_cat+0xe6>
mov    0x2e50(%rip),%r12
mov    %rsp,%rbx
lea    0xe56(%rip),%r13
lea    0xe54(%rip),%r14
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
test   %eax,%eax
jle    11e7 <nonblocking_cat+0xa7>
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    11c0 <nonblocking_cat+0x80>
jmp    1226 <nonblocking_cat+0xe6>
movslq %r15d,%r15
lea    (%r15,%r13,1),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    (%r12),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%r15d
cmp    $0x4,%r15d
mov    $0x0,%eax
cmove  %eax,%r15d
mov    $0x186a0,%edi
call   1060 <usleep@plt>
jmp    11d2 <nonblocking_cat+0x92>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"|/-\\\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [2 x i8] c"\08\00"
define void @nonblocking_cat(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
store i32 0, i32* %6
%7 = load i32, i32* %2
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* %2
%10 = load i32, i32* @F_GETFL
%11 = call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %10)
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8, i32 %13)
br label %15
15: ; preds = %42, %23, %1
%16 = load i32, i32* %2
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 4096)
store i32 %18, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %44
20: ; preds = %15
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* @STDOUT_FILENO
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%26 = load i32, i32* %4
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
br label %15
28: ; preds = %20
%29 = load i32, i32* @STDOUT_FILENO
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = call i32 @write(i32 %29, i8* %33, i32 1)
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, 4
br i1 %40, label %41, label %42
41: ; preds = %28
store i32 0, i32* %6
br label %42
42: ; preds = %41, %28
%43 = call i32 @usleep(i32 100000)
br label %15
44: ; preds = %15
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"|/-\\\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [2 x i8] c"\08\00"
define void @nonblocking_cat(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @F_SETFL
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %5)
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %4, i32 %8)
%10 = call i32 @read(i32 %0, i8* %3, i32 4096)
%11 = icmp eq i32 %10, 0
br i1 %11, label %35, label %12
12: ; preds = %1, %23
%13 = phi i32 [ %33, %23 ], [ %10, %1 ]
%14 = phi i32 [ %31, %23 ], [ 0, %1 ]
br label %15
15: ; preds = %12, %19
%16 = phi i32 [ %13, %12 ], [ %21, %19 ]
%17 = icmp sgt i32 %16, 0
%18 = load i32, i32* @STDOUT_FILENO
br i1 %17, label %19, label %23
19: ; preds = %15
%20 = call i32 @write(i32 %18, i8* %3, i32 %16)
%21 = call i32 @read(i32 %0, i8* %3, i32 4096)
%22 = icmp eq i32 %21, 0
br i1 %22, label %35, label %15
23: ; preds = %15
%24 = sext i32 %14 to i64
%25 = getelementptr inbounds [5 x i8], [5 x i8]* @.str, i64 0, i64 %24
%26 = tail call i32 @write(i32 %18, i8* %25, i32 1)
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @write(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%29 = add nsw i32 %14, 1
%30 = icmp eq i32 %29, 4
%31 = select i1 %30, i32 0, i32 %29
%32 = tail call i32 @usleep(i32 100000)
%33 = call i32 @read(i32 %0, i8* %3, i32 4096)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %12
35: ; preds = %23, %19, %1
ret void
}