#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 */

/* Variables and functions */
char *ending_slash(char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ malloc(scalar_t__);
char *memset(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
char const *strcat(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int testsafepath(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int basicjailissafe(const char *path) {
  char *tmp, *path_w_slash;
  int retval = 1;
  if (path && testsafepath(path, 0, 0) != 0) {
    return 0;
  }
  path_w_slash = ending_slash(path);
  tmp = memset(malloc(strlen(path_w_slash) + 6), 0, strlen(path_w_slash) + 6);
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "dev/"), 0, 0) & ~1) != 0)
    retval = 0;
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "etc/"), 0, 0) & ~1) != 0)
    retval = 0;
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "lib/"), 0, 0) & ~1) != 0)
    retval = 0;
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "usr/"), 0, 0) & ~1) != 0)
    retval = 0;
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "bin/"), 0, 0) & ~1) != 0)
    retval = 0;
  if (retval == 1 &&
      (testsafepath(strcat(strcpy(tmp, path_w_slash), "sbin/"), 0, 0) & ~1) !=
          0)
    retval = 0;
  free(tmp);
  free(path_w_slash);
  ;
  return retval;
}


x86 assembly (clang, -O0):
<basicjailissafe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c0 <basicjailissafe+0x40>
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
cmp    $0x0,%eax
je     11c0 <basicjailissafe+0x40>
movl   $0x0,-0x4(%rbp)
jmp    13b3 <basicjailissafe+0x233>
mov    -0x10(%rbp),%rdi
call   1090 <ending_slash@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x6,%rdi
call   1080 <malloc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
add    $0x6,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1247 <basicjailissafe+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xddd(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     1247 <basicjailissafe+0xc7>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    128b <basicjailissafe+0x10b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xd9e(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     128b <basicjailissafe+0x10b>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    12cf <basicjailissafe+0x14f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xd5f(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     12cf <basicjailissafe+0x14f>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1313 <basicjailissafe+0x193>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xd20(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     1313 <basicjailissafe+0x193>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1357 <basicjailissafe+0x1d7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xce2(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     1357 <basicjailissafe+0x1d7>
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    139b <basicjailissafe+0x21b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %eax,%edi
lea    0xc9d(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <testsafepath@plt>
and    $0xfffffffe,%eax
cmp    $0x0,%eax
je     139b <basicjailissafe+0x21b>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basicjailissafe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     11a5 <basicjailissafe+0x25>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
test   %eax,%eax
jne    1301 <basicjailissafe+0x181>
mov    %rbx,%rdi
call   1090 <ending_slash@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0x6(%rax),%rdi
call   1080 <malloc@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x6(%rax),%rdx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xe11(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
cmp    $0x1,%eax
ja     12f1 <basicjailissafe+0x171>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xde8(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
cmp    $0x1,%eax
ja     12f1 <basicjailissafe+0x171>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xdbd(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
cmp    $0x1,%eax
ja     12f1 <basicjailissafe+0x171>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xd92(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
cmp    $0x1,%eax
ja     12f1 <basicjailissafe+0x171>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xd6c(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
cmp    $0x1,%eax
ja     12f1 <basicjailissafe+0x171>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xd3f(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcat@plt>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <testsafepath@plt>
xor    %ebp,%ebp
cmp    $0x2,%eax
setb   %bpl
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"dev/\00"
@.str.1 = [5 x i8] c"etc/\00"
@.str.2 = [5 x i8] c"lib/\00"
@.str.3 = [5 x i8] c"usr/\00"
@.str.4 = [5 x i8] c"bin/\00"
@.str.5 = [6 x i8] c"sbin/\00"
define i32 @basicjailissafe(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 1, i32* %6
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @testsafepath(i8* %10, i32 0, i32 0)
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %102
14: ; preds = %9, %1
%15 = load i8*, i8** %3
%16 = call i8* @ending_slash(i8* %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %18, 6
%20 = call i32 @malloc(i64 %19)
%21 = load i8*, i8** %5
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 6
%24 = call i8* @memset(i32 %20, i32 0, i64 %23)
store i8* %24, i8** %4
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %36
27: ; preds = %14
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = call i8* @strcat(i32 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @testsafepath(i8* %31, i32 0, i32 0)
%33 = and i32 %32, -2
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 0, i32* %6
br label %36
36: ; preds = %35, %27, %14
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i8*, i8** %4
%41 = load i8*, i8** %5
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = call i8* @strcat(i32 %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @testsafepath(i8* %43, i32 0, i32 0)
%45 = and i32 %44, -2
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %39
store i32 0, i32* %6
br label %48
48: ; preds = %47, %39, %36
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = load i8*, i8** %5
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = call i8* @strcat(i32 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @testsafepath(i8* %55, i32 0, i32 0)
%57 = and i32 %56, -2
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %51
store i32 0, i32* %6
br label %60
60: ; preds = %59, %51, %48
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %72
63: ; preds = %60
%64 = load i8*, i8** %4
%65 = load i8*, i8** %5
%66 = call i32 @strcpy(i8* %64, i8* %65)
%67 = call i8* @strcat(i32 %66, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 @testsafepath(i8* %67, i32 0, i32 0)
%69 = and i32 %68, -2
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %63
store i32 0, i32* %6
br label %72
72: ; preds = %71, %63, %60
%73 = load i32, i32* %6
%74 = icmp eq i32 %73, 1
br i1 %74, label %75, label %84
75: ; preds = %72
%76 = load i8*, i8** %4
%77 = load i8*, i8** %5
%78 = call i32 @strcpy(i8* %76, i8* %77)
%79 = call i8* @strcat(i32 %78, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%80 = call i32 @testsafepath(i8* %79, i32 0, i32 0)
%81 = and i32 %80, -2
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %75
store i32 0, i32* %6
br label %84
84: ; preds = %83, %75, %72
%85 = load i32, i32* %6
%86 = icmp eq i32 %85, 1
br i1 %86, label %87, label %96
87: ; preds = %84
%88 = load i8*, i8** %4
%89 = load i8*, i8** %5
%90 = call i32 @strcpy(i8* %88, i8* %89)
%91 = call i8* @strcat(i32 %90, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%92 = call i32 @testsafepath(i8* %91, i32 0, i32 0)
%93 = and i32 %92, -2
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %87
store i32 0, i32* %6
br label %96
96: ; preds = %95, %87, %84
%97 = load i8*, i8** %4
%98 = call i32 @free(i8* %97)
%99 = load i8*, i8** %5
%100 = call i32 @free(i8* %99)
%101 = load i32, i32* %6
store i32 %101, i32* %2
br label %102
102: ; preds = %96, %13
%103 = load i32, i32* %2
ret i32 %103
}

LLVM IR (-O2):
@.str = [5 x i8] c"dev/\00"
@.str.1 = [5 x i8] c"etc/\00"
@.str.2 = [5 x i8] c"lib/\00"
@.str.3 = [5 x i8] c"usr/\00"
@.str.4 = [5 x i8] c"bin/\00"
@.str.5 = [6 x i8] c"sbin/\00"
define i32 @basicjailissafe(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = tail call i32 @testsafepath(i8* %0, i32 0, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %48
6: ; preds = %3, %1
%7 = tail call i8* @ending_slash(i8* %0)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%9 = add nsw i64 %8, 6
%10 = tail call i32 @malloc(i64 %9)
%11 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%12 = add nsw i64 %11, 6
%13 = tail call i8* @memset(i32 %10, i32 0, i64 %12)
%14 = tail call i32 @strcpy(i8* %13, i8* %7)
%15 = tail call i8* @strcat(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @testsafepath(i8* %15, i32 0, i32 0)
%17 = icmp ult i32 %16, 2
br i1 %17, label %18, label %44
18: ; preds = %6
%19 = tail call i32 @strcpy(i8* %13, i8* %7)
%20 = tail call i8* @strcat(i32 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @testsafepath(i8* %20, i32 0, i32 0)
%22 = icmp ult i32 %21, 2
br i1 %22, label %23, label %44
23: ; preds = %18
%24 = tail call i32 @strcpy(i8* %13, i8* %7)
%25 = tail call i8* @strcat(i32 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @testsafepath(i8* %25, i32 0, i32 0)
%27 = icmp ult i32 %26, 2
br i1 %27, label %28, label %44
28: ; preds = %23
%29 = tail call i32 @strcpy(i8* %13, i8* %7)
%30 = tail call i8* @strcat(i32 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @testsafepath(i8* %30, i32 0, i32 0)
%32 = icmp ult i32 %31, 2
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = tail call i32 @strcpy(i8* %13, i8* %7)
%35 = tail call i8* @strcat(i32 %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%36 = tail call i32 @testsafepath(i8* %35, i32 0, i32 0)
%37 = icmp ult i32 %36, 2
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = tail call i32 @strcpy(i8* %13, i8* %7)
%40 = tail call i8* @strcat(i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = tail call i32 @testsafepath(i8* %40, i32 0, i32 0)
%42 = icmp ult i32 %41, 2
%43 = zext i1 %42 to i32
br label %44
44: ; preds = %38, %6, %18, %23, %28, %33
%45 = phi i32 [ 0, %33 ], [ %43, %38 ], [ 0, %28 ], [ 0, %23 ], [ 0, %18 ], [ 0, %6 ]
%46 = tail call i32 @free(i8* %13)
%47 = tail call i32 @free(i8* %7)
br label %48
48: ; preds = %3, %44
%49 = phi i32 [ %45, %44 ], [ 0, %3 ]
ret i32 %49
}