#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 *realpath(char *, char *);
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
int valid_pwd(char *cwd, char *env_pwd) {
  char rp_pwd_buf[8192];
  char *rp_pwd = realpath(env_pwd, rp_pwd_buf);
  size_t env_pwd_len = strlen(env_pwd);
  if (rp_pwd == NULL) {
    return 0;
  }
  if (env_pwd[0] != '/') {
    return 0;
  }
  if (strcmp(cwd, rp_pwd) != 0) {
    return 0;
  }
  char *tmp = env_pwd + env_pwd_len - 5;
  if ((strstr(env_pwd, "/../") != NULL) || (strncmp(tmp, "/..", 4) == 0)) {
    return 0;
  }
  tmp = env_pwd + env_pwd_len - 2;
  if ((strstr(env_pwd, "/./") != NULL) || (strncmp(tmp, "/.", 4) == 0)) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<valid_pwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
call   1060 <realpath@plt>
mov    %rax,-0x2028(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x2030(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jne    11a4 <valid_pwd+0x54>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <valid_pwd+0x159>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11c0 <valid_pwd+0x70>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <valid_pwd+0x159>
mov    -0x10(%rbp),%rdi
mov    -0x2028(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11e6 <valid_pwd+0x96>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <valid_pwd+0x159>
mov    -0x18(%rbp),%rax
add    -0x2030(%rbp),%rax
add    $0xfffffffffffffffb,%rax
mov    %rax,-0x2038(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    1238 <valid_pwd+0xe8>
mov    -0x2038(%rbp),%rdi
lea    0xde1(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1244 <valid_pwd+0xf4>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <valid_pwd+0x159>
mov    -0x18(%rbp),%rax
add    -0x2030(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x2038(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    1296 <valid_pwd+0x146>
mov    -0x2038(%rbp),%rdi
lea    0xd8b(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12a2 <valid_pwd+0x152>
movl   $0x0,-0x4(%rbp)
jmp    12a9 <valid_pwd+0x159>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_pwd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <realpath@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %r15d,%r15d
test   %rbp,%rbp
je     11af <valid_pwd+0x5f>
cmpb   $0x2f,(%rbx)
jne    11af <valid_pwd+0x5f>
mov    %rax,%r14
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11af <valid_pwd+0x5f>
lea    0xe5e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c2 <valid_pwd+0x72>
mov    %r15d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
add    %rbx,%r14
lea    -0x5(%r14),%rdi
lea    0xe35(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11af <valid_pwd+0x5f>
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
jne    11af <valid_pwd+0x5f>
add    $0xfffffffffffffffe,%r14
lea    0xe0f(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
setne  %r15b
jmp    11af <valid_pwd+0x5f>

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

LLVM IR (-O2):
@.str = [5 x i8] c"/../\00"
@.str.1 = [4 x i8] c"/..\00"
@.str.2 = [4 x i8] c"/./\00"
@.str.3 = [3 x i8] c"/.\00"
define i32 @valid_pwd(i8* %0, i8* %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = call i8* @realpath(i8* %1, i8* %4)
%6 = call i64 @strlen(i8* dereferenceable(1) %1)
%7 = icmp eq i8* %5, null
br i1 %7, label %30, label %8
8: ; preds = %2
%9 = load i8, i8* %1
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %30
11: ; preds = %8
%12 = call i64 @strcmp(i8* %0, i8* %5)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %1, i64 %6
%16 = call i32* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %15, i64 -5
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %18
%23 = call i32* @strstr(i8* %1, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = getelementptr inbounds i8, i8* %15, i64 -2
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 4)
%28 = icmp ne i64 %27, 0
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %25, %18, %14, %22, %11, %8, %2
%31 = phi i32 [ 0, %2 ], [ 0, %8 ], [ 0, %11 ], [ 0, %18 ], [ 0, %14 ], [ 0, %22 ], [ %29, %25 ]
ret i32 %31
}