#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;
typedef int /*<<< orphan*/ cmd_buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ system(char *);
void gdb_info(pid_t pid) {
  char respfile[64];
  char cmd_buf[128];
  FILE *f;
  int fd;
  strcpy(respfile, "/tmp/gdb-respfile-XXXXXX");
  if ((fd = mkstemp(respfile)) >= 0 && (f = fdopen(fd, "w")) != NULL) {
    fprintf(f, "attach %d\n", pid);
    fputs("shell echo \"\"\n"
          "shell echo \"* Loaded Libraries\"\n"
          "info sharedlibrary\n"
          "shell echo \"\"\n"
          "shell echo \"* Threads\"\n"
          "info threads\n"
          "shell echo \"\"\n"
          "shell echo \"* FPU Status\"\n"
          "info float\n"
          "shell echo \"\"\n"
          "shell echo \"* Registers\"\n"
          "info registers\n"
          "shell echo \"\"\n"
          "shell echo \"* Backtrace\"\n"
          "thread apply all backtrace full\n"
          "detach\n"
          "quit\n",
          f);
    fflush(f);
    snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s",
             respfile);
    printf("Executing: %s\n", cmd_buf);
    fflush(stdout);
    system(cmd_buf);
    fclose(f);
    remove(respfile);
  } else {
    if (fd >= 0) {
      close(fd);
      remove(respfile);
    }
    puts("!!! Could not create gdb command file");
  }
  fflush(stdout);
}


