#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int fd;
  int c;
  int ret;
  char buf[5];
  fd = open("/dev/userspace", O_RDONLY);
  if (fd <= 0) {
    perror("error opening device file");
  }
  for (c = 0; c < 1000; c++) {
    memset(buf, 0x00, 4);
    ret = read(fd, buf, 4);
    buf[4] = '\0';
    if (ret > 0)
      printf("%s %d\n", buf, ret);
    else
      printf("No data\n");
    sleep(1);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11b4 <main+0x44>
lea    0xe60(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3e8,-0x18(%rbp)
jge    123a <main+0xca>
lea    -0x21(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1214 <main+0xa4>
lea    -0x21(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1222 <main+0xb2>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bb <main+0x4b>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    1237 <main+0xb7>
mov    $0x3e8,%ebp
lea    0xb(%rsp),%rbx
lea    0xe6c(%rip),%r14
lea    0xe6c(%rip),%r15
jmp    11e7 <main+0x67>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
add    $0xffffffff,%ebp
je     1220 <main+0xa0>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <read@plt>
movb   $0x0,0xf(%rsp)
test   %eax,%eax
jle    11d0 <main+0x50>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d8 <main+0x58>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
jmp    11ac <main+0x2c>

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/userspace\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"error opening device file\00"
@.str.2 = [7 x i8] c"%s %d\0A\00"
@.str.3 = [9 x i8] c"No data\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [5 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
store i32 0, i32* %7
br label %17
17: ; preds = %37, %16
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 1000
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 4)
%23 = load i32, i32* %6
%24 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 4)
store i32 %25, i32* %8
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 4
store i8 0, i8* %26
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%31 = load i32, i32* %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %31)
br label %35
33: ; preds = %20
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = call i32 @sleep(i32 1)
br label %37
37: ; preds = %35
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %17
40: ; preds = %17
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/userspace\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"error opening device file\00"
@.str.2 = [7 x i8] c"%s %d\0A\00"
@str = [8 x i8] c"No data\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 4
br label %12
12: ; preds = %10, %21
%13 = phi i32 [ 0, %10 ], [ %23, %21 ]
%14 = call i32 @memset(i8* %4, i32 0, i32 4)
%15 = call i32 @read(i32 %6, i8* %4, i32 4)
store i8 0, i8* %11
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %15)
br label %21
19: ; preds = %12
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
%22 = call i32 @sleep(i32 1)
%23 = add nuw nsw i32 %13, 1
%24 = icmp eq i32 %23, 1000
br i1 %24, label %25, label %12
25: ; preds = %21
%26 = call i32 @close(i32 %6)
ret i32 0
}