#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 cgetstr(char *, char *, char **);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int ckqueue(char *cap) {
  struct dirent *d;
  DIR *dirp;
  const char *spooldir;
  char *sd = NULL;
  int rv = 0;
  spooldir = cgetstr(cap, "sd", &sd) == -1 ? "/var/spool/output/lpd" : sd;
  if ((dirp = opendir(spooldir)) == NULL) {
    rv = -1;
    goto out;
  }
  while ((d = readdir(dirp)) != NULL) {
    if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
      continue;
    rv = 1;
    break;
  }
out:
  if (dirp != NULL)
    closedir(dirp);
  if (spooldir != sd)
    free(sd);
  return (rv);
}


x86 assembly (clang, -O0):
<ckqueue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <cgetstr@plt>
cmp    $0xffffffff,%eax
jne    1198 <ckqueue+0x48>
lea    0xe74(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11a0 <ckqueue+0x50>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cb <ckqueue+0x7b>
movl   $0xffffffff,-0x2c(%rbp)
jmp    121f <ckqueue+0xcf>
jmp    11d0 <ckqueue+0x80>
mov    -0x18(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     121a <ckqueue+0xca>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x63,%eax
jne    120e <ckqueue+0xbe>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x66,%eax
je     1213 <ckqueue+0xc3>
jmp    11d0 <ckqueue+0x80>
movl   $0x1,-0x2c(%rbp)
jmp    121f <ckqueue+0xcf>
cmpq   $0x0,-0x18(%rbp)
je     1233 <ckqueue+0xe3>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     124a <ckqueue+0xfa>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ckqueue>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe98(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1070 <cgetstr@plt>
cmp    $0xffffffff,%eax
je     117e <ckqueue+0x2e>
mov    0x8(%rsp),%rbx
jmp    1185 <ckqueue+0x35>
lea    0xe7e(%rip),%rbx
mov    %rbx,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11db <ckqueue+0x8b>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <readdir@plt>
xor    %r14d,%r14d
jmp    11b8 <ckqueue+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d1 <ckqueue+0x81>
mov    (%rax),%rax
cmpb   $0x63,(%rax)
jne    11b0 <ckqueue+0x60>
cmpb   $0x66,0x1(%rax)
jne    11b0 <ckqueue+0x60>
mov    $0x1,%r14d
mov    %rbp,%rdi
call   1050 <closedir@plt>
jmp    11e1 <ckqueue+0x91>
mov    $0xffffffff,%r14d
mov    0x8(%rsp),%rdi
cmp    %rdi,%rbx
je     11f0 <ckqueue+0xa0>
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"sd\00"
@.str.1 = [22 x i8] c"/var/spool/output/lpd\00"
define i32 @ckqueue(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.dirent*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %6
store i32 0, i32* %7
%8 = load i8*, i8** %2
%9 = call i32 @cgetstr(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
br label %14
12: ; preds = %1
%13 = load i8*, i8** %6
br label %14
14: ; preds = %12, %11
%15 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %11 ], [ %13, %12 ]
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %4
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %7
br label %44
20: ; preds = %14
br label %21
21: ; preds = %41, %20
%22 = load i32*, i32** %4
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %3
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %43
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %3
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 99
br i1 %32, label %41, label %33
33: ; preds = %25
%34 = load %struct.dirent*, %struct.dirent** %3
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 102
br i1 %40, label %41, label %42
41: ; preds = %33, %25
br label %21
42: ; preds = %33
store i32 1, i32* %7
br label %43
43: ; preds = %42, %21
br label %44
44: ; preds = %43, %19
%45 = load i32*, i32** %4
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32*, i32** %4
%49 = call i32 @closedir(i32* %48)
br label %50
50: ; preds = %47, %44
%51 = load i8*, i8** %5
%52 = load i8*, i8** %6
%53 = icmp ne i8* %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %50
%58 = load i32, i32* %7
ret i32 %58
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"sd\00"
@.str.1 = [22 x i8] c"/var/spool/output/lpd\00"
define i32 @ckqueue(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%4 = call i32 @cgetstr(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %2)
%5 = icmp eq i32 %4, -1
%6 = load i8*, i8** %2
%7 = select i1 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %6
%8 = call i32* @opendir(i8* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %29, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %26, label %13
13: ; preds = %10, %23
%14 = phi %struct.dirent* [ %24, %23 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 99
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = getelementptr inbounds i8, i8* %16, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 102
br i1 %22, label %26, label %23
23: ; preds = %19, %13
%24 = call %struct.dirent* @readdir(i32* %8)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %13
26: ; preds = %19, %23, %10
%27 = phi i32 [ 0, %10 ], [ 1, %19 ], [ 0, %23 ]
%28 = call i32 @closedir(i32* %8)
br label %29
29: ; preds = %1, %26
%30 = phi i32 [ %27, %26 ], [ -1, %1 ]
%31 = load i8*, i8** %2
%32 = icmp eq i8* %7, %31
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 @free(i8* %31)
br label %35
35: ; preds = %33, %29
ret i32 %30
}