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


x86 assembly (clang, -O0):
<ckqueue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <cgetstr@plt>
cmp    $0xffffffff,%eax
jne    1192 <ckqueue+0x42>
lea    0xe7c(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11b2 <ckqueue+0x62>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <ckqueue+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <ckqueue+0xe6>
jmp    11ce <ckqueue+0x7e>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1226 <ckqueue+0xd6>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x63,%eax
jne    120c <ckqueue+0xbc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x66,%eax
je     1211 <ckqueue+0xc1>
jmp    11ce <ckqueue+0x7e>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    1236 <ckqueue+0xe6>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ckqueue>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rsi
mov    %rsp,%rdx
call   1070 <cgetstr@plt>
cmp    $0xffffffff,%eax
je     11ba <ckqueue+0x6a>
mov    (%rsp),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11d2 <ckqueue+0x82>
mov    %rbx,%rdi
call   1060 <readdir@plt>
xor    %ebp,%ebp
jmp    1198 <ckqueue+0x48>
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11b0 <ckqueue+0x60>
mov    (%rax),%rax
cmpb   $0x63,(%rax)
jne    1190 <ckqueue+0x40>
cmpb   $0x66,0x1(%rax)
jne    1190 <ckqueue+0x40>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <closedir@plt>
jmp    11d7 <ckqueue+0x87>
lea    0xe42(%rip),%rdi
mov    %rdi,(%rsp)
call   1040 <opendir@plt>
mov    %rax,%rbx
test   %rbx,%rbx
jne    1181 <ckqueue+0x31>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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