#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct string_set {
  int dummy;
};
struct dirent {
  char *d_name;
};
struct TYPE_3__ {
  size_t rm_eo;
  int rm_so;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ REG_EXTENDED;
int REG_NOMATCH;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int regexec(int /*<<< orphan*/ *, char *, int, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ string_set_add(struct string_set *, char *);
struct string_set *string_set_create();
struct string_set *serial_port_enumerate(void) {
  struct string_set *ss;
  struct dirent *de;
  regex_t regex;
  int error;
  DIR *dir;
  error = regcomp(&regex, "^cu\\.(.+)$", REG_EXTENDED);
  if (error != 0)
    return (NULL);
  dir = opendir("/dev");
  if (dir == NULL) {
    regfree(&regex);
    return (NULL);
  }
  ss = string_set_create();
  if (ss == NULL) {
    regfree(&regex);
    closedir(dir);
    return (NULL);
  }
  while ((de = readdir(dir)) != NULL) {
    regmatch_t rm[2];
    error = regexec(&regex, de->d_name, sizeof rm / sizeof rm[0], rm, 0);
    if (error == REG_NOMATCH)
      continue;
    if (error != 0) {
      abort();
      continue;
    }
    de->d_name[rm[1].rm_eo] = '\0';
    string_set_add(ss, de->d_name + rm[1].rm_so);
  }
  regfree(&regex);
  closedir(dir);
  return (ss);
}


x86 assembly (clang, -O0):
<serial_port_enumerate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <regcomp@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11cb <serial_port_enumerate+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <serial_port_enumerate+0x149>
lea    0xe39(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fc <serial_port_enumerate+0x6c>
lea    -0x1c(%rbp),%rdi
call   1050 <regfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <serial_port_enumerate+0x149>
mov    $0x0,%al
call   10b0 <string_set_create@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1231 <serial_port_enumerate+0xa1>
lea    -0x1c(%rbp),%rdi
call   1050 <regfree@plt>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <serial_port_enumerate+0x149>
jmp    1236 <serial_port_enumerate+0xa6>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12bf <serial_port_enumerate+0x12f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    -0x50(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x2,%edx
xor    %r8d,%r8d
call   10a0 <regexec@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
jne    1283 <serial_port_enumerate+0xf3>
jmp    1236 <serial_port_enumerate+0xa6>
cmpl   $0x0,-0x20(%rbp)
je     1294 <serial_port_enumerate+0x104>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
call   1080 <string_set_add@plt>
jmp    1236 <serial_port_enumerate+0xa6>
lea    -0x1c(%rbp),%rdi
call   1050 <regfree@plt>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_port_enumerate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
lea    0xe52(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1070 <regcomp@plt>
test   %eax,%eax
je     11d1 <serial_port_enumerate+0x41>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1284 <serial_port_enumerate+0xf4>
mov    %rax,%r13
xor    %eax,%eax
call   10b0 <string_set_create@plt>
test   %rax,%rax
je     1293 <serial_port_enumerate+0x103>
mov    %rax,%r14
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     126d <serial_port_enumerate+0xdd>
mov    %rax,%rbx
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2dcb(%rip),%rbp
jmp    1230 <serial_port_enumerate+0xa0>
nop
mov    %r13,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     126d <serial_port_enumerate+0xdd>
mov    (%rbx),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   10a0 <regexec@plt>
cmp    0x0(%rbp),%eax
je     1220 <serial_port_enumerate+0x90>
test   %eax,%eax
jne    12aa <serial_port_enumerate+0x11a>
mov    (%rbx),%rax
mov    0x20(%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
movslq 0x28(%rsp),%rsi
add    (%rbx),%rsi
mov    %r14,%rdi
call   1080 <string_set_add@plt>
jmp    1220 <serial_port_enumerate+0x90>
lea    0xc(%rsp),%rdi
call   1050 <regfree@plt>
mov    %r13,%rdi
call   1060 <closedir@plt>
jmp    11bf <serial_port_enumerate+0x2f>
lea    0xc(%rsp),%rdi
call   1050 <regfree@plt>
jmp    11bc <serial_port_enumerate+0x2c>
lea    0xc(%rsp),%rdi
call   1050 <regfree@plt>
mov    %r13,%rdi
call   1060 <closedir@plt>
jmp    11bc <serial_port_enumerate+0x2c>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.string_set = type { i32 }
%struct.dirent = type { i8* }
%struct.TYPE_3__ = type { i64, i32 }
@.str = [11 x i8] c"^cu\\.(.+)$\00"
@REG_EXTENDED = global i32 0
@.str.1 = [5 x i8] c"/dev\00"
@REG_NOMATCH = global i32 0
define %struct.string_set* @serial_port_enumerate() {
%1 = alloca %struct.string_set*
%2 = alloca %struct.string_set*
%3 = alloca %struct.dirent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [2 x %struct.TYPE_3__]
%8 = load i32, i32* @REG_EXTENDED
%9 = call i32 @regcomp(i32* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store %struct.string_set* null, %struct.string_set** %1
br label %70
13: ; preds = %0
%14 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @regfree(i32* %4)
store %struct.string_set* null, %struct.string_set** %1
br label %70
19: ; preds = %13
%20 = call %struct.string_set* (...) @string_set_create()
store %struct.string_set* %20, %struct.string_set** %2
%21 = load %struct.string_set*, %struct.string_set** %2
%22 = icmp eq %struct.string_set* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @regfree(i32* %4)
%25 = load i32*, i32** %6
%26 = call i32 @closedir(i32* %25)
store %struct.string_set* null, %struct.string_set** %1
br label %70
27: ; preds = %19
br label %28
28: ; preds = %47, %41, %27
%29 = load i32*, i32** %6
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %3
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %65
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %3
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %7, i64 0, i64 0
%37 = call i32 @regexec(i32* %4, i8* %35, i32 2, %struct.TYPE_3__* %36, i32 0)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = load i32, i32* @REG_NOMATCH
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %32
br label %28
42: ; preds = %32
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %42
%48 = load %struct.dirent*, %struct.dirent** %3
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %7, i64 0, i64 1
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 0, i8* %54
%55 = load %struct.string_set*, %struct.string_set** %2
%56 = load %struct.dirent*, %struct.dirent** %3
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %7, i64 0, i64 1
%60 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %59, i32 0, i32 1
%61 = load i32, i32* %60
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %58, i64 %62
%64 = call i32 @string_set_add(%struct.string_set* %55, i8* %63)
br label %28
65: ; preds = %28
%66 = call i32 @regfree(i32* %4)
%67 = load i32*, i32** %6
%68 = call i32 @closedir(i32* %67)
%69 = load %struct.string_set*, %struct.string_set** %2
store %struct.string_set* %69, %struct.string_set** %1
br label %70
70: ; preds = %65, %23, %17, %12
%71 = load %struct.string_set*, %struct.string_set** %1
ret %struct.string_set* %71
}

LLVM IR (-O2):
%struct.string_set = type { i32 }
%struct.TYPE_3__ = type { i64, i32 }
%struct.dirent = type { i8* }
@.str = [11 x i8] c"^cu\\.(.+)$\00"
@REG_EXTENDED = global i32 0
@.str.1 = [5 x i8] c"/dev\00"
@REG_NOMATCH = global i32 0
define %struct.string_set* @serial_port_enumerate() {
%1 = alloca i32
%2 = alloca [2 x %struct.TYPE_3__]
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @REG_EXTENDED
%5 = call i32 @regcomp(i32* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %52
7: ; preds = %0
%8 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = call i32 @regfree(i32* %1)
br label %52
12: ; preds = %7
%13 = call %struct.string_set* (...) @string_set_create()
%14 = icmp eq %struct.string_set* %13, null
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = call %struct.dirent* @readdir(i32* %8)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %49, label %18
18: ; preds = %15
%19 = bitcast [2 x %struct.TYPE_3__]* %2 to i8*
%20 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 0
%21 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 1, i32 0
%22 = getelementptr inbounds [2 x %struct.TYPE_3__], [2 x %struct.TYPE_3__]* %2, i64 0, i64 1, i32 1
br label %26
23: ; preds = %12
%24 = call i32 @regfree(i32* %1)
%25 = call i32 @closedir(i32* %8)
br label %52
26: ; preds = %18, %46
%27 = phi %struct.dirent* [ %16, %18 ], [ %47, %46 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @regexec(i32* %1, i8* %29, i32 2, %struct.TYPE_3__* %20, i32 0)
%31 = load i32, i32* @REG_NOMATCH
%32 = icmp eq i32 %30, %31
br i1 %32, label %46, label %33
33: ; preds = %26
%34 = icmp eq i32 %30, 0
br i1 %34, label %37, label %35
35: ; preds = %33
%36 = call i32 (...) @abort()
unreachable
37: ; preds = %33
%38 = load i8*, i8** %28
%39 = load i64, i64* %21
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %28
%42 = load i32, i32* %22
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = call i32 @string_set_add(%struct.string_set* %13, i8* %44)
br label %46
46: ; preds = %26, %37
%47 = call %struct.dirent* @readdir(i32* %8)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %49, label %26
49: ; preds = %46, %15
%50 = call i32 @regfree(i32* %1)
%51 = call i32 @closedir(i32* %8)
br label %52
52: ; preds = %0, %49, %23, %10
%53 = phi %struct.string_set* [ null, %10 ], [ null, %23 ], [ %13, %49 ], [ null, %0 ]
ret %struct.string_set* %53
}