x86 assembly (clang, -O0):
<gdb_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x50(%rbp),%rdi
call   10e0 <mkstemp@plt>
mov    %eax,-0xdc(%rbp)
cmp    $0x0,%eax
jl     12c2 <gdb_info+0xf2>
mov    -0xdc(%rbp),%edi
lea    0xe06(%rip),%rsi
call   10f0 <fdopen@plt>
mov    %rax,-0xd8(%rbp)
cmp    $0x0,%rax
je     12c2 <gdb_info+0xf2>
mov    -0xd8(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xde1(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0xd8(%rbp),%rsi
lea    0xdd9(%rip),%rdi
call   10a0 <fputs@plt>
mov    -0xd8(%rbp),%rdi
call   10d0 <fflush@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x50(%rbp),%rcx
mov    $0x80,%esi
lea    0xee1(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0xd0(%rbp),%rsi
lea    0xeef(%rip),%rdi
call   1080 <printf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rdi
call   10d0 <fflush@plt>
lea    -0xd0(%rbp),%rdi
call   1070 <system@plt>
mov    -0xd8(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x50(%rbp),%rdi
call   1030 <remove@plt>
jmp    12ef <gdb_info+0x11f>
cmpl   $0x0,-0xdc(%rbp)
jl     12e3 <gdb_info+0x113>
mov    -0xdc(%rbp),%edi
call   10b0 <close@plt>
lea    -0x50(%rbp),%rdi
call   1030 <remove@plt>
lea    0xe9c(%rip),%rdi
call   1050 <puts@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%rdi
call   10d0 <fflush@plt>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_info>:
push   %rbp
push   %r14
push   %rbx
sub    $0xc0,%rsp
mov    %edi,%r14d
lea    0xe1b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   10d0 <mkstemp@plt>
test   %eax,%eax
js     12b5 <gdb_info+0xe5>
mov    %eax,%ebp
lea    0xe10(%rip),%rsi
mov    %eax,%edi
call   10e0 <fdopen@plt>
test   %rax,%rax
je     12a6 <gdb_info+0xd6>
mov    %rax,%rbx
lea    0xdf8(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   10b0 <fprintf@plt>
lea    0xdf1(%rip),%rdi
mov    $0x12f,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10f0 <fwrite@plt>
mov    %rbx,%rdi
call   10c0 <fflush@plt>
lea    0xf00(%rip),%rdx
lea    0x40(%rsp),%rbp
mov    %rsp,%r14
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
call   1090 <snprintf@plt>
lea    0xf02(%rip),%rdi
mov    %rbp,%rsi
call   1080 <printf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%rdi
call   10c0 <fflush@plt>
mov    %rbp,%rdi
call   1070 <system@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
jmp    12c1 <gdb_info+0xf1>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %rsp,%rdi
call   1030 <remove@plt>
lea    0xeca(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%rdi
call   10c0 <fflush@plt>
add    $0xc0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/tmp/gdb-respfile-XXXXXX\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"attach %d\0A\00"
@.str.3 = [304 x i8] c"shell echo \22\22\0Ashell echo \22* Loaded Libraries\22\0Ainfo sharedlibrary\0Ashell echo \22\22\0Ashell echo \22* Threads\22\0Ainfo threads\0Ashell echo \22\22\0Ashell echo \22* FPU Status\22\0Ainfo float\0Ashell echo \22\22\0Ashell echo \22* Registers\22\0Ainfo registers\0Ashell echo \22\22\0Ashell echo \22* Backtrace\22\0Athread apply all backtrace full\0Adetach\0Aquit\0A\00"
@.str.4 = [33 x i8] c"gdb --quiet --batch --command=%s\00"
@.str.5 = [15 x i8] c"Executing: %s\0A\00"
@stdout = global i32* null
@.str.6 = [38 x i8] c"!!! Could not create gdb command file\00"
define void @gdb_info(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %2
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = call i32 @mkstemp(i8* %9)
store i32 %10, i32* %6
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %1
%13 = load i32, i32* %6
%14 = call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %37
16: ; preds = %12
%17 = load i32*, i32** %5
%18 = load i32, i32* %2
%19 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %5
%21 = call i32 @fputs(i8* getelementptr inbounds ([304 x i8], [304 x i8]* @.str.3, i64 0, i64 0), i32* %20)
%22 = load i32*, i32** %5
%23 = call i32 @fflush(i32* %22)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%26 = call i32 @snprintf(i8* %24, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = load i32*, i32** @stdout
%30 = call i32 @fflush(i32* %29)
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%32 = call i32 @system(i8* %31)
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%36 = call i32 @remove(i8* %35)
br label %47
37: ; preds = %12, %1
%38 = load i32, i32* %6
%39 = icmp sge i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%44 = call i32 @remove(i8* %43)
br label %45
45: ; preds = %40, %37
%46 = call i32 @puts(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %45, %16
%48 = load i32*, i32** @stdout
%49 = call i32 @fflush(i32* %48)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"/tmp/gdb-respfile-XXXXXX\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"attach %d\0A\00"
@.str.3 = [304 x i8] c"shell echo \22\22\0Ashell echo \22* Loaded Libraries\22\0Ainfo sharedlibrary\0Ashell echo \22\22\0Ashell echo \22* Threads\22\0Ainfo threads\0Ashell echo \22\22\0Ashell echo \22* FPU Status\22\0Ainfo float\0Ashell echo \22\22\0Ashell echo \22* Registers\22\0Ainfo registers\0Ashell echo \22\22\0Ashell echo \22* Backtrace\22\0Athread apply all backtrace full\0Adetach\0Aquit\0A\00"
@.str.4 = [33 x i8] c"gdb --quiet --batch --command=%s\00"
@.str.5 = [15 x i8] c"Executing: %s\0A\00"
@stdout = global i32* null
@.str.6 = [38 x i8] c"!!! Could not create gdb command file\00"
define void @gdb_info(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @mkstemp(i8* %4)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %26
9: ; preds = %1
%10 = call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%14 = call i64 @fwrite(i8* getelementptr inbounds ([304 x i8], [304 x i8]* @.str.3, i64 0, i64 0), i64 303, i64 1, i32* %10)
%15 = call i32 @fflush(i32* %10)
%16 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%17 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%18 = load i32*, i32** @stdout
%19 = call i32 @fflush(i32* %18)
%20 = call i32 @system(i8* %5)
%21 = call i32 @fclose(i32* %10)
%22 = call i32 @remove(i8* %4)
br label %28
23: ; preds = %9
%24 = call i32 @close(i32 %7)
%25 = call i32 @remove(i8* %4)
br label %26
26: ; preds = %1, %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %28
28: ; preds = %26, %12
%29 = load i32*, i32** @stdout
%30 = call i32 @fflush(i32* %29)
ret void
}