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

/* Variables and functions */
int /*<<< orphan*/ bu_extra_argument(char *);
scalar_t__ bu_handle_version(int, char **);
char *getcwd(char *, int);
char *getenv(char *);
scalar_t__ has_arg(int, char **, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ valid_pwd(char *, char *);
int main(int argc, char **argv) {
  int dash_p = 0;
  if (has_arg(argc, argv, "-h") || has_arg(argc, argv, "--help")) {
    puts("Usage: pwd [-L|-P]\n");
    puts("Print the name of the current working directory.");
    return 1;
  }
  if (bu_handle_version(argc, argv)) {
    return 0;
  }
  for (int i = 1; i < argc; i++) {
    if (strncmp(argv[i], "-P", 3) == 0) {
      dash_p = 1;
    } else if (strncmp(argv[i], "-L", 3) == 0) {
      dash_p = 0;
    } else {
      bu_extra_argument(argv[0]);
      return 1;
    }
  }
  char buf[8192];
  char *env_pwd = getenv("PWD");
  char *cwd = getcwd(buf, sizeof(buf));
  char *ret = NULL;
  if (!dash_p && (env_pwd != NULL) && (env_pwd[0] != '\0') &&
      valid_pwd(cwd, env_pwd)) {
    ret = env_pwd;
  } else {
    ret = cwd;
  }
  if (ret == NULL) {
    perror(argv[0]);
    return 1;
  }
  puts(ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe42(%rip),%rdx
call   1060 <has_arg@plt>
cmp    $0x0,%rax
jne    11ea <main+0x5a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe28(%rip),%rdx
call   1060 <has_arg@plt>
cmp    $0x0,%rax
je     120e <main+0x7e>
lea    0xe19(%rip),%rdi
call   1050 <puts@plt>
lea    0xe21(%rip),%rdi
call   1050 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b1 <main+0x221>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10b0 <bu_handle_version@plt>
cmp    $0x0,%rax
je     1230 <main+0xa0>
movl   $0x0,-0x4(%rbp)
jmp    13b1 <main+0x221>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12d9 <main+0x149>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdf9(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1276 <main+0xe6>
movl   $0x1,-0x14(%rbp)
jmp    12c6 <main+0x136>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdc9(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12a9 <main+0x119>
movl   $0x0,-0x14(%rbp)
jmp    12c1 <main+0x131>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <bu_extra_argument@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b1 <main+0x221>
jmp    12c6 <main+0x136>
jmp    12cb <main+0x13b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1237 <main+0xa7>
lea    0xd75(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x2028(%rbp)
lea    -0x2020(%rbp),%rdi
mov    $0x2000,%esi
call   1080 <getcwd@plt>
mov    %rax,-0x2030(%rbp)
movq   $0x0,-0x2038(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    136a <main+0x1da>
cmpq   $0x0,-0x2028(%rbp)
je     136a <main+0x1da>
mov    -0x2028(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     136a <main+0x1da>
mov    -0x2030(%rbp),%rdi
mov    -0x2028(%rbp),%rsi
call   1070 <valid_pwd@plt>
cmp    $0x0,%rax
je     136a <main+0x1da>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x2038(%rbp)
jmp    1378 <main+0x1e8>
mov    -0x2030(%rbp),%rax
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
jne    139e <main+0x20e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b1 <main+0x221>
mov    -0x2038(%rbp),%rdi
call   1050 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe53(%rip),%rdx
call   1060 <has_arg@plt>
test   %rax,%rax
jne    11cd <main+0x3d>
lea    0xe45(%rip),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <has_arg@plt>
test   %rax,%rax
je     11fc <main+0x6c>
lea    0xe36(%rip),%rdi
call   1050 <puts@plt>
lea    0xe3e(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <bu_handle_version@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11ea <main+0x5a>
xor    %r13d,%r13d
cmp    $0x2,%ebp
jl     127b <main+0xeb>
mov    %ebp,%r12d
mov    $0x1,%ebp
lea    0xe28(%rip),%r15
lea    0xe24(%rip),%r14
jmp    123f <main+0xaf>
mov    $0x1,%r13d
add    $0x1,%rbp
cmp    %rbp,%r12
je     127b <main+0xeb>
mov    (%rbx,%rbp,8),%rdi
mov    %r15,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1230 <main+0xa0>
mov    (%rbx,%rbp,8),%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1236 <main+0xa6>
mov    (%rbx),%rdi
call   1090 <bu_extra_argument@plt>
jmp    11e5 <main+0x55>
lea    0xdd3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x2000,%esi
call   1080 <getcwd@plt>
mov    %rax,%r14
test   %r13d,%r13d
jne    12ba <main+0x12a>
test   %rbp,%rbp
je     12ba <main+0x12a>
cmpb   $0x0,0x0(%rbp)
je     12ba <main+0x12a>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <valid_pwd@plt>
test   %rax,%rax
jne    12c2 <main+0x132>
mov    %r14,%rbp
test   %r14,%r14
je     12d1 <main+0x141>
mov    %rbp,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
jmp    11ea <main+0x5a>
mov    (%rbx),%rdi
call   10a0 <perror@plt>
jmp    11e5 <main+0x55>

LLVM IR (-O0):
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@.str.2 = [20 x i8] c"Usage: pwd [-L|-P]\0A\00"
@.str.3 = [49 x i8] c"Print the name of the current working directory.\00"
@.str.4 = [3 x i8] c"-P\00"
@.str.5 = [3 x i8] c"-L\00"
@.str.6 = [4 x i8] c"PWD\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i64 @has_arg(i32 %12, i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %2
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i64 @has_arg(i32 %17, i8** %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16, %2
%22 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @puts(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %98
24: ; preds = %16
%25 = load i32, i32* %4
%26 = load i8**, i8*** %5
%27 = call i64 @bu_handle_version(i32 %25, i8** %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 0, i32* %3
br label %98
30: ; preds = %24
store i32 1, i32* %7
br label %31
31: ; preds = %60, %30
%32 = load i32, i32* %7
%33 = load i32, i32* %4
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %63
35: ; preds = %31
%36 = load i8**, i8*** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i64 @strncmp(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
store i32 1, i32* %6
br label %59
44: ; preds = %35
%45 = load i8**, i8*** %5
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
%49 = load i8*, i8** %48
%50 = call i64 @strncmp(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 3)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %44
store i32 0, i32* %6
br label %58
53: ; preds = %44
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = call i32 @bu_extra_argument(i8* %56)
store i32 1, i32* %3
br label %98
58: ; preds = %52
br label %59
59: ; preds = %58, %43
br label %60
60: ; preds = %59
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
br label %31
63: ; preds = %31
%64 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
store i8* %64, i8** %9
%65 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%66 = call i8* @getcwd(i8* %65, i32 8192)
store i8* %66, i8** %10
store i8* null, i8** %11
%67 = load i32, i32* %6
%68 = icmp ne i32 %67, 0
br i1 %68, label %85, label %69
69: ; preds = %63
%70 = load i8*, i8** %9
%71 = icmp ne i8* %70, null
br i1 %71, label %72, label %85
72: ; preds = %69
%73 = load i8*, i8** %9
%74 = getelementptr inbounds i8, i8* %73, i64 0
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %85
78: ; preds = %72
%79 = load i8*, i8** %10
%80 = load i8*, i8** %9
%81 = call i64 @valid_pwd(i8* %79, i8* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %78
%84 = load i8*, i8** %9
store i8* %84, i8** %11
br label %87
85: ; preds = %78, %72, %69, %63
%86 = load i8*, i8** %10
store i8* %86, i8** %11
br label %87
87: ; preds = %85, %83
%88 = load i8*, i8** %11
%89 = icmp eq i8* %88, null
br i1 %89, label %90, label %95
90: ; preds = %87
%91 = load i8**, i8*** %5
%92 = getelementptr inbounds i8*, i8** %91, i64 0
%93 = load i8*, i8** %92
%94 = call i32 @perror(i8* %93)
store i32 1, i32* %3
br label %98
95: ; preds = %87
%96 = load i8*, i8** %11
%97 = call i32 @puts(i8* %96)
store i32 0, i32* %3
br label %98
98: ; preds = %95, %90, %53, %29, %21
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
@.str = [3 x i8] c"-h\00"
@.str.1 = [7 x i8] c"--help\00"
@.str.2 = [20 x i8] c"Usage: pwd [-L|-P]\0A\00"
@.str.3 = [49 x i8] c"Print the name of the current working directory.\00"
@.str.4 = [3 x i8] c"-P\00"
@.str.5 = [3 x i8] c"-L\00"
@.str.6 = [4 x i8] c"PWD\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [8192 x i8]
%4 = tail call i64 @has_arg(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i64 @has_arg(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6, %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %60
12: ; preds = %6
%13 = tail call i64 @bu_handle_version(i32 %0, i8** %1)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %60
15: ; preds = %12
%16 = icmp sgt i32 %0, 1
br i1 %16, label %17, label %36
17: ; preds = %15
%18 = zext i32 %0 to i64
br label %19
19: ; preds = %17, %32
%20 = phi i64 [ 1, %17 ], [ %34, %32 ]
%21 = getelementptr inbounds i8*, i8** %1, i64 %20
%22 = load i8*, i8** %21
%23 = tail call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %19
%26 = load i8*, i8** %21
%27 = tail call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i8*, i8** %1
%31 = tail call i32 @bu_extra_argument(i8* %30)
br label %60
32: ; preds = %25, %19
%33 = phi i32 [ 1, %19 ], [ 0, %25 ]
%34 = add nuw nsw i64 %20, 1
%35 = icmp eq i64 %34, %18
br i1 %35, label %36, label %19
36: ; preds = %32, %15
%37 = phi i32 [ 0, %15 ], [ %33, %32 ]
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%39 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%40 = call i8* @getcwd(i8* %38, i32 8192)
%41 = icmp eq i32 %37, 0
%42 = icmp ne i8* %39, null
%43 = and i1 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %36
%45 = load i8, i8* %39
%46 = icmp eq i8 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = call i64 @valid_pwd(i8* %40, i8* %39)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %36, %44, %47
%51 = icmp eq i8* %40, null
br i1 %51, label %52, label %55
52: ; preds = %50
%53 = load i8*, i8** %1
%54 = call i32 @perror(i8* %53)
br label %58
55: ; preds = %47, %50
%56 = phi i8* [ %40, %50 ], [ %39, %47 ]
%57 = call i32 @puts(i8* %56)
br label %58
58: ; preds = %55, %52
%59 = phi i32 [ 1, %52 ], [ 0, %55 ]
br label %60
60: ; preds = %29, %12, %58, %9
%61 = phi i32 [ 1, %9 ], [ %59, %58 ], [ 0, %12 ], [ 1, %29 ]
ret i32 %61
}