#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 */
scalar_t__ dirtycow(char const *, char const *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, const char *argv[]) {
  const char *target_file, *source_file;
  if (argc != 3) {
    printf("usage: <target file>  <src file>\n");
    return 1;
  }
  target_file = argv[1];
  source_file = argv[2];
  printf("[+] start replacing %s by %s\n", target_file, source_file);
  if (dirtycow(target_file, source_file)) {
    printf("ERROR: could not dirtycow %s with %s\n", target_file, source_file);
    return 1;
  }
  printf("[+] %s replaced done!\n", target_file);
  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)
cmpl   $0x3,-0x8(%rbp)
je     115a <main+0x3a>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11da <main+0xba>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <dirtycow@plt>
cmp    $0x0,%rax
je     11c1 <main+0xa1>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11da <main+0xba>
mov    -0x18(%rbp),%rsi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
cmp    $0x3,%edi
jne    1183 <main+0x53>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
lea    0xeb7(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <dirtycow@plt>
test   %rax,%rax
je     1196 <main+0x66>
lea    0xeaf(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
jmp    11a7 <main+0x77>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    11a7 <main+0x77>
lea    0xea7(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"usage: <target file> <src file>\0A\00"
@.str.1 = [30 x i8] c"[+] start replacing %s by %s\0A\00"
@.str.2 = [38 x i8] c"ERROR: could not dirtycow %s with %s\0A\00"
@.str.3 = [23 x i8] c"[+] %s replaced done!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %33
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
store i8* %18, i8** %7
%19 = load i8*, i8** %6
%20 = load i8*, i8** %7
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %20)
%22 = load i8*, i8** %6
%23 = load i8*, i8** %7
%24 = call i64 @dirtycow(i8* %22, i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %12
%27 = load i8*, i8** %6
%28 = load i8*, i8** %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %27, i8* %28)
store i32 1, i32* %3
br label %33
30: ; preds = %12
%31 = load i8*, i8** %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %31)
store i32 0, i32* %3
br label %33
33: ; preds = %30, %26, %10
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str.1 = [30 x i8] c"[+] start replacing %s by %s\0A\00"
@.str.2 = [38 x i8] c"ERROR: could not dirtycow %s with %s\0A\00"
@.str.3 = [23 x i8] c"[+] %s replaced done!\0A\00"
@str = [33 x i8] c"usage: <target file> <src file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %18
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %10)
%12 = tail call i64 @dirtycow(i8* %8, i8* %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* %10)
br label %18
16: ; preds = %6
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %18
18: ; preds = %16, %14, %4
%19 = phi i32 [ 1, %4 ], [ 1, %14 ], [ 0, %16 ]
ret i32 %19
}