#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 */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
size_t read(int, char *, int);
int _q_countread(const char *filepath) {
  int fd = open(filepath, O_RDONLY, 0);
  if (fd < 0)
    return 0;
  char buf[10 + 1];
  ssize_t readed = read(fd, buf, (sizeof(buf) - 1));
  close(fd);
  if (readed > 0) {
    buf[readed] = '\0';
    return atoi(buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<_q_countread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1179 <_q_countread+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <_q_countread+0x82>
mov    -0x14(%rbp),%edi
lea    -0x1f(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x28(%rbp)
jbe    11bb <_q_countread+0x7b>
mov    -0x28(%rbp),%rax
movb   $0x0,-0x1f(%rbp,%rax,1)
lea    -0x1f(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    11c2 <_q_countread+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_q_countread>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1193 <_q_countread+0x53>
mov    %eax,%ebp
lea    0xd(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1040 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
test   %rbx,%rbx
je     1191 <_q_countread+0x51>
movb   $0x0,0xd(%rsp,%rbx,1)
lea    0xd(%rsp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
jmp    1193 <_q_countread+0x53>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @_q_countread(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [11 x i8]
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %27
13: ; preds = %1
%14 = load i32, i32* %4
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 10)
store i64 %16, i64* %6
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
%19 = load i64, i64* %6
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i64, i64* %6
%23 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %2
br label %27
26: ; preds = %13
store i32 0, i32* %2
br label %27
27: ; preds = %26, %21, %12
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @_q_countread(i8* nocapture %0) {
%2 = alloca [11 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %1
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%8 = call i64 @read(i32 %4, i8* %7, i32 10)
%9 = tail call i32 @close(i32 %4)
%10 = icmp eq i64 %8, 0
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 %8
store i8 0, i8* %12
%13 = call i32 @atoi(i8* %7)
br label %14
14: ; preds = %6, %11
%15 = phi i32 [ %13, %11 ], [ 0, %6 ]
br label %16
16: ; preds = %1, %14
%17 = phi i32 [ %15, %14 ], [ 0, %1 ]
ret i32 %17
}