#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 mkstemp(char *);
int /*<<< orphan*/ printf(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, "gdb-respfile-XXXXXX");
  if ((fd = mkstemp(respfile)) >= 0 && (f = fdopen(fd, "w")) != NULL) {
    fprintf(f,
            "attach %d\n"
            "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",
            pid);
    fclose(f);
    snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s",
             respfile);
    printf("Executing: %s\n", cmd_buf);
    fflush(stdout);
    system(cmd_buf);
    remove(respfile);
  } else {
    if (fd >= 0) {
      close(fd);
      remove(respfile);
    }
    printf("!!! Could not create gdb command file\n");
  }
  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    0xe37(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x50(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0xdc(%rbp)
cmp    $0x0,%eax
jl     1284 <gdb_info+0xd4>
mov    -0xdc(%rbp),%edi
lea    0xe21(%rip),%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0xd8(%rbp)
cmp    $0x0,%rax
je     1284 <gdb_info+0xd4>
mov    -0xd8(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xdfc(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0xd8(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x50(%rbp),%rcx
mov    $0x80,%esi
lea    0xf0e(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0xd0(%rbp),%rsi
lea    0xf1c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
lea    -0xd0(%rbp),%rdi
call   1060 <system@plt>
lea    -0x50(%rbp),%rdi
call   1030 <remove@plt>
jmp    12b3 <gdb_info+0x103>
cmpl   $0x0,-0xdc(%rbp)
jl     12a5 <gdb_info+0xf5>
mov    -0xdc(%rbp),%edi
call   1090 <close@plt>
lea    -0x50(%rbp),%rdi
call   1030 <remove@plt>
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10b0 <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    0xe2b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   10d0 <mkstemp@plt>
test   %eax,%eax
js     1281 <gdb_info+0xc1>
mov    %eax,%ebp
lea    0xe1b(%rip),%rsi
mov    %eax,%edi
call   10e0 <fdopen@plt>
test   %rax,%rax
je     1272 <gdb_info+0xb2>
mov    %rax,%rbx
lea    0xe07(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   10b0 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xf27(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %rbp,%rcx
call   1090 <snprintf@plt>
lea    0xf29(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    %rbx,%rdi
call   1070 <system@plt>
mov    %rbp,%rdi
call   1030 <remove@plt>
jmp    128d <gdb_info+0xcd>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    %rsp,%rdi
call   1030 <remove@plt>
lea    0xef8(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
add    $0xc0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"gdb-respfile-XXXXXX\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [314 x i8] c"attach %d\0Ashell 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.3 = [33 x i8] c"gdb --quiet --batch --command=%s\00"
@.str.4 = [15 x i8] c"Executing: %s\0A\00"
@stdout = global i32 0
@.str.5 = [39 x i8] c"!!! Could not create gdb command file\0A\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 ([20 x i8], [20 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 %33
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 %33
16: ; preds = %12
%17 = load i32*, i32** %5
%18 = load i32, i32* %2
%19 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([314 x i8], [314 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %5
%21 = call i32 @fclose(i32* %20)
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%24 = call i32 @snprintf(i8* %22, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @stdout
%28 = call i32 @fflush(i32 %27)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%30 = call i32 @system(i8* %29)
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%32 = call i32 @remove(i8* %31)
br label %43
33: ; preds = %12, %1
%34 = load i32, i32* %6
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%40 = call i32 @remove(i8* %39)
br label %41
41: ; preds = %36, %33
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %16
%44 = load i32, i32* @stdout
%45 = call i32 @fflush(i32 %44)
ret void
}

LLVM IR (-O2):
@.str = [20 x i8] c"gdb-respfile-XXXXXX\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [314 x i8] c"attach %d\0Ashell 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.3 = [33 x i8] c"gdb --quiet --batch --command=%s\00"
@.str.4 = [15 x i8] c"Executing: %s\0A\00"
@stdout = global i32 0
@str = [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 ([20 x i8], [20 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 %24
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 %21, label %12
12: ; preds = %9
%13 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([314 x i8], [314 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%14 = call i32 @fclose(i32* %10)
%15 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%17 = load i32, i32* @stdout
%18 = call i32 @fflush(i32 %17)
%19 = call i32 @system(i8* %5)
%20 = call i32 @remove(i8* %4)
br label %26
21: ; preds = %9
%22 = call i32 @close(i32 %7)
%23 = call i32 @remove(i8* %4)
br label %24
24: ; preds = %1, %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %26
26: ; preds = %24, %12
%27 = load i32, i32* @stdout
%28 = call i32 @fflush(i32 %27)
ret void
}