#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 st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
scalar_t__ *strrchr(char *, char);
int /*<<< orphan*/ tcpd_warn(char *, char const *);
int check_path(const char *path, struct stat *st) {
  struct stat stbuf;
  char buf[BUFSIZ];
  if (stat(path, st) < 0)
    return (-1);
  if (st->st_mode & 002)
    tcpd_warn("%s: world writable", path);
  if (path[0] == '/' && path[1] != 0) {
    strlcpy(buf, path, sizeof(buf));
    strrchr(buf, '/')[0] = 0;
    (void)check_path(buf[0] ? buf : "/", &stbuf);
  }
  return (0);
}


x86 assembly (clang, -O0):
<check_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11ba <check_path+0x6a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1270 <check_path+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11dc <check_path+0x8c>
mov    -0x10(%rbp),%rsi
lea    0xe29(%rip),%rdi
call   1050 <tcpd_warn@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1262 <check_path+0x112>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     1262 <check_path+0x112>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1060 <strlcpy@plt>
mov    -0x40(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
movq   $0x0,(%rcx)
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1245 <check_path+0xf5>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1255 <check_path+0x105>
lea    0xdc7(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1255 <check_path+0x105>
mov    -0x50(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <check_path@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%r13
mov    0x2e72(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1040 <stat@plt>
test   %rax,%rax
js     11ed <check_path+0x9d>
testb  $0x2,(%r15)
je     11a2 <check_path+0x52>
lea    0xe66(%rip),%rdi
mov    %r14,%rsi
call   1050 <tcpd_warn@plt>
xor    %r15d,%r15d
cmpb   $0x2f,(%r14)
jne    11f3 <check_path+0xa3>
cmpb   $0x0,0x1(%r14)
je     11f3 <check_path+0xa3>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <strlcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
movq   $0x0,(%rax)
cmpb   $0x0,(%rbx)
lea    0xe35(%rip),%rdi
cmovne %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   1070 <check_path@plt>
jmp    11f3 <check_path+0xa3>
mov    $0xffffffff,%r15d
mov    %r13,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@.str = [19 x i8] c"%s: world writable\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @check_path(i8* %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load i8*, i8** %4
%15 = load %struct.stat*, %struct.stat** %5
%16 = call i64 @stat(i8* %14, %struct.stat* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %9
br label %56
19: ; preds = %2
%20 = load %struct.stat*, %struct.stat** %5
%21 = getelementptr inbounds %struct.stat, %struct.stat* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = call i32 @tcpd_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %19
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 47
br i1 %33, label %34, label %55
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %55
40: ; preds = %34
%41 = load i8*, i8** %4
%42 = trunc i64 %11 to i32
%43 = call i32 @strlcpy(i8* %13, i8* %41, i32 %42)
%44 = call i64* @strrchr(i8* %13, i8 signext 47)
%45 = getelementptr inbounds i64, i64* %44, i64 0
store i64 0, i64* %45
%46 = getelementptr inbounds i8, i8* %13, i64 0
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %40
br label %52
51: ; preds = %40
br label %52
52: ; preds = %51, %50
%53 = phi i8* [ %13, %50 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %51 ]
%54 = call i32 @check_path(i8* %53, %struct.stat* %6)
br label %55
55: ; preds = %52, %34, %28
store i32 0, i32* %3
store i32 1, i32* %9
br label %56
56: ; preds = %55, %18
%57 = load i8*, i8** %7
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@BUFSIZ = global i32 0
@.str = [19 x i8] c"%s: world writable\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @check_path(i8* %0, %struct.stat* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = tail call i64 @stat(i8* %0, %struct.stat* %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, 2
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @tcpd_warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %11
%19 = load i8, i8* %0
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %0, i64 1
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = call i32 @strlcpy(i8* %8, i8* %0, i32 %5)
%27 = call i64* @strrchr(i8* %8, i8 signext 47)
store i64 0, i64* %27
%28 = load i8, i8* %8
%29 = icmp eq i8 %28, 0
%30 = select i1 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %8
%31 = call i32 @check_path(i8* %30, %struct.stat* %3)
br label %32
32: ; preds = %18, %21, %25, %2
%33 = phi i32 [ -1, %2 ], [ 0, %25 ], [ 0, %21 ], [ 0, %18 ]
ret i32 %33
}