#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 PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ file_is_writable(char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int getpdufile(char *filename) {
  int result = 0;
  struct stat statbuf;
  DIR *dirdata;
  struct dirent *ent;
  char tmpname[PATH_MAX];
  if (*filename) {
    if (filename[strlen(filename) - 1] != '/') {
      if (file_is_writable(filename))
        result = 1;
    } else if (!strchr(filename, '.')) {
      if (stat(filename, &statbuf) == 0) {
        if (S_ISDIR(statbuf.st_mode)) {
          if ((dirdata = opendir(filename))) {
            while ((ent = readdir(dirdata))) {
              if (ent->d_name[0] != '.') {
                sprintf(tmpname, "%s%s", filename, ent->d_name);
                if (file_is_writable(tmpname)) {
                  strcpy(filename, tmpname);
                  result = 1;
                  break;
                }
              }
            }
            closedir(dirdata);
          }
        }
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<getpdufile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     132e <getpdufile+0x18e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1235 <getpdufile+0x95>
mov    -0x8(%rbp),%rdi
call   1040 <file_is_writable@plt>
cmp    $0x0,%rax
je     1230 <getpdufile+0x90>
movl   $0x1,-0xc(%rbp)
jmp    1329 <getpdufile+0x189>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strchr@plt>
cmp    $0x0,%eax
jne    1324 <getpdufile+0x184>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jne    131f <getpdufile+0x17f>
mov    -0x10(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     131a <getpdufile+0x17a>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1315 <getpdufile+0x175>
jmp    1291 <getpdufile+0xf1>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     130c <getpdufile+0x16c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1307 <getpdufile+0x167>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
lea    0xd2f(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <file_is_writable@plt>
cmp    $0x0,%rax
je     1302 <getpdufile+0x162>
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdi
call   1030 <strcpy@plt>
movl   $0x1,-0xc(%rbp)
jmp    130c <getpdufile+0x16c>
jmp    1307 <getpdufile+0x167>
jmp    1291 <getpdufile+0xf1>
mov    -0x18(%rbp),%rdi
call   1090 <closedir@plt>
jmp    131a <getpdufile+0x17a>
jmp    131f <getpdufile+0x17f>
jmp    1324 <getpdufile+0x184>
jmp    1329 <getpdufile+0x189>
jmp    132e <getpdufile+0x18e>
mov    -0xc(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpdufile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsp,%r13
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r14d,%r14d
cmpb   $0x0,(%rdi)
je     1243 <getpdufile+0xa3>
mov    %rdi,%r15
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r15,1)
jne    1231 <getpdufile+0x91>
mov    %r15,%rdi
mov    $0x2e,%esi
call   1070 <strchr@plt>
test   %eax,%eax
jne    1243 <getpdufile+0xa3>
lea    -0x30(%rbp),%rsi
mov    %r15,%rdi
call   10a0 <stat@plt>
test   %rax,%rax
jne    1243 <getpdufile+0xa3>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     1243 <getpdufile+0xa3>
mov    %r15,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1243 <getpdufile+0xa3>
mov    %rax,%r12
mov    %rax,%rdi
call   10b0 <readdir@plt>
xor    %r14d,%r14d
jmp    1268 <getpdufile+0xc8>
mov    %r15,%rdi
call   1040 <file_is_writable@plt>
xor    %r14d,%r14d
test   %rax,%rax
setne  %r14b
mov    %r13,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12a5 <getpdufile+0x105>
mov    (%rax),%rcx
cmpb   $0x2e,(%rcx)
je     1260 <getpdufile+0xc0>
mov    %rbx,%rdi
lea    0xd81(%rip),%rsi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <file_is_writable@plt>
test   %rax,%rax
je     1260 <getpdufile+0xc0>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    $0x1,%r14d
mov    %r12,%rdi
call   1090 <closedir@plt>
jmp    1243 <getpdufile+0xa3>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s%s\00"
define i32 @getpdufile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 %10
store i64 %10, i64* %8
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %83
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = load i8*, i8** %2
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 47
br i1 %25, label %26, label %32
26: ; preds = %16
%27 = load i8*, i8** %2
%28 = call i64 @file_is_writable(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 1, i32* %3
br label %31
31: ; preds = %30, %26
br label %82
32: ; preds = %16
%33 = load i8*, i8** %2
%34 = call i32 @strchr(i8* %33, i8 signext 46)
%35 = icmp ne i32 %34, 0
br i1 %35, label %81, label %36
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = call i64 @stat(i8* %37, %struct.stat* %4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %80
40: ; preds = %36
%41 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @S_ISDIR(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %79
45: ; preds = %40
%46 = load i8*, i8** %2
%47 = call i32* @opendir(i8* %46)
store i32* %47, i32** %5
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %78
49: ; preds = %45
br label %50
50: ; preds = %74, %49
%51 = load i32*, i32** %5
%52 = call %struct.dirent* @readdir(i32* %51)
store %struct.dirent* %52, %struct.dirent** %6
%53 = icmp ne %struct.dirent* %52, null
br i1 %53, label %54, label %75
54: ; preds = %50
%55 = load %struct.dirent*, %struct.dirent** %6
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = getelementptr inbounds i8, i8* %57, i64 0
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %60, 46
br i1 %61, label %62, label %74
62: ; preds = %54
%63 = load i8*, i8** %2
%64 = load %struct.dirent*, %struct.dirent** %6
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %63, i8* %66)
%68 = call i64 @file_is_writable(i8* %12)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %62
%71 = load i8*, i8** %2
%72 = call i32 @strcpy(i8* %71, i8* %12)
store i32 1, i32* %3
br label %75
73: ; preds = %62
br label %74
74: ; preds = %73, %54
br label %50
75: ; preds = %70, %50
%76 = load i32*, i32** %5
%77 = call i32 @closedir(i32* %76)
br label %78
78: ; preds = %75, %45
br label %79
79: ; preds = %78, %40
br label %80
80: ; preds = %79, %36
br label %81
81: ; preds = %80, %32
br label %82
82: ; preds = %81, %31
br label %83
83: ; preds = %82, %1
%84 = load i32, i32* %3
%85 = load i8*, i8** %7
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s%s\00"
define i32 @getpdufile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %56, label %10
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 47
br i1 %16, label %21, label %17
17: ; preds = %10
%18 = tail call i64 @file_is_writable(i8* %0)
%19 = icmp ne i64 %18, 0
%20 = zext i1 %19 to i32
br label %56
21: ; preds = %10
%22 = tail call i32 @strchr(i8* %0, i8 signext 46)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %56
24: ; preds = %21
%25 = call i64 @stat(i8* %0, %struct.stat* %2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %56
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISDIR(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %56, label %32
32: ; preds = %27
%33 = call i32* @opendir(i8* %0)
%34 = icmp eq i32* %33, null
br i1 %34, label %56, label %35
35: ; preds = %32
%36 = call %struct.dirent* @readdir(i32* %33)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %53, label %38
38: ; preds = %35, %50
%39 = phi %struct.dirent* [ %51, %50 ], [ %36, %35 ]
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i64 0, i32 0
%41 = load i8*, i8** %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, 46
br i1 %43, label %50, label %44
44: ; preds = %38
%45 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %41)
%46 = call i64 @file_is_writable(i8* %7)
%47 = icmp eq i64 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 @strcpy(i8* %0, i8* %7)
br label %53
50: ; preds = %44, %38
%51 = call %struct.dirent* @readdir(i32* %33)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %53, label %38
53: ; preds = %50, %35, %48
%54 = phi i32 [ 1, %48 ], [ 0, %35 ], [ 0, %50 ]
%55 = call i32 @closedir(i32* %33)
br label %56
56: ; preds = %17, %24, %32, %53, %27, %21, %1
%57 = phi i32 [ 0, %21 ], [ %54, %53 ], [ 0, %32 ], [ 0, %27 ], [ 0, %24 ], [ 0, %1 ], [ %20, %17 ]
ret i32 %57
}