#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*/ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ PTRACE_POKETEXT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, ...);
int /*<<< orphan*/ kill_child(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int do_parent(char const *program_name, char const *executable_name,
              pid_t child_pid) {
  int child_status;
  int instr;
  puts("Parent: waiting for child ... ");
  if (waitpid(child_pid, &child_status, 0) < 0) {
    fprintf(stderr, "%s: could not wait for %s due to %s\n", program_name,
            executable_name, strerror(errno));
    goto could_not_wait;
  }
  printf("Extracting instr ...\n");
  if (((instr = ptrace(PTRACE_PEEKTEXT, child_pid, 0x1038c, 0)) < 0) &&
      errno != 0) {
    fprintf(stderr, "%s: could not determine instr due to %s\n", program_name,
            strerror(errno));
    goto could_not_determine_instr;
  }
  printf("instr= %x\n", instr);
  if ((ptrace(PTRACE_POKETEXT, child_pid, 0x1038c, instr) < 0) && errno != 0) {
    fprintf(stderr, "%s: could not write instr due to %s\n", program_name,
            strerror(errno));
    goto could_not_determine_instr;
  }
  printf("Continuing ...\n");
  if (ptrace(PTRACE_CONT, child_pid, 0, 0) < 0) {
    fprintf(stderr, "%s: could not continue %s due to %s\n", program_name,
            executable_name, strerror(errno));
    goto could_not_continue;
  }
  return EXIT_SUCCESS;
could_not_continue:
could_not_determine_instr:
could_not_wait:
  kill_child(child_pid);
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<do_parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jge    11f6 <do_parent+0x86>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    %rax,%r8
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1390 <do_parent+0x220>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
mov    $0x1038c,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1279 <do_parent+0x109>
mov    0x2da9(%rip),%rax
cmpq   $0x0,(%rax)
je     1279 <do_parent+0x109>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    138b <do_parent+0x21b>
mov    -0x24(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%ecx
mov    $0x1038c,%edx
call   1070 <ptrace@plt>
cmp    $0x0,%eax
jge    12fd <do_parent+0x18d>
mov    0x2d25(%rip),%rax
cmpq   $0x0,(%rax)
je     12fd <do_parent+0x18d>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    138b <do_parent+0x21b>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <ptrace@plt>
cmp    $0x0,%eax
jge    1375 <do_parent+0x205>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c8c(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%rcx
mov    %rax,%r8
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1386 <do_parent+0x216>
mov    0x2c44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a4 <do_parent+0x234>
jmp    138b <do_parent+0x21b>
jmp    1390 <do_parent+0x220>
mov    -0x1c(%rbp),%edi
call   1040 <kill_child@plt>
mov    0x2c31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_parent>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %rax,%rax
js     11f3 <do_parent+0x83>
lea    0xf18(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    $0x1038c,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
mov    %eax,%ebp
mov    0x2e09(%rip),%r12
test   %eax,%eax
jns    1226 <do_parent+0xb6>
mov    (%r12),%rdi
test   %rdi,%rdi
je     1226 <do_parent+0xb6>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ebp
call   1090 <strerror@plt>
lea    0xe53(%rip),%rsi
jmp    126f <do_parent+0xff>
mov    0x2de6(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd5(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe0d(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127e <do_parent+0x10e>
lea    0xe40(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    $0x1038c,%edx
mov    %ebp,%ecx
call   1070 <ptrace@plt>
test   %eax,%eax
jns    129b <do_parent+0x12b>
mov    (%r12),%rdi
test   %rdi,%rdi
je     129b <do_parent+0x12b>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ebp
call   1090 <strerror@plt>
lea    0xe09(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <kill_child@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
test   %eax,%eax
js     12c8 <do_parent+0x158>
mov    0x2cfa(%rip),%rax
jmp    128c <do_parent+0x11c>
mov    0x2d11(%rip),%rax
mov    (%rax),%ebp
mov    (%r12),%rdi
call   1090 <strerror@plt>
lea    0xdbc(%rip),%rsi
jmp    1212 <do_parent+0xa2>

LLVM IR (-O0):
@.str = [31 x i8] c"Parent: waiting for child ... \00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"%s: could not wait for %s due to %s\0A\00"
@errno = global i64 0
@.str.2 = [22 x i8] c"Extracting instr ...\0A\00"
@PTRACE_PEEKTEXT = global i32 0
@.str.3 = [41 x i8] c"%s: could not determine instr due to %s\0A\00"
@.str.4 = [11 x i8] c"instr= %x\0A\00"
@PTRACE_POKETEXT = global i32 0
@.str.5 = [37 x i8] c"%s: could not write instr due to %s\0A\00"
@.str.6 = [16 x i8] c"Continuing ...\0A\00"
@PTRACE_CONT = global i32 0
@.str.7 = [37 x i8] c"%s: could not continue %s due to %s\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @do_parent(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %7
%12 = call i64 @waitpid(i32 %11, i32* %8, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i64, i64* @errno
%19 = call i8* @strerror(i64 %18)
%20 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %17, i8* %19)
br label %70
21: ; preds = %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @PTRACE_PEEKTEXT
%24 = load i32, i32* %7
%25 = call i32 @ptrace(i32 %23, i32 %24, i32 66444, i32 0)
store i32 %25, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %5
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %34)
br label %69
36: ; preds = %27, %21
%37 = load i32, i32* %9
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @PTRACE_POKETEXT
%40 = load i32, i32* %7
%41 = load i32, i32* %9
%42 = call i32 @ptrace(i32 %39, i32 %40, i32 66444, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %36
%45 = load i64, i64* @errno
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %5
%50 = load i64, i64* @errno
%51 = call i8* @strerror(i64 %50)
%52 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %49, i8* %51)
br label %69
53: ; preds = %44, %36
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @PTRACE_CONT
%56 = load i32, i32* %7
%57 = call i32 @ptrace(i32 %55, i32 %56, i32 0, i32 0)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %53
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %5
%62 = load i8*, i8** %6
%63 = load i64, i64* @errno
%64 = call i8* @strerror(i64 %63)
%65 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %61, i8* %62, i8* %64)
br label %68
66: ; preds = %53
%67 = load i32, i32* @EXIT_SUCCESS
store i32 %67, i32* %4
br label %74
68: ; preds = %59
br label %69
69: ; preds = %68, %47, %30
br label %70
70: ; preds = %69, %14
%71 = load i32, i32* %7
%72 = call i32 @kill_child(i32 %71)
%73 = load i32, i32* @EXIT_FAILURE
store i32 %73, i32* %4
br label %74
74: ; preds = %70, %66
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [31 x i8] c"Parent: waiting for child ... \00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"%s: could not wait for %s due to %s\0A\00"
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
@.str.3 = [41 x i8] c"%s: could not determine instr due to %s\0A\00"
@.str.4 = [11 x i8] c"instr= %x\0A\00"
@PTRACE_POKETEXT = global i32 0
@.str.5 = [37 x i8] c"%s: could not write instr due to %s\0A\00"
@PTRACE_CONT = global i32 0
@.str.7 = [37 x i8] c"%s: could not continue %s due to %s\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
@str = [21 x i8] c"Extracting instr ...\00"
@str.8 = [15 x i8] c"Continuing ...\00"
define i32 @do_parent(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @waitpid(i32 %2, i32* %4, i32 0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i64, i64* @errno
%12 = call i8* @strerror(i64 %11)
%13 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i8* %12)
br label %48
14: ; preds = %3
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @PTRACE_PEEKTEXT
%17 = call i32 @ptrace(i32 %16, i32 %2, i32 66444, i32 0)
%18 = icmp slt i32 %17, 0
%19 = load i64, i64* @errno
%20 = icmp ne i64 %19, 0
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = call i8* @strerror(i64 %19)
%25 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %24)
br label %48
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%28 = load i32, i32* @PTRACE_POKETEXT
%29 = call i32 @ptrace(i32 %28, i32 %2, i32 66444, i32 %17)
%30 = icmp slt i32 %29, 0
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = call i8* @strerror(i64 %31)
%37 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %36)
br label %48
38: ; preds = %26
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%40 = load i32, i32* @PTRACE_CONT
%41 = call i32 @ptrace(i32 %40, i32 %2, i32 0, i32 0)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i64, i64* @errno
%46 = call i8* @strerror(i64 %45)
%47 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %1, i8* %46)
br label %48
48: ; preds = %22, %34, %43, %9
%49 = call i32 @kill_child(i32 %2)
br label %50
50: ; preds = %38, %48
%51 = phi i32* [ @EXIT_FAILURE, %48 ], [ @EXIT_SUCCESS, %38 ]
%52 = load i32, i32* %51
ret i32 %52
}