#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 */
struct stat {
  int st_size;
};
struct dirent {
  int dummy;
};

/* Variables and functions */
int _DIRENT_MINSIZE(struct dirent *);
int fstat(int, struct stat *);
size_t dirsize(int fd, size_t olen) {
  struct stat stb;
  size_t nlen;
  if (fstat(fd, &stb) == -1)
    return 0;
  nlen = (size_t)(stb.st_size / _DIRENT_MINSIZE((struct dirent *)0));
  if (nlen == 0)
    nlen = (size_t)(stb.st_size ? stb.st_size : 10);
  return olen + nlen;
}


x86 assembly (clang, -O0):
<dirsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1040 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1151 <dirsize+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11b0 <dirsize+0x90>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1030 <_DIRENT_MINSIZE@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
cltq
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <dirsize+0x84>
cmpl   $0x0,-0x20(%rbp)
je     118e <dirsize+0x6e>
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    119b <dirsize+0x7b>
mov    $0xa,%eax
mov    %eax,-0x30(%rbp)
jmp    119b <dirsize+0x7b>
mov    -0x30(%rbp),%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirsize>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rsp,%rsi
call   1040 <fstat@plt>
cmp    $0xffffffff,%eax
je     115b <dirsize+0x3b>
mov    (%rsp),%ebp
xor    %edi,%edi
call   1030 <_DIRENT_MINSIZE@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
test   %ebp,%ebp
mov    $0xa,%ecx
cmovne %ebp,%ecx
test   %eax,%eax
cmovne %eax,%ecx
movslq %ecx,%rax
add    %rbx,%rax
jmp    115d <dirsize+0x3d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
define i64 @dirsize(i32 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
%8 = load i32, i32* %4
%9 = call i32 @fstat(i32 %8, %struct.stat* %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i64 0, i64* %3
br label %35
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @_DIRENT_MINSIZE(%struct.dirent* null)
%16 = sdiv i32 %14, %15
%17 = sext i32 %16 to i64
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %12
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
br label %28
27: ; preds = %20
br label %28
28: ; preds = %27, %24
%29 = phi i32 [ %26, %24 ], [ 10, %27 ]
%30 = sext i32 %29 to i64
store i64 %30, i64* %7
br label %31
31: ; preds = %28, %12
%32 = load i64, i64* %5
%33 = load i64, i64* %7
%34 = add i64 %32, %33
store i64 %34, i64* %3
br label %35
35: ; preds = %31, %11
%36 = load i64, i64* %3
ret i64 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
define i64 @dirsize(i32 %0, i64 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @fstat(i32 %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @_DIRENT_MINSIZE(%struct.dirent* null)
%11 = sdiv i32 %9, %10
%12 = icmp eq i32 %11, 0
%13 = icmp eq i32 %9, 0
%14 = select i1 %13, i32 10, i32 %9
%15 = select i1 %12, i32 %14, i32 %11
%16 = sext i32 %15 to i64
%17 = add i64 %16, %1
br label %18
18: ; preds = %2, %7
%19 = phi i64 [ %17, %7 ], [ 0, %2 ]
ret i64 %19
}