#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*/ sprintf(char *, 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;
  sprintf(buf, "%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,%edx
lea    0xe2d(%rip),%rsi
call   1090 <sprintf@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    126e <debug+0xee>
mov    -0x4(%rbp),%eax
mov    %eax,-0x64(%rbp)
test   %eax,%eax
je     1226 <debug+0xa6>
jmp    1215 <debug+0x95>
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
je     1249 <debug+0xc9>
jmp    1267 <debug+0xe7>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x50(%rbp),%rdi
call   1060 <debug_stop@plt>
jmp    1267 <debug+0xe7>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <stack_trace@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    128a <debug+0x10a>
lea    0xda0(%rip),%rdi
call   1080 <perror@plt>
jmp    12a5 <debug+0x125>
jmp    128f <debug+0x10f>
movl   $0x1,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     12a5 <debug+0x125>
jmp    1296 <debug+0x116>
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),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rbx,0x8(%rsp)
mov    %rbp,0x10(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     11e2 <debug+0x62>
test   %rax,%rax
je     11f7 <debug+0x77>
xchg   %ax,%ax
jmp    11e0 <debug+0x60>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0x1,%r14d
je     1226 <debug+0xa6>
test   %r14d,%r14d
jne    1243 <debug+0xc3>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rsp,%rdi
call   1060 <debug_stop@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1070 <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 @sprintf(i8* %10, 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 @sprintf(i8* %5, i8* dereferenceable(1) 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
}