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

/* Type definitions */
struct TYPE_5__ {
  scalar_t__ gl_pathc;
  char **gl_pathv;
};
typedef TYPE_1__ glob_t;
typedef int /*<<< orphan*/ gl;

/* Variables and functions */
int GLOB_NOCHECK;
scalar_t__ glob(char *, int, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ reply_noformat(int, char *);
int strcspn(char *, char *);
char *strdup(char *);
char *expglob(char *s) {
  char *p, **pp, *rval;
  int flags = GLOB_NOCHECK;
  int n;
  glob_t gl;
  memset(&gl, 0, sizeof(gl));
  if (glob(s, flags, NULL, &gl) == 0 && gl.gl_pathc != 0) {
    for (pp = gl.gl_pathv, p = NULL, n = 0; *pp; pp++)
      if (*(*pp + strcspn(*pp, "\r\n")) == '\0') {
        p = *pp;
        n++;
      }
    if (n == 0)
      rval = strdup(s);
    else if (n == 1)
      rval = strdup(p);
    else {
      reply_noformat(550, "Wildcard is ambiguous.");
      rval = NULL;
    }
  } else {
    reply_noformat(550, "Wildcard expansion error.");
    rval = NULL;
  }
  globfree(&gl);
  return (rval);
}


x86 assembly (clang, -O0):
<expglob>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x38(%rbp),%rcx
call   1070 <glob@plt>
cmp    $0x0,%rax
jne    128f <expglob+0x12f>
cmpq   $0x0,-0x38(%rbp)
je     128f <expglob+0x12f>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1234 <expglob+0xd4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xe11(%rip),%rsi
call   1060 <strcspn@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    121e <expglob+0xbe>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1223 <expglob+0xc3>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    11c8 <expglob+0x68>
cmpl   $0x0,-0x28(%rbp)
jne    1250 <expglob+0xf0>
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    128a <expglob+0x12a>
cmpl   $0x1,-0x28(%rbp)
jne    126c <expglob+0x10c>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    1285 <expglob+0x125>
mov    $0x226,%edi
lea    0xd8b(%rip),%rsi
call   1050 <reply_noformat@plt>
movq   $0x0,-0x20(%rbp)
jmp    128a <expglob+0x12a>
jmp    12a8 <expglob+0x148>
mov    $0x226,%edi
lea    0xd7f(%rip),%rsi
call   1050 <reply_noformat@plt>
movq   $0x0,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
call   1030 <globfree@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expglob>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e68(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
mov    %r15,%rcx
call   1070 <glob@plt>
test   %rax,%rax
jne    1207 <expglob+0xa7>
cmpq   $0x0,0x8(%rsp)
je     1207 <expglob+0xa7>
mov    0x10(%rsp),%r13
mov    0x0(%r13),%rbx
test   %rbx,%rbx
je     1202 <expglob+0xa2>
add    $0x8,%r13
xor    %ebp,%ebp
lea    0xe3b(%rip),%r12
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strcspn@plt>
cltq
xor    %ecx,%ecx
cmpb   $0x0,(%rbx,%rax,1)
sete   %cl
cmove  %rbx,%r15
add    %ecx,%ebp
mov    0x0(%r13),%rbx
add    $0x8,%r13
test   %rbx,%rbx
jne    11d0 <expglob+0x70>
cmp    $0x1,%ebp
je     121c <expglob+0xbc>
test   %ebp,%ebp
jne    1243 <expglob+0xe3>
mov    %r14,%rdi
jmp    121f <expglob+0xbf>
lea    0xe0c(%rip),%rsi
mov    $0x226,%edi
call   1050 <reply_noformat@plt>
xor    %ebx,%ebx
jmp    1227 <expglob+0xc7>
mov    %r15,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1030 <globfree@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb9(%rip),%rsi
jmp    120e <expglob+0xae>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, i8** }
@GLOB_NOCHECK = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [23 x i8] c"Wildcard is ambiguous.\00"
@.str.2 = [26 x i8] c"Wildcard expansion error.\00"
define i8* @expglob(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.TYPE_5__
store i8* %0, i8** %2
%9 = load i32, i32* @GLOB_NOCHECK
store i32 %9, i32* %6
%10 = call i32 @memset(%struct.TYPE_5__* %8, i32 0, i32 16)
%11 = load i8*, i8** %2
%12 = load i32, i32* %6
%13 = call i64 @glob(i8* %11, i32 %12, i32* null, %struct.TYPE_5__* %8)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %62
15: ; preds = %1
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %8, i32 0, i32 0
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %62
19: ; preds = %15
%20 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %8, i32 0, i32 1
%21 = load i8**, i8*** %20
store i8** %21, i8*** %4
store i8* null, i8** %3
store i32 0, i32* %7
br label %22
22: ; preds = %43, %19
%23 = load i8**, i8*** %4
%24 = load i8*, i8** %23
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %46
26: ; preds = %22
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = call i32 @strcspn(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
store i8* %39, i8** %3
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %42
42: ; preds = %37, %26
br label %43
43: ; preds = %42
%44 = load i8**, i8*** %4
%45 = getelementptr inbounds i8*, i8** %44, i32 1
store i8** %45, i8*** %4
br label %22
46: ; preds = %22
%47 = load i32, i32* %7
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %2
%51 = call i8* @strdup(i8* %50)
store i8* %51, i8** %5
br label %61
52: ; preds = %46
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i8*, i8** %3
%57 = call i8* @strdup(i8* %56)
store i8* %57, i8** %5
br label %60
58: ; preds = %52
%59 = call i32 @reply_noformat(i32 550, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %5
br label %60
60: ; preds = %58, %55
br label %61
61: ; preds = %60, %49
br label %64
62: ; preds = %15, %1
%63 = call i32 @reply_noformat(i32 550, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %5
br label %64
64: ; preds = %62, %61
%65 = call i32 @globfree(%struct.TYPE_5__* %8)
%66 = load i8*, i8** %5
ret i8* %66
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, i8** }
@GLOB_NOCHECK = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [23 x i8] c"Wildcard is ambiguous.\00"
@.str.2 = [26 x i8] c"Wildcard expansion error.\00"
define noalias i8* @expglob(i8* %0) {
%2 = alloca %struct.TYPE_5__
%3 = load i32, i32* @GLOB_NOCHECK
%4 = bitcast %struct.TYPE_5__* %2 to i8*
%5 = call i32 @memset(%struct.TYPE_5__* %2, i32 0, i32 16)
%6 = call i64 @glob(i8* %0, i32 %3, i32* null, %struct.TYPE_5__* %2)
%7 = icmp eq i64 %6, 0
%8 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = icmp ne i64 %9, 0
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %12, label %40
12: ; preds = %1
%13 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 1
%14 = load i8**, i8*** %13
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %34, label %17
17: ; preds = %12, %17
%18 = phi i8* [ %31, %17 ], [ %15, %12 ]
%19 = phi i32 [ %29, %17 ], [ 0, %12 ]
%20 = phi i8* [ %27, %17 ], [ null, %12 ]
%21 = phi i8** [ %30, %17 ], [ %14, %12 ]
%22 = call i32 @strcspn(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %18, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
%27 = select i1 %26, i8* %18, i8* %20
%28 = zext i1 %26 to i32
%29 = add nuw nsw i32 %19, %28
%30 = getelementptr inbounds i8*, i8** %21, i64 1
%31 = load i8*, i8** %30
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %17
33: ; preds = %17
switch i32 %29, label %38 [
i32 0, label %34
i32 1, label %36
]
34: ; preds = %12, %33
%35 = call i8* @strdup(i8* %0)
br label %42
36: ; preds = %33
%37 = call i8* @strdup(i8* %27)
br label %42
38: ; preds = %33
%39 = call i32 @reply_noformat(i32 550, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %42
40: ; preds = %1
%41 = call i32 @reply_noformat(i32 550, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %34, %38, %36, %40
%43 = phi i8* [ %35, %34 ], [ %37, %36 ], [ null, %38 ], [ null, %40 ]
%44 = call i32 @globfree(%struct.TYPE_5__* %2)
ret i8* %43
}