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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void invoke_gdb(void) {
  pid_t pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (pid) {
    int status, wpid;
    do {
      wpid = waitpid(pid, &status, WUNTRACED);
      if (wpid == -1) {
        perror("waitpid");
        exit(EXIT_FAILURE);
      }
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  } else {
    char s[13];
    sprintf(s, "%d", getppid());
    execlp("gdb", "gdb", "-p", s, NULL);
  }
}


x86 assembly (clang, -O0):
<invoke_gdb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11c6 <invoke_gdb+0x36>
lea    0xe4d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     1251 <invoke_gdb+0xc1>
jmp    11d5 <invoke_gdb+0x45>
mov    -0x4(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1211 <invoke_gdb+0x81>
lea    0xe07(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1216 <invoke_gdb+0x86>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1a(%rbp)
jne    1241 <invoke_gdb+0xb1>
mov    -0x8(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    11d5 <invoke_gdb+0x45>
jmp    1291 <invoke_gdb+0x101>
lea    -0x19(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xda0(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x19(%rbp),%rcx
lea    0xd93(%rip),%rsi
lea    0xd90(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10a0 <execlp@plt>
jmp    1296 <invoke_gdb+0x106>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invoke_gdb>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11ec <invoke_gdb+0x5c>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1247 <invoke_gdb+0xb7>
mov    0x2e2a(%rip),%r15
mov    %rsp,%r14
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     122d <invoke_gdb+0x9d>
mov    (%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
jne    1223 <invoke_gdb+0x93>
mov    (%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %eax,%eax
je     11c0 <invoke_gdb+0x30>
jmp    1223 <invoke_gdb+0x93>
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe13(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
lea    0xe02(%rip),%rdi
lea    0xdff(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10a0 <execlp@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdd1(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xdb2(%rip),%rdi
jmp    1234 <invoke_gdb+0xa4>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [4 x i8] c"gdb\00"
@.str.4 = [3 x i8] c"-p\00"
define void @invoke_gdb() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [13 x i8]
%5 = call i32 (...) @fork()
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %39
15: ; preds = %12
br label %16
16: ; preds = %36, %15
%17 = load i32, i32* %1
%18 = load i32, i32* @WUNTRACED
%19 = call i32 @waitpid(i32 %17, i32* %2, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
br label %27
27: ; preds = %26
%28 = load i32, i32* %2
%29 = call i32 @WIFEXITED(i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %27
%32 = load i32, i32* %2
%33 = call i32 @WIFSIGNALED(i32 %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br label %36
36: ; preds = %31, %27
%37 = phi i1 [ false, %27 ], [ %35, %31 ]
br i1 %37, label %16, label %38
38: ; preds = %36
br label %45
39: ; preds = %12
%40 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%41 = call i32 (...) @getppid()
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%44 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32* null)
br label %45
45: ; preds = %39, %38
br label %46
46: ; preds = %45
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@WUNTRACED = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [4 x i8] c"gdb\00"
@.str.4 = [3 x i8] c"-p\00"
define void @invoke_gdb() {
%1 = alloca i32
%2 = alloca [13 x i8]
%3 = tail call i32 (...) @fork()
switch i32 %3, label %8 [
i32 -1, label %4
i32 0, label %27
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = bitcast i32* %1 to i8*
br label %10
10: ; preds = %22, %8
%11 = load i32, i32* @WUNTRACED
%12 = call i32 @waitpid(i32 %3, i32* %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = load i32, i32* %1
%20 = call i32 @WIFEXITED(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* %1
%24 = call i32 @WIFSIGNALED(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %10, label %26
26: ; preds = %18, %22
br label %32
27: ; preds = %0
%28 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%29 = tail call i32 (...) @getppid()
%30 = call i32 @sprintf(i8* %28, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %28, i32* null)
br label %32
32: ; preds = %26, %27
ret void
}