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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ addFile(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ *strstr(char const *, char *);
void initMatic(const char *base) {
  char drPath[256];
  char drFile[256];
  DIR *foodir;
  struct dirent *d;
  struct stat st;
  if (strstr(base, "foomatic") == NULL)
    return;
  snprintf(drPath, 256, "%s/driver", base);
  foodir = opendir(drPath);
  if (foodir == NULL)
    return;
  while ((d = readdir(foodir)) != NULL) {
    snprintf(drFile, 256, "foomatic:%s/%s", drPath, d->d_name);
    if (stat(drFile + 9, &st) != 0)
      continue;
    else if (!S_ISREG(st.st_mode))
      continue;
    addFile(drFile);
  }
  closedir(foodir);
}


x86 assembly (clang, -O0):
<initMatic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
jne    11ae <initMatic+0x2e>
jmp    12a0 <initMatic+0x120>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe44(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11f2 <initMatic+0x72>
jmp    12a0 <initMatic+0x120>
jmp    11f7 <initMatic+0x77>
mov    -0x218(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x220(%rbp)
cmp    $0x0,%rax
je     1294 <initMatic+0x114>
lea    -0x210(%rbp),%rdi
lea    -0x110(%rbp),%rcx
mov    -0x220(%rbp),%rax
mov    (%rax),%r8
mov    $0x100,%esi
lea    0xddb(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x210(%rbp),%rdi
add    $0x9,%rdi
lea    -0x228(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1265 <initMatic+0xe5>
jmp    11f7 <initMatic+0x77>
mov    -0x228(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    127e <initMatic+0xfe>
jmp    11f7 <initMatic+0x77>
jmp    1283 <initMatic+0x103>
lea    -0x210(%rbp),%rdi
call   1090 <addFile@plt>
jmp    11f7 <initMatic+0x77>
mov    -0x218(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initMatic>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%rbx
lea    0xe65(%rip),%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1264 <initMatic+0xe4>
lea    0xe59(%rip),%rdx
lea    0x110(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     1264 <initMatic+0xe4>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     125c <initMatic+0xdc>
lea    0x19(%rsp),%r14
lea    0xe1c(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x110(%rsp),%r13
lea    0x8(%rsp),%rbp
jmp    121d <initMatic+0x9d>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     125c <initMatic+0xdc>
mov    (%rax),%r8
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    1210 <initMatic+0x90>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1210 <initMatic+0x90>
mov    %r12,%rdi
call   1090 <addFile@plt>
jmp    1210 <initMatic+0x90>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [9 x i8] c"foomatic\00"
@.str.1 = [10 x i8] c"%s/driver\00"
@.str.2 = [15 x i8] c"foomatic:%s/%s\00"
define void @initMatic(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = tail call i32* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %38, label %10
10: ; preds = %1
%11 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = call i32* @opendir(i8* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %38, label %14
14: ; preds = %10
%15 = call %struct.dirent* @readdir(i32* %12)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %36, label %17
17: ; preds = %14
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 9
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %20
20: ; preds = %17, %33
%21 = phi %struct.dirent* [ %15, %17 ], [ %34, %33 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %23)
%25 = call i64 @stat(i8* %18, %struct.stat* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = load i32, i32* %19
%29 = call i32 @S_ISREG(i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @addFile(i8* %6)
br label %33
33: ; preds = %31, %20, %27
%34 = call %struct.dirent* @readdir(i32* %12)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %20
36: ; preds = %33, %14
%37 = call i32 @closedir(i32* %12)
br label %38
38: ; preds = %10, %1, %36
ret void
}