#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_RDWR;
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
char *strerror(int /*<<< orphan*/);
int main(void) {
  char buff[1024];
  int fd;
  int ret;
  fd = open("/dev/ringb", O_RDWR);
  if (fd < 0) {
    printf("open device faied: %s\n", strerror(errno));
  }
  while (1) {
    ret = read(fd, buff, 4);
    buff[ret] = 0;
    printf("read %d char: %s\n", ret, buff);
    sleep(3);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    11a9 <main+0x59>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ae <main+0x5e>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x418(%rbp)
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x418(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
jmp    11ae <main+0x5e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1195 <main+0x45>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe80(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rbx
lea    0xe83(%rip),%r14
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
movslq %eax,%rsi
movb   $0x0,(%rsp,%rsi,1)
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x3,%edi
call   1070 <sleep@plt>
jmp    11a0 <main+0x50>

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

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ringb\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"open device faied: %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"read %d char: %s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %6, %0
br label %11
11: ; preds = %10, %11
%12 = call i32 @read(i32 %4, i8* %2, i32 4)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %2)
%16 = call i32 @sleep(i32 3)
br label %11
}