#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 int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ *opendir(char *);
scalar_t__ readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *dircount(void) {
  int i;
  char *count = (char *)malloc(1024 * sizeof(char));
  DIR *dirp;
  for (i = -2, dirp = opendir("."); readdir(dirp); i++)
    ;
  sprintf(count, "%d", i);
  return count;
}


x86 assembly (clang, -O0):
<dircount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0xfffffffe,-0x4(%rbp)
lea    0xe9c(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <readdir@plt>
cmp    $0x0,%rax
je     1193 <dircount+0x53>
jmp    1185 <dircount+0x45>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    116d <dircount+0x2d>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe61(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dircount>:
push   %rbp
push   %r14
push   %rbx
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xea8(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    $0xfffffffd,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1040 <readdir@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1170 <dircount+0x30>
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%d\00"
define i8* @dircount() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = call i64 @malloc(i32 1024)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %2
store i32 -2, i32* %1
%6 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
br label %7
7: ; preds = %12, %0
%8 = load i32*, i32** %3
%9 = call i64 @readdir(i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
br label %12
12: ; preds = %11
%13 = load i32, i32* %1
%14 = add nsw i32 %13, 1
store i32 %14, i32* %1
br label %7
15: ; preds = %7
%16 = load i8*, i8** %2
%17 = load i32, i32* %1
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"%d\00"
define i8* @dircount() {
%1 = tail call i64 @malloc(i32 1024)
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %3
3: ; preds = %3, %0
%4 = phi i32 [ -2, %0 ], [ %7, %3 ]
%5 = tail call i64 @readdir(i32* %2)
%6 = icmp eq i64 %5, 0
%7 = add nsw i32 %4, 1
br i1 %6, label %8, label %3
8: ; preds = %3
%9 = inttoptr i64 %1 to i8*
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %4)
ret i8* %9
}