#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;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gdb_sleep(int *, int /*<<< orphan*/);
char *getenv(char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, int *);
scalar_t__ strlen(char *);
void gdb_spin(int target) {
  char *s = getenv("GDB_SPIN");
  int gdb_spin_number;
  if (s != NULL && strlen(s) > 0) {
    int c = sscanf(s, "%i", &gdb_spin_number);
    if (c != 1) {
      printf("Invalid GDB_SPIN: %s\n", s);
      exit(1);
    }
    if (gdb_spin_number == target) {
      pid_t pid = getpid();
      printf("Waiting for gdb: target: %i pid: %i\n", target, pid);
      int t = 0;
      int i = 0;
      while (!t)
        gdb_sleep(&t, i++);
    }
  }
}


x86 assembly (clang, -O0):
<gdb_spin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1257 <gdb_spin+0xe7>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%rax
jle    1257 <gdb_spin+0xe7>
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     11e6 <gdb_spin+0x76>
mov    -0x10(%rbp),%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1252 <gdb_spin+0xe2>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1234 <gdb_spin+0xc4>
jmp    124d <gdb_spin+0xdd>
mov    -0x24(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <gdb_sleep@plt>
jmp    121e <gdb_spin+0xae>
jmp    1252 <gdb_spin+0xe2>
jmp    1257 <gdb_spin+0xe7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_spin>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1206 <gdb_spin+0x96>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    1206 <gdb_spin+0x96>
lea    0xe67(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    120f <gdb_spin+0x9f>
cmp    %ebp,0xc(%rsp)
jne    1206 <gdb_spin+0x96>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe58(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x1(%rbx),%ebp
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <gdb_sleep@plt>
cmpl   $0x0,0x8(%rsp)
mov    %ebp,%ebx
je     11f0 <gdb_spin+0x80>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"GDB_SPIN\00"
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [22 x i8] c"Invalid GDB_SPIN: %s\0A\00"
@.str.3 = [37 x i8] c"Waiting for gdb: target: %i pid: %i\0A\00"
define void @gdb_spin(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %44
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* %13)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %4
%27 = load i32, i32* %2
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = call i32 (...) @getpid()
store i32 %30, i32* %6
%31 = load i32, i32* %2
%32 = load i32, i32* %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %32)
store i32 0, i32* %7
store i32 0, i32* %8
br label %34
34: ; preds = %38, %29
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
%37 = xor i1 %36, true
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = call i32 @gdb_sleep(i32* %7, i32 %39)
br label %34
42: ; preds = %34
br label %43
43: ; preds = %42, %25
br label %44
44: ; preds = %43, %12, %1
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"GDB_SPIN\00"
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [22 x i8] c"Invalid GDB_SPIN: %s\0A\00"
@.str.3 = [37 x i8] c"Waiting for gdb: target: %i pid: %i\0A\00"
define void @gdb_spin(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = bitcast i32* %2 to i8*
%6 = icmp eq i8* %4, null
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = tail call i64 @strlen(i8* %4)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %7
%11 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, %0
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = call i32 (...) @getpid()
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %20)
%22 = bitcast i32* %3 to i8*
store i32 0, i32* %3
br label %23
23: ; preds = %19, %23
%24 = phi i32 [ 0, %19 ], [ %25, %23 ]
%25 = add nuw nsw i32 %24, 1
%26 = call i32 @gdb_sleep(i32* %3, i32 %24)
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
br i1 %28, label %23, label %29
29: ; preds = %23
br label %30
30: ; preds = %16, %29, %7, %1
ret void
}