#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*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ debug_stop(char **);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stack_trace(char **);
int /*<<< orphan*/ stderr;
void debug(int method, char *prog_name) {
  pid_t pid;
  char buf[16];
  char *args[4] = {"gdb", NULL, NULL, NULL};
  int x;
  snprintf(buf, 99, "%d", getpid());
  args[1] = prog_name;
  args[2] = buf;
  pid = fork();
  if (pid == 0) {
    switch (method) {
    case 0:
      fprintf(stderr, "debug_stop\n");
      debug_stop(args);
      break;
    case 1:
      fprintf(stderr, "stack_trace\n");
      stack_trace(args);
      break;
    }
    _exit(0);
  } else if (pid == (pid_t)-1) {
    perror("could not fork");
    return;
  }
  x = 1;
  while (x)
    ;
}


x86 assembly (clang, -O0):
<debug>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c5a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c57(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2c54(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c51(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%ecx
mov    $0x63,%esi
lea    0xe28(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1273 <debug+0xf3>
mov    -0x4(%rbp),%eax
mov    %eax,-0x64(%rbp)
test   %eax,%eax
je     122b <debug+0xab>
jmp    121a <debug+0x9a>
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
je     124e <debug+0xce>
jmp    126c <debug+0xec>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x50(%rbp),%rdi
call   1060 <debug_stop@plt>
jmp    126c <debug+0xec>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <stack_trace@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    128f <debug+0x10f>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
jmp    12aa <debug+0x12a>
jmp    1294 <debug+0x114>
movl   $0x1,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     12aa <debug+0x12a>
jmp    129b <debug+0x11b>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
movaps 0x2c6b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2c4f(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe51(%rip),%rdx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,0x8(%rsp)
mov    %rbp,0x10(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     11f2 <debug+0x72>
test   %rax,%rax
je     1207 <debug+0x87>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
jmp    11f0 <debug+0x70>
lea    0xe27(%rip),%rdi
call   1090 <perror@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0x1,%r14d
je     1236 <debug+0xb6>
test   %r14d,%r14d
jne    1253 <debug+0xd3>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1080 <fprintf@plt>
mov    %rsp,%rdi
call   1060 <debug_stop@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
call   1080 <fprintf@plt>
mov    %rsp,%rdi
call   1040 <stack_trace@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"gdb\00"
@__const.debug.args = [4 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* null, i8* null, i8* null]
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"debug_stop\0A\00"
@.str.3 = [13 x i8] c"stack_trace\0A\00"
@.str.4 = [15 x i8] c"could not fork\00"
define void @debug(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [16 x i8]
%7 = alloca [4 x i8*]
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = bitcast [4 x i8*]* %7 to i8*
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %10, i32 99, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i8*, i8** %4
%14 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 1
store i8* %13, i8** %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 2
store i8* %15, i8** %16
%17 = call i64 (...) @fork()
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %2
%21 = load i32, i32* %3
switch i32 %21, label %32 [
i32 0, label %22
i32 1, label %27
]
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 0
%26 = call i32 @debug_stop(i8** %25)
br label %32
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 0
%31 = call i32 @stack_trace(i8** %30)
br label %32
32: ; preds = %20, %27, %22
%33 = call i32 @_exit(i32 0)
unreachable
34: ; preds = %2
%35 = load i64, i64* %5
%36 = icmp eq i64 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %45
39: ; preds = %34
br label %40
40: ; preds = %39
store i32 1, i32* %8
br label %41
41: ; preds = %44, %40
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
br label %41
45: ; preds = %37, %41
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"gdb\00"
@__const.debug.args = [4 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* null, i8* null, i8* null]
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"debug_stop\0A\00"
@.str.3 = [13 x i8] c"stack_trace\0A\00"
@.str.4 = [15 x i8] c"could not fork\00"
define void @debug(i32 %0, i8* %1) {
%3 = alloca [16 x i8]
%4 = alloca [4 x i8*]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = bitcast [4 x i8*]* %4 to i8*
%7 = tail call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %5, i32 99, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 1
store i8* %1, i8** %9
%10 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 2
store i8* %5, i8** %10
%11 = call i64 (...) @fork()
switch i64 %11, label %27 [
i64 0, label %12
i64 -1, label %25
]
12: ; preds = %2
switch i32 %0, label %23 [
i32 0, label %13
i32 1, label %18
]
13: ; preds = %12
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%17 = call i32 @debug_stop(i8** %16)
br label %23
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%22 = call i32 @stack_trace(i8** %21)
br label %23
23: ; preds = %12, %18, %13
%24 = call i32 @_exit(i32 0)
unreachable
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
ret void
27: ; preds = %2, %27
br label %27
}