#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_4__ TYPE_1__;

/* Type definitions */
struct dirent {
  char *d_name;
};
struct TYPE_4__ {
  int gl_pathc;
  scalar_t__ *gl_pathv;
  int /*<<< orphan*/ gl_offs;
};
typedef TYPE_1__ glob_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, scalar_t__, scalar_t__);
int test_glob(void) {
  int i = 0;
  glob_t g = {
      .gl_offs = 0,
  };
  char path[] = "/sys/class/tty/*/power/wakeup";
  DIR *list;
  struct dirent *entity;
  int matched = 0;
  if ((i = glob(path, 0, NULL, &g)) != 0) {
    fprintf(stderr, "glob %d failed\n", i);
    return EINVAL;
  }
  list = opendir("/sys/class/tty/");
  while ((entity = readdir(list))) {
    int found = 0;
    if (strcmp(entity->d_name, ".") == 0 || strcmp(entity->d_name, "..") == 0)
      continue;
    for (i = 0; g.gl_pathv[i]; i++) {
      if (strncmp(entity->d_name, g.gl_pathv[i] + strlen("/sys/class/tty/"),
                  strlen(entity->d_name)) == 0) {
        found = 1;
        matched++;
        break;
      }
    }
    if (!found) {
      printf("Non matched dir %s\n", entity->d_name);
      return -EINVAL;
    }
  }
  if (matched != g.gl_pathc) {
    printf("matched %d, found %d\n", matched, g.gl_pathc);
    return -EINVAL;
  }
  closedir(list);
  globfree(&g);
  printf("glob test passed\n");
  return 0;
}


x86 assembly (clang, -O0):
<test_glob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    0xe2a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe27(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe24(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe22(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    0xe1c(%rip),%ax
mov    %ax,-0x24(%rbp)
movl   $0x0,-0x54(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
call   10d0 <glob@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     1253 <test_glob+0xa3>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xde1(%rip),%rsi
call   10b0 <fprintf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13ee <test_glob+0x23e>
lea    0xdd4(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     1394 <test_glob+0x1e4>
movl   $0x0,-0x58(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
lea    0xdb0(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     12bb <test_glob+0x10b>
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
lea    0xd92(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    12c0 <test_glob+0x110>
jmp    1263 <test_glob+0xb3>
movl   $0x0,-0x8(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     135d <test_glob+0x1ad>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x70(%rbp)
lea    0xd32(%rip),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x70(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    134a <test_glob+0x19a>
movl   $0x1,-0x58(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    135d <test_glob+0x1ad>
jmp    134f <test_glob+0x19f>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12c7 <test_glob+0x117>
cmpl   $0x0,-0x58(%rbp)
jne    138f <test_glob+0x1df>
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
lea    0xccc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
mov    0x2c53(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13ee <test_glob+0x23e>
jmp    1263 <test_glob+0xb3>
mov    -0x54(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     13c7 <test_glob+0x217>
mov    -0x54(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xca8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
mov    0x2c1b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13ee <test_glob+0x23e>
mov    -0x48(%rbp),%rdi
call   1090 <closedir@plt>
lea    -0x20(%rbp),%rdi
call   1030 <globfree@plt>
lea    0xc8b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_glob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
movups 0xe39(%rip),%xmm0
movups %xmm0,0x2e(%rsp)
movaps 0xe1f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
xor    %r13d,%r13d
lea    0x20(%rsp),%rdi
mov    %rsp,%rcx
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <glob@plt>
test   %eax,%eax
je     1223 <test_glob+0x73>
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%edi
lea    0xe10(%rip),%rsi
mov    %eax,%edx
call   10b0 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%ebx
jmp    1351 <test_glob+0x1a1>
lea    0xe04(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     130d <test_glob+0x15d>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0xdef(%rip),%rbp
lea    0xde7(%rip),%r12
mov    %r14,0x18(%rsp)
jmp    1285 <test_glob+0xd5>
add    $0x1,%r13d
mov    %r14,%rbp
mov    0x18(%rsp),%r14
lea    0xdcd(%rip),%r12
mov    %r14,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     130d <test_glob+0x15d>
mov    (%rbx),%rdi
mov    %rbp,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1271 <test_glob+0xc1>
mov    (%rbx),%rdi
mov    %r12,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1271 <test_glob+0xc1>
mov    %rbp,%r14
mov    0x8(%rsp),%rax
mov    (%rax),%rbp
test   %rbp,%rbp
je     12f8 <test_glob+0x148>
mov    $0x8,%r15d
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%r12
add    $0xf,%rbp
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     125e <test_glob+0xae>
mov    0x8(%rsp),%rax
mov    (%rax,%r15,1),%rbp
add    $0x8,%r15
test   %rbp,%rbp
jne    12c0 <test_glob+0x110>
mov    (%rbx),%rsi
lea    0xd3f(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1348 <test_glob+0x198>
mov    (%rsp),%edx
cmp    %edx,%r13d
jne    1335 <test_glob+0x185>
mov    %r14,%rdi
call   1090 <closedir@plt>
mov    %rsp,%rdi
call   1030 <globfree@plt>
lea    0xd3f(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
jmp    1351 <test_glob+0x1a1>
lea    0xd19(%rip),%rdi
xor    %ebx,%ebx
mov    %r13d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2c89(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i64*, i32 }
%struct.dirent = type { i8* }
@__const.test_glob.path = [30 x i8] c"/sys/class/tty/*/power/wakeup\00"
@stderr = global i32 0
@.str = [16 x i8] c"glob %d failed\0A\00"
@EINVAL = global i32 0
@.str.1 = [16 x i8] c"/sys/class/tty/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [20 x i8] c"Non matched dir %s\0A\00"
@.str.5 = [22 x i8] c"matched %d, found %d\0A\00"
@.str.6 = [18 x i8] c"glob test passed\0A\00"
define i32 @test_glob() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.TYPE_4__
%4 = alloca [30 x i8]
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %2
%9 = bitcast %struct.TYPE_4__* %3 to i8*
%10 = bitcast [30 x i8]* %4 to i8*
store i32 0, i32* %7
%11 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%12 = call i32 @glob(i8* %11, i32 0, i32* null, %struct.TYPE_4__* %3)
store i32 %12, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* %2
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @EINVAL
store i32 %18, i32* %1
br label %100
19: ; preds = %0
%20 = call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %5
br label %21
21: ; preds = %82, %37, %19
%22 = load i32*, i32** %5
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %6
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %83
25: ; preds = %21
store i32 0, i32* %8
%26 = load %struct.dirent*, %struct.dirent** %6
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load %struct.dirent*, %struct.dirent** %6
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31, %25
br label %21
38: ; preds = %31
store i32 0, i32* %2
br label %39
39: ; preds = %69, %38
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 1
%41 = load i64*, i64** %40
%42 = load i32, i32* %2
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i64, i64* %41, i64 %43
%45 = load i64, i64* %44
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %72
47: ; preds = %39
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 1
%52 = load i64*, i64** %51
%53 = load i32, i32* %2
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i64, i64* %52, i64 %54
%56 = load i64, i64* %55
%57 = call i64 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%58 = add nsw i64 %56, %57
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i64 @strlen(i8* %61)
%63 = call i64 @strncmp(i8* %50, i64 %58, i64 %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %47
store i32 1, i32* %8
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %72
68: ; preds = %47
br label %69
69: ; preds = %68
%70 = load i32, i32* %2
%71 = add nsw i32 %70, 1
store i32 %71, i32* %2
br label %39
72: ; preds = %65, %39
%73 = load i32, i32* %8
%74 = icmp ne i32 %73, 0
br i1 %74, label %82, label %75
75: ; preds = %72
%76 = load %struct.dirent*, %struct.dirent** %6
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i32, i32* @EINVAL
%81 = sub nsw i32 0, %80
store i32 %81, i32* %1
br label %100
82: ; preds = %72
br label %21
83: ; preds = %21
%84 = load i32, i32* %7
%85 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 0
%86 = load i32, i32* %85
%87 = icmp ne i32 %84, %86
br i1 %87, label %88, label %95
88: ; preds = %83
%89 = load i32, i32* %7
%90 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 0
%91 = load i32, i32* %90
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %89, i32 %91)
%93 = load i32, i32* @EINVAL
%94 = sub nsw i32 0, %93
store i32 %94, i32* %1
br label %100
95: ; preds = %83
%96 = load i32*, i32** %5
%97 = call i32 @closedir(i32* %96)
%98 = call i32 @globfree(%struct.TYPE_4__* %3)
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %100
100: ; preds = %95, %88, %75, %14
%101 = load i32, i32* %1
ret i32 %101
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i64*, i32 }
%struct.dirent = type { i8* }
@__const.test_glob.path = [30 x i8] c"/sys/class/tty/*/power/wakeup\00"
@stderr = global i32 0
@.str = [16 x i8] c"glob %d failed\0A\00"
@EINVAL = global i32 0
@.str.1 = [16 x i8] c"/sys/class/tty/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [20 x i8] c"Non matched dir %s\0A\00"
@.str.5 = [22 x i8] c"matched %d, found %d\0A\00"
@str = [17 x i8] c"glob test passed\00"
define i32 @test_glob() {
%1 = alloca %struct.TYPE_4__
%2 = alloca [30 x i8]
%3 = bitcast %struct.TYPE_4__* %1 to i8*
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%5 = call i32 @glob(i8* %4, i32 0, i32* null, %struct.TYPE_4__* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
%10 = load i32, i32* @EINVAL
br label %70
11: ; preds = %0
%12 = call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = call %struct.dirent* @readdir(i32* %12)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %57, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 1
br label %17
17: ; preds = %15, %53
%18 = phi %struct.dirent* [ %13, %15 ], [ %55, %53 ]
%19 = phi i32 [ 0, %15 ], [ %54, %53 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %53, label %24
24: ; preds = %17
%25 = load i8*, i8** %20
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %53, label %28
28: ; preds = %24
%29 = load i64*, i64** %16
%30 = load i64, i64* %29
%31 = icmp eq i64 %30, 0
br i1 %31, label %48, label %37
32: ; preds = %37
%33 = load i64*, i64** %16
%34 = getelementptr inbounds i64, i64* %33, i64 %45
%35 = load i64, i64* %34
%36 = icmp eq i64 %35, 0
br i1 %36, label %48, label %37
37: ; preds = %28, %32
%38 = phi i64 [ %45, %32 ], [ 0, %28 ]
%39 = phi i64 [ %35, %32 ], [ %30, %28 ]
%40 = load i8*, i8** %20
%41 = add nsw i64 %39, 15
%42 = call i64 @strlen(i8* dereferenceable(1) %40)
%43 = call i64 @strncmp(i8* %40, i64 %41, i64 %42)
%44 = icmp eq i64 %43, 0
%45 = add nuw i64 %38, 1
br i1 %44, label %46, label %32
46: ; preds = %37
%47 = add nsw i32 %19, 1
br label %53
48: ; preds = %28, %32
%49 = load i8*, i8** %20
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EINVAL
%52 = sub nsw i32 0, %51
br label %70
53: ; preds = %46, %17, %24
%54 = phi i32 [ %19, %24 ], [ %19, %17 ], [ %47, %46 ]
%55 = call %struct.dirent* @readdir(i32* %12)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %17
57: ; preds = %53, %11
%58 = phi i32 [ 0, %11 ], [ %54, %53 ]
%59 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
%60 = load i32, i32* %59
%61 = icmp eq i32 %58, %60
br i1 %61, label %66, label %62
62: ; preds = %57
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %58, i32 %60)
%64 = load i32, i32* @EINVAL
%65 = sub nsw i32 0, %64
br label %70
66: ; preds = %57
%67 = call i32 @closedir(i32* %12)
%68 = call i32 @globfree(%struct.TYPE_4__* %1)
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %70
70: ; preds = %48, %66, %62, %7
%71 = phi i32 [ %10, %7 ], [ %65, %62 ], [ 0, %66 ], [ %52, %48 ]
ret i32 %71
}