#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int fd;
  char buf;
  if ((fd = open("/dev/fake_mq", O_RDONLY)) < 0) {
    perror("open()");
    return 1;
  }
  while (1) {
    if (read(fd, &buf, 1) <= 0) {
      perror("read()");
      break;
    }
    printf("%d\n", (int)buf);
    sleep(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a8 <main+0x48>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1207 <main+0xa7>
jmp    11ad <main+0x4d>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jg     11d9 <main+0x79>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
jmp    11f8 <main+0x98>
movsbl -0x9(%rbp),%esi
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11ad <main+0x4d>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1195 <main+0x35>
lea    0xe84(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    1200 <main+0xa0>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11eb <main+0x8b>
lea    0xe67(%rip),%r14
lea    0xf(%rsp),%r15
nopl   0x0(%rax)
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11c0 <main+0x60>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/fake_mq\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %24
9: ; preds = %0
br label %10
10: ; preds = %9, %16
%11 = load i32, i32* %2
%12 = call i64 @read(i32 %11, i8* %3, i32 1)
%13 = icmp sle i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
16: ; preds = %10
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i32 @sleep(i32 1)
br label %10
21: ; preds = %14
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 0, i32* %1
br label %24
24: ; preds = %21, %7
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/fake_mq\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i64 @read(i32 %3, i8* %1, i32 1)
%7 = icmp slt i64 %6, 1
br i1 %7, label %10, label %13
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
10: ; preds = %13, %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @close(i32 %3)
br label %20
13: ; preds = %5, %13
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = tail call i32 @sleep(i32 1)
%18 = call i64 @read(i32 %3, i8* %1, i32 1)
%19 = icmp slt i64 %18, 1
br i1 %19, label %10, label %13
20: ; preds = %10, %8
%21 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %21
}