#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*/ siginfo_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ exe;

/* Variables and functions */
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *, char *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fatal_errno(char *);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
scalar_t__ getpid();
scalar_t__ readlink(char *, char *, int);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
char *xasprintf(char *, scalar_t__);
void gdb_sighandler(int sig, siginfo_t *info) {
  char exe[256];
  if (readlink("/proc/self/exe", exe, sizeof(exe)) < 0)
    fatal_errno("readlink");
  pid_t pp = getpid();
  pid_t p = fork();
  if (p == 0) {
    char *pid = xasprintf("%d", pp);
    execl("/usr/bin/gdb", "gdb", "-ex", "cont", exe, pid, NULL);
    free(pid);
    fatal_errno("execl");
  } else if (p < 0)
    fatal_errno("fork");
  else {
    sleep(1);
    signal(sig, SIG_DFL);
  }
}


x86 assembly (clang, -O0):
<gdb_sighandler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x110(%rbp),%rsi
lea    0xe50(%rip),%rdi
mov    $0x100,%edx
call   1040 <readlink@plt>
cmp    $0x0,%rax
jge    11d0 <gdb_sighandler+0x40>
lea    0xe44(%rip),%rdi
call   1070 <fatal_errno@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x118(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    126a <gdb_sighandler+0xda>
mov    -0x118(%rbp),%rsi
lea    0xe10(%rip),%rdi
call   1080 <xasprintf@plt>
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%r8
mov    -0x128(%rbp),%r9
lea    0xdf2(%rip),%rdi
lea    0xdf4(%rip),%rsi
lea    0xdf1(%rip),%rdx
lea    0xdee(%rip),%rcx
xor    %eax,%eax
movq   $0x0,(%rsp)
call   1090 <execl@plt>
mov    -0x128(%rbp),%rdi
call   1030 <free@plt>
lea    0xdd1(%rip),%rdi
call   1070 <fatal_errno@plt>
jmp    12a9 <gdb_sighandler+0x119>
cmpq   $0x0,-0x120(%rbp)
jge    1289 <gdb_sighandler+0xf9>
lea    0xdb8(%rip),%rdi
call   1070 <fatal_errno@plt>
jmp    12a4 <gdb_sighandler+0x114>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
jmp    12a9 <gdb_sighandler+0x119>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_sighandler>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebp
lea    0xe5e(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1040 <readlink@plt>
test   %rax,%rax
jns    11c2 <gdb_sighandler+0x32>
lea    0xe52(%rip),%rdi
call   1070 <fatal_errno@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1201 <gdb_sighandler+0x71>
js     1262 <gdb_sighandler+0xd2>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
add    $0x118,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <signal@plt>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
call   1080 <xasprintf@plt>
mov    %rax,%rbx
movq   $0x0,(%rsp)
lea    0xdf9(%rip),%rdi
lea    0xdfb(%rip),%rsi
lea    0xdf8(%rip),%rdx
lea    0xdf5(%rip),%rcx
lea    0x10(%rsp),%r8
mov    %rax,%r9
call   1090 <execl@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdde(%rip),%rdi
call   1070 <fatal_errno@plt>
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdce(%rip),%rdi
add    $0x118,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <fatal_errno@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"readlink\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [13 x i8] c"/usr/bin/gdb\00"
@.str.4 = [4 x i8] c"gdb\00"
@.str.5 = [4 x i8] c"-ex\00"
@.str.6 = [5 x i8] c"cont\00"
@.str.7 = [6 x i8] c"execl\00"
@.str.8 = [5 x i8] c"fork\00"
@SIG_DFL = global i32 0
define void @gdb_sighandler(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %3
store i32* %1, i32** %4
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i32 256)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @fatal_errno(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i64 (...) @getpid()
store i64 %15, i64* %6
%16 = call i64 (...) @fork()
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i64, i64* %6
%21 = call i8* @xasprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %20)
store i8* %21, i8** %8
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = load i8*, i8** %8
%24 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %22, i8* %23, i32* null)
%25 = load i8*, i8** %8
%26 = call i32 @free(i8* %25)
%27 = call i32 @fatal_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %39
28: ; preds = %14
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @fatal_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %38
33: ; preds = %28
%34 = call i32 @sleep(i32 1)
%35 = load i32, i32* %3
%36 = load i32, i32* @SIG_DFL
%37 = call i32 @signal(i32 %35, i32 %36)
br label %38
38: ; preds = %33, %31
br label %39
39: ; preds = %38, %19
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [9 x i8] c"readlink\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [13 x i8] c"/usr/bin/gdb\00"
@.str.4 = [4 x i8] c"gdb\00"
@.str.5 = [4 x i8] c"-ex\00"
@.str.6 = [5 x i8] c"cont\00"
@.str.7 = [6 x i8] c"execl\00"
@.str.8 = [5 x i8] c"fork\00"
@SIG_DFL = global i32 0
define void @gdb_sighandler(i32 %0, i32* nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %4, i32 256)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @fatal_errno(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i64 (...) @getpid()
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = tail call i8* @xasprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %10)
%15 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %4, i8* %14, i32* null)
%16 = call i32 @free(i8* %14)
%17 = call i32 @fatal_errno(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %26
18: ; preds = %9
%19 = icmp slt i64 %11, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 @fatal_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %26
22: ; preds = %18
%23 = tail call i32 @sleep(i32 1)
%24 = load i32, i32* @SIG_DFL
%25 = tail call i32 @signal(i32 %0, i32 %24)
br label %26
26: ; preds = %20, %22, %13
ret void
}