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

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


x86 assembly (clang, -O0):
<qcount_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1179 <qcount_read+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <qcount_read+0x83>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1040 <read@plt>
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    11bd <qcount_read+0x7d>
mov    -0x38(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
call   1050 <atoll@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qcount_read>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     118e <qcount_read+0x4e>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1040 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1030 <close@plt>
test   %rbx,%rbx
je     118c <qcount_read+0x4c>
movb   $0x0,(%rsp,%rbx,1)
mov    %rsp,%rdi
call   1050 <atoll@plt>
mov    %eax,%ebx
jmp    118e <qcount_read+0x4e>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @qcount_read(i8* nocapture %0) {
%2 = alloca [21 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 [21 x i8], [21 x i8]* %2, i64 0, i64 0
%8 = call i64 @read(i32 %4, i8* %7, i32 20)
%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 [21 x i8], [21 x i8]* %2, i64 0, i64 %8
store i8 0, i8* %12
%13 = call i32 @atoll(i8* %7)
br label %14
14: ; preds = %11, %6
%15 = phi i32 [ %13, %11 ], [ 0, %6 ]
br label %16
16: ; preds = %1, %14
%17 = phi i32 [ %15, %14 ], [ 0, %1 ]
ret i32 %17
}