#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 */
int MAP_ANONYMOUS;
long *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ PTRACE_POKEDATA;
int /*<<< orphan*/ fatal(char *);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int, int /*<<< orphan*/,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ pfatal(char *);
int ptrace(int /*<<< orphan*/, int, int, long);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void copypath(int ppid, const char *dir) {
  long *ptr;
  int cnt;
  if ((strlen(dir) + 1) > 4096)
    fatal("directory name too long");
  ptr = (long *)mmap(0, 4096, PROT_READ | PROT_WRITE,
                     MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0);
  if (ptr == MAP_FAILED)
    pfatal("mmap");
  strncpy((char *)ptr, dir, 4096);
  for (cnt = 0; cnt < (4096 / sizeof(long)); cnt++)
    if (ptrace(PTRACE_POKEDATA, ppid, 65536 + (cnt * sizeof(long)), ptr[cnt]) ==
        -1)
      pfatal("ptrace");
  munmap((void *)ptr, 4096);
}


x86 assembly (clang, -O0):
<copypath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    $0x1000,%eax
jle    11a2 <copypath+0x32>
lea    0xe63(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%rip),%rax
or     (%rax),%edx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    0x2df4(%rip),%rax
or     (%rax),%ecx
mov    0x2df3(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
mov    %r9d,%r8d
call   1050 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <copypath+0x96>
lea    0xe17(%rip),%rdi
call   1060 <pfatal@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x200,%rax
jae    1284 <copypath+0x114>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
add    $0x10000,%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1271 <copypath+0x101>
lea    0xdb1(%rip),%rdi
call   1060 <pfatal@plt>
jmp    1276 <copypath+0x106>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121f <copypath+0xaf>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1080 <munmap@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copypath>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x1000,%eax
jl     1199 <copypath+0x29>
lea    0xe6c(%rip),%rdi
call   1090 <fatal@plt>
mov    0x2e58(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dfc(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dce(%rip),%rax
cmp    %rbx,(%rax)
jne    11f3 <copypath+0x83>
lea    0xe2a(%rip),%rdi
call   1060 <pfatal@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
xor    %ebp,%ebp
mov    0x2dc4(%rip),%r12
lea    0xe0a(%rip),%r14
jmp    122d <copypath+0xbd>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x8,%rbp
cmp    $0x1000,%rbp
je     1252 <copypath+0xe2>
mov    (%r12),%edi
mov    (%rbx,%rbp,1),%rcx
lea    0x10000(%rbp),%edx
mov    %r15d,%esi
call   1070 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1220 <copypath+0xb0>
mov    %r14,%rdi
call   1060 <pfatal@plt>
jmp    1220 <copypath+0xb0>
mov    %rbx,%rdi
mov    $0x1000,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <munmap@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"directory name too long\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64* null
@.str.1 = [5 x i8] c"mmap\00"
@PTRACE_POKEDATA = global i32 0
@.str.2 = [7 x i8] c"ptrace\00"
define void @copypath(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = icmp sgt i32 %9, 4096
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = load i32, i32* @MAP_FIXED
%19 = or i32 %17, %18
%20 = load i32, i32* @MAP_PRIVATE
%21 = or i32 %19, %20
%22 = call i64 @mmap(i32 0, i32 4096, i32 %16, i32 %21, i32 0, i32 0)
%23 = inttoptr i64 %22 to i64*
store i64* %23, i64** %5
%24 = load i64*, i64** %5
%25 = load i64*, i64** @MAP_FAILED
%26 = icmp eq i64* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = call i32 @pfatal(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %13
%30 = load i64*, i64** %5
%31 = bitcast i64* %30 to i8*
%32 = load i8*, i8** %4
%33 = call i32 @strncpy(i8* %31, i8* %32, i32 4096)
store i32 0, i32* %6
br label %34
34: ; preds = %56, %29
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = icmp ult i64 %36, 512
br i1 %37, label %38, label %59
38: ; preds = %34
%39 = load i32, i32* @PTRACE_POKEDATA
%40 = load i32, i32* %3
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = mul i64 %42, 8
%44 = add i64 65536, %43
%45 = trunc i64 %44 to i32
%46 = load i64*, i64** %5
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i64, i64* %46, i64 %48
%50 = load i64, i64* %49
%51 = call i32 @ptrace(i32 %39, i32 %40, i32 %45, i64 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %38
%54 = call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %38
br label %56
56: ; preds = %55
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
store i32 %58, i32* %6
br label %34
59: ; preds = %34
%60 = load i64*, i64** %5
%61 = bitcast i64* %60 to i8*
%62 = call i32 @munmap(i8* %61, i32 4096)
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"directory name too long\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64* null
@.str.1 = [5 x i8] c"mmap\00"
@PTRACE_POKEDATA = global i32 0
@.str.2 = [7 x i8] c"ptrace\00"
define void @copypath(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 4095
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = load i32, i32* @MAP_FIXED
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_PRIVATE
%15 = or i32 %13, %14
%16 = tail call i64 @mmap(i32 0, i32 4096, i32 %10, i32 %15, i32 0, i32 0)
%17 = inttoptr i64 %16 to i64*
%18 = load i64*, i64** @MAP_FAILED
%19 = icmp eq i64* %18, %17
br i1 %19, label %20, label %22
20: ; preds = %7
%21 = tail call i32 @pfatal(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %7
%23 = inttoptr i64 %16 to i8*
%24 = tail call i32 @strncpy(i8* %23, i8* %1, i32 4096)
br label %25
25: ; preds = %22, %37
%26 = phi i64 [ 0, %22 ], [ %38, %37 ]
%27 = load i32, i32* @PTRACE_POKEDATA
%28 = getelementptr inbounds i64, i64* %17, i64 %26
%29 = load i64, i64* %28
%30 = trunc i64 %26 to i32
%31 = shl i32 %30, 3
%32 = add i32 %31, 65536
%33 = tail call i32 @ptrace(i32 %27, i32 %0, i32 %32, i64 %29)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %25
%36 = tail call i32 @pfatal(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %25, %35
%38 = add nuw nsw i64 %26, 1
%39 = icmp eq i64 %38, 512
br i1 %39, label %40, label %25
40: ; preds = %37
%41 = tail call i32 @munmap(i8* %23, i32 4096)
ret void
}