#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ isSerialPortDev(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncat(char *, char *, int);
void SerialPortName(int portIndex, char *bsdPath, int maxPathSize) {
  DIR *dirPtr;
  struct dirent *entryPtr;
  int cnt = 0;
  *bsdPath = '\0';
  if (portIndex < 1)
    return;
  if ((dirPtr = opendir("/dev")) == NULL)
    return;
  while ((entryPtr = readdir(dirPtr)) != NULL) {
    if (isSerialPortDev(entryPtr->d_name))
      cnt++;
    if (cnt == portIndex) {
      strncat(bsdPath, "/dev/", maxPathSize);
      strncat(bsdPath, entryPtr->d_name, maxPathSize);
      closedir(dirPtr);
      return;
    }
  }
  closedir(dirPtr);
}


x86 assembly (clang, -O0):
<SerialPortName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x1,-0x4(%rbp)
jge    117f <SerialPortName+0x2f>
jmp    1227 <SerialPortName+0xd7>
lea    0xe7a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119e <SerialPortName+0x4e>
jmp    1227 <SerialPortName+0xd7>
jmp    11a3 <SerialPortName+0x53>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121e <SerialPortName+0xce>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1070 <isSerialPortDev@plt>
cmp    $0x0,%rax
je     11d9 <SerialPortName+0x89>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1219 <SerialPortName+0xc9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe12(%rip),%rsi
call   1040 <strncat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncat@plt>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1227 <SerialPortName+0xd7>
jmp    11a3 <SerialPortName+0x53>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SerialPortName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x0,(%rsi)
test   %edi,%edi
jle    11eb <SerialPortName+0x9b>
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r13d
lea    0xe8a(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11eb <SerialPortName+0x9b>
mov    %rax,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d5 <SerialPortName+0x85>
mov    %rax,%rbp
mov    (%rax),%rdi
call   1070 <isSerialPortDev@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
cmp    %r13d,%ebx
jne    1190 <SerialPortName+0x40>
lea    0xe4a(%rip),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <strncat@plt>
mov    0x0(%rbp),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <strncat@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <closedir@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"/dev/\00"
define void @SerialPortName(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %9
%10 = load i8*, i8** %5
store i8 0, i8* %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %3
br label %52
14: ; preds = %3
%15 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14
br label %52
18: ; preds = %14
br label %19
19: ; preds = %48, %18
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %8
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %49
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %8
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @isSerialPortDev(i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %32
32: ; preds = %29, %23
%33 = load i32, i32* %9
%34 = load i32, i32* %4
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i32 @strncat(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %5
%41 = load %struct.dirent*, %struct.dirent** %8
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = load i32, i32* %6
%45 = call i32 @strncat(i8* %40, i8* %43, i32 %44)
%46 = load i32*, i32** %7
%47 = call i32 @closedir(i32* %46)
br label %52
48: ; preds = %32
br label %19
49: ; preds = %19
%50 = load i32*, i32** %7
%51 = call i32 @closedir(i32* %50)
br label %52
52: ; preds = %49, %36, %17, %13
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [6 x i8] c"/dev/\00"
define void @SerialPortName(i32 %0, i8* %1, i32 %2) {
store i8 0, i8* %1
%4 = icmp slt i32 %0, 1
br i1 %4, label %27, label %5
5: ; preds = %3
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %27, label %8
8: ; preds = %5, %12
%9 = phi i32 [ %18, %12 ], [ 0, %5 ]
%10 = tail call %struct.dirent* @readdir(i32* %6)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %25, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i64 @isSerialPortDev(i8* %14)
%16 = icmp ne i64 %15, 0
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %9, %17
%19 = icmp eq i32 %18, %0
br i1 %19, label %20, label %8
20: ; preds = %12
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%22 = tail call i32 @strncat(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%23 = load i8*, i8** %21
%24 = tail call i32 @strncat(i8* %1, i8* %23, i32 %2)
br label %25
25: ; preds = %8, %20
%26 = tail call i32 @closedir(i32* %6)
br label %27
27: ; preds = %25, %5, %3
ret void
}