#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*/ fflush(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ stdout;
void second_test(void) {
  int fd;
  unsigned int counter;
  fd = open("/dev/second", O_RDONLY);
  if (fd > 0) {
    while (1) {
      read(fd, &counter, sizeof(unsigned int));
      printf("counter: %d\r", counter);
      fflush(stdout);
    }
  } else {
    perror("");
  }
  close(fd);
}


x86 assembly (clang, -O0):
<second_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11c2 <second_test+0x62>
jmp    118f <second_test+0x2f>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    118f <second_test+0x2f>
lea    0xe4f(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<second_test>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11c6 <second_test+0x66>
lea    0x4(%rsp),%r14
lea    0xe7d(%rip),%r15
mov    0x2e42(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1030 <printf@plt>
mov    (%r12),%edi
call   1060 <fflush@plt>
jmp    11a0 <second_test+0x40>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <close@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/second\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"counter: %d\0D\00"
@stdout = global i32 0
@.str.2 = [1 x i8] zeroinitializer
define void @second_test() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %0
br label %8
8: ; preds = %7, %8
%9 = load i32, i32* %1
%10 = call i32 @read(i32 %9, i32* %2, i32 4)
%11 = load i32, i32* %2
%12 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
br label %8
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/second\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"counter: %d\0D\00"
@stdout = global i32 0
@.str.2 = [1 x i8] zeroinitializer
define void @second_test() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0, %6
%7 = call i32 @read(i32 %4, i32* %1, i32 4)
%8 = load i32, i32* %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @stdout
%11 = tail call i32 @fflush(i32 %10)
br label %6
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @close(i32 %4)
ret void
}