#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_TRACEME;
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
pid_t spawn_and_attach_process(char **child_argv) {
  pid_t pid = fork();
  if (!pid) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    execvp(child_argv[0], child_argv);
    fprintf(stderr, "Failed to execute %s\n", child_argv[0]);
  } else {
    int status;
    if (!waitpid(pid, &status, WUNTRACED))
      return 0;
    if (WIFEXITED(status))
      return 0;
    printf("Attached to process %d\n", (int)pid);
  }
  return pid;
}


x86 assembly (clang, -O0):
<spawn_and_attach_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <spawn_and_attach_process+0x67>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <ptrace@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe33(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1234 <spawn_and_attach_process+0xc4>
mov    -0x18(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1060 <waitpid@plt>
cmp    $0x0,%eax
jne    1203 <spawn_and_attach_process+0x93>
movq   $0x0,-0x8(%rbp)
jmp    123c <spawn_and_attach_process+0xcc>
mov    -0x1c(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1222 <spawn_and_attach_process+0xb2>
movq   $0x0,-0x8(%rbp)
jmp    123c <spawn_and_attach_process+0xcc>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xde7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_and_attach_process>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11b2 <spawn_and_attach_process+0x42>
mov    %rax,%rbx
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <waitpid@plt>
test   %eax,%eax
je     11ae <spawn_and_attach_process+0x3e>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11ed <spawn_and_attach_process+0x7d>
xor    %ebx,%ebx
jmp    11fb <spawn_and_attach_process+0x8b>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe1a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11fb <spawn_and_attach_process+0x8b>
lea    0xe22(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PTRACE_TRACEME = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Failed to execute %s\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [24 x i8] c"Attached to process %d\0A\00"
define i64 @spawn_and_attach_process(i8** %0) {
%2 = alloca i64
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
store i8** %0, i8*** %3
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp ne i64 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %1
%10 = load i32, i32* @PTRACE_TRACEME
%11 = call i32 @ptrace(i32 %10, i32 0, i32 0, i32 0)
%12 = load i8**, i8*** %3
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %3
%16 = call i32 @execvp(i8* %14, i8** %15)
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %3
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %37
22: ; preds = %1
%23 = load i64, i64* %4
%24 = load i32, i32* @WUNTRACED
%25 = call i32 @waitpid(i64 %23, i32* %5, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %22
store i64 0, i64* %2
br label %39
28: ; preds = %22
%29 = load i32, i32* %5
%30 = call i64 @WIFEXITED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i64 0, i64* %2
br label %39
33: ; preds = %28
%34 = load i64, i64* %4
%35 = trunc i64 %34 to i32
%36 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %9
%38 = load i64, i64* %4
store i64 %38, i64* %2
br label %39
39: ; preds = %37, %32, %27
%40 = load i64, i64* %2
ret i64 %40
}

LLVM IR (-O2):
@PTRACE_TRACEME = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Failed to execute %s\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [24 x i8] c"Attached to process %d\0A\00"
define i64 @spawn_and_attach_process(i8** %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %1
%6 = load i32, i32* @PTRACE_TRACEME
%7 = tail call i32 @ptrace(i32 %6, i32 0, i32 0, i32 0)
%8 = load i8*, i8** %0
%9 = tail call i32 @execvp(i8* %8, i8** %0)
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %0
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %26
13: ; preds = %1
%14 = bitcast i32* %2 to i8*
%15 = load i32, i32* @WUNTRACED
%16 = call i32 @waitpid(i64 %3, i32* %2, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %13
%19 = load i32, i32* %2
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = trunc i64 %3 to i32
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %26
25: ; preds = %18, %13
br label %26
26: ; preds = %5, %22, %25
%27 = phi i64 [ 0, %25 ], [ %3, %22 ], [ 0, %5 ]
ret i64 %27
}