#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ PTRACE_SYSCALL;
scalar_t__ SIGTRAP;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WIFSTOPPED(int);
scalar_t__ WSTOPSIG(int);
scalar_t__ changepdir(scalar_t__, int, char *);
int /*<<< orphan*/ fatal(char *);
scalar_t__ getppid();
int /*<<< orphan*/ pfatal(char *);
int ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/);
scalar_t__ wait(int *);
int main(int argc, char **argv) {
  int status, count = 0;
  pid_t ppid;
  if (argc != 2)
    fatal("Usage: cd [dir]");
  ppid = getppid();
  if (ptrace(PTRACE_ATTACH, ppid, 0, 0) == -1)
    pfatal("ptrace");
  for (;;) {
    if (wait(&status) != ppid)
      continue;
    if (WIFEXITED(status))
      fatal("Process finished\n");
    if (WIFSIGNALED(status))
      fatal("Process finished with signal");
    if (!WIFSTOPPED(status))
      continue;
    if (WSTOPSIG(status) == SIGTRAP)
      count += changepdir(ppid, count, argv[1]);
    if (count == 4) {
      ptrace(PTRACE_DETACH, ppid, 0, 0);
      return 0;
    }
    if (ptrace(PTRACE_SYSCALL, ppid, 0, 0) == -1)
      pfatal("ptrace");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d3 <main+0x33>
lea    0xe32(%rip),%rdi
call   10a0 <fatal@plt>
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1209 <main+0x69>
lea    0xe0c(%rip),%rdi
call   1030 <pfatal@plt>
jmp    120e <main+0x6e>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
cmp    -0x20(%rbp),%rax
je     1226 <main+0x86>
jmp    120e <main+0x6e>
mov    -0x14(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1244 <main+0xa4>
lea    0xdd8(%rip),%rdi
call   10a0 <fatal@plt>
mov    -0x14(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1262 <main+0xc2>
lea    0xdcc(%rip),%rdi
call   10a0 <fatal@plt>
mov    -0x14(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%eax
jne    1278 <main+0xd8>
jmp    120e <main+0x6e>
mov    -0x14(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <main+0x111>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1060 <changepdir@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jne    12d9 <main+0x139>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <ptrace@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1304 <main+0x164>
lea    0xd11(%rip),%rdi
call   1030 <pfatal@plt>
jmp    120e <main+0x6e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
cmp    $0x2,%edi
je     11c4 <main+0x24>
lea    0xe41(%rip),%rdi
call   10a0 <fatal@plt>
xor    %r15d,%r15d
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %rax,%rbx
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    11f7 <main+0x57>
lea    0xe1e(%rip),%rdi
call   1030 <pfatal@plt>
lea    0xc(%rsp),%rbp
mov    0x2dcd(%rip),%r14
lea    0xe1f(%rip),%r13
lea    0xe06(%rip),%r12
jmp    122d <main+0x8d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %eax,%eax
jne    1268 <main+0xc8>
mov    %rbp,%rdi
call   10c0 <wait@plt>
cmp    %rbx,%rax
jne    122d <main+0x8d>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     1250 <main+0xb0>
mov    %r12,%rdi
call   10a0 <fatal@plt>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     1220 <main+0x80>
mov    %r13,%rdi
call   10a0 <fatal@plt>
jmp    1220 <main+0x80>
mov    0xc(%rsp),%edi
call   10b0 <WSTOPSIG@plt>
cmp    (%r14),%rax
jne    1290 <main+0xf0>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1060 <changepdir@plt>
add    %r15d,%eax
mov    %eax,%r15d
cmp    $0x4,%r15d
je     12c5 <main+0x125>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0x8d>
lea    0xd55(%rip),%rdi
call   1030 <pfatal@plt>
jmp    122d <main+0x8d>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Usage: cd [dir]\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [7 x i8] c"ptrace\00"
@.str.2 = [18 x i8] c"Process finished\0A\00"
@.str.3 = [29 x i8] c"Process finished with signal\00"
@SIGTRAP = global i64 0
@PTRACE_DETACH = global i32 0
@PTRACE_SYSCALL = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i64 (...) @getppid()
store i64 %14, i64* %8
%15 = load i32, i32* @PTRACE_ATTACH
%16 = load i64, i64* %8
%17 = call i32 @ptrace(i32 %15, i64 %16, i32 0, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
br label %22
22: ; preds = %74, %43, %26, %21
%23 = call i64 @wait(i32* %6)
%24 = load i64, i64* %8
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %22
27: ; preds = %22
%28 = load i32, i32* %6
%29 = call i64 @WIFEXITED(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i32, i32* %6
%35 = call i64 @WIFSIGNALED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = load i32, i32* %6
%41 = call i32 @WIFSTOPPED(i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %39
br label %22
44: ; preds = %39
%45 = load i32, i32* %6
%46 = call i64 @WSTOPSIG(i32 %45)
%47 = load i64, i64* @SIGTRAP
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %60
49: ; preds = %44
%50 = load i64, i64* %8
%51 = load i32, i32* %7
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = call i64 @changepdir(i64 %50, i32 %51, i8* %54)
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = add nsw i64 %57, %55
%59 = trunc i64 %58 to i32
store i32 %59, i32* %7
br label %60
60: ; preds = %49, %44
%61 = load i32, i32* %7
%62 = icmp eq i32 %61, 4
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* @PTRACE_DETACH
%65 = load i64, i64* %8
%66 = call i32 @ptrace(i32 %64, i64 %65, i32 0, i32 0)
ret i32 0
67: ; preds = %60
%68 = load i32, i32* @PTRACE_SYSCALL
%69 = load i64, i64* %8
%70 = call i32 @ptrace(i32 %68, i64 %69, i32 0, i32 0)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %74
74: ; preds = %72, %67
br label %22
}

LLVM IR (-O2):
@.str = [16 x i8] c"Usage: cd [dir]\00"
@PTRACE_ATTACH = global i32 0
@.str.1 = [7 x i8] c"ptrace\00"
@.str.2 = [18 x i8] c"Process finished\0A\00"
@.str.3 = [29 x i8] c"Process finished with signal\00"
@SIGTRAP = global i64 0
@PTRACE_DETACH = global i32 0
@PTRACE_SYSCALL = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = tail call i64 (...) @getppid()
%10 = load i32, i32* @PTRACE_ATTACH
%11 = tail call i32 @ptrace(i32 %10, i64 %9, i32 0, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = getelementptr inbounds i8*, i8** %1, i64 1
br label %17
17: ; preds = %61, %15
%18 = phi i32 [ 0, %15 ], [ %50, %61 ]
br label %19
19: ; preds = %22, %17
%20 = call i64 @wait(i32* %3)
%21 = icmp eq i64 %20, %9
br i1 %21, label %23, label %22
22: ; preds = %19, %35
br label %19
23: ; preds = %19
%24 = load i32, i32* %3
%25 = call i64 @WIFEXITED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = load i32, i32* %3
%31 = call i64 @WIFSIGNALED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %3
%37 = call i32 @WIFSTOPPED(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %22, label %39
39: ; preds = %35
%40 = load i32, i32* %3
%41 = call i64 @WSTOPSIG(i32 %40)
%42 = load i64, i64* @SIGTRAP
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i8*, i8** %16
%46 = call i64 @changepdir(i64 %9, i32 %18, i8* %45)
%47 = trunc i64 %46 to i32
%48 = add i32 %18, %47
br label %49
49: ; preds = %44, %39
%50 = phi i32 [ %48, %44 ], [ %18, %39 ]
%51 = icmp eq i32 %50, 4
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* @PTRACE_DETACH
%54 = call i32 @ptrace(i32 %53, i64 %9, i32 0, i32 0)
ret i32 0
55: ; preds = %49
%56 = load i32, i32* @PTRACE_SYSCALL
%57 = call i32 @ptrace(i32 %56, i64 %9, i32 0, i32 0)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %61
61: ; preds = %59, %55
br label %17
}