#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 */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int getFDCountOfProccess(char *directory, char *pid) {
  char *directoryFinal = (char *)malloc(sizeof(char) * (256 + 1));
  strcpy(directoryFinal, directory);
  strcat(directoryFinal, "/");
  strcat(directoryFinal, pid);
  strcat(directoryFinal, "/fd");
  DIR *dir = opendir(directoryFinal);
  int countFD = -2;
  if (dir != NULL) {
    while (readdir(dir) != NULL) {
      countFD++;
    }
    closedir(dir);
  } else {
    countFD = 0;
  }
  free(directoryFinal);
  return countFD;
}


x86 assembly (clang, -O0):
<getFDCountOfProccess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x101,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
movl   $0xfffffffe,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     121b <getFDCountOfProccess+0xab>
jmp    11ec <getFDCountOfProccess+0x7c>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
cmp    $0x0,%rax
je     120d <getFDCountOfProccess+0x9d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ec <getFDCountOfProccess+0x7c>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
jmp    1222 <getFDCountOfProccess+0xb2>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFDCountOfProccess>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    $0x101,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xe67(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
lea    0xe4f(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     11ea <getFDCountOfProccess+0x7a>
mov    %rax,%rbx
mov    $0xfffffffd,%ebp
mov    %rbx,%rdi
call   1070 <readdir@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    11d0 <getFDCountOfProccess+0x60>
mov    %rbx,%rdi
call   1060 <closedir@plt>
jmp    11ec <getFDCountOfProccess+0x7c>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"/fd\00"
define i32 @getFDCountOfProccess(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i64 @malloc(i32 257)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = load i8*, i8** %3
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = load i8*, i8** %5
%14 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = load i8*, i8** %5
%16 = load i8*, i8** %4
%17 = call i32 @strcat(i8* %15, i8* %16)
%18 = load i8*, i8** %5
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = load i8*, i8** %5
%21 = call i32* @opendir(i8* %20)
store i32* %21, i32** %6
store i32 -2, i32* %7
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %35
24: ; preds = %2
br label %25
25: ; preds = %29, %24
%26 = load i32*, i32** %6
%27 = call i32* @readdir(i32* %26)
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %25
32: ; preds = %25
%33 = load i32*, i32** %6
%34 = call i32 @closedir(i32* %33)
br label %36
35: ; preds = %2
store i32 0, i32* %7
br label %36
36: ; preds = %35, %32
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"/fd\00"
define i32 @getFDCountOfProccess(i8* %0, i8* %1) {
%3 = tail call i64 @malloc(i32 257)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @strcpy(i8* %4, i8* %0)
%6 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @strcat(i8* %4, i8* %1)
%8 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32* @opendir(i8* %4)
%10 = icmp eq i32* %9, null
br i1 %10, label %18, label %11
11: ; preds = %2, %11
%12 = phi i32 [ %15, %11 ], [ -2, %2 ]
%13 = tail call i32* @readdir(i32* %9)
%14 = icmp eq i32* %13, null
%15 = add nsw i32 %12, 1
br i1 %14, label %16, label %11
16: ; preds = %11
%17 = tail call i32 @closedir(i32* %9)
br label %18
18: ; preds = %2, %16
%19 = phi i32 [ %12, %16 ], [ 0, %2 ]
%20 = tail call i32 @free(i8* %4)
ret i32 %19
}