#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 */

/* Variables and functions */
scalar_t__ EINTR;
int SIGUSR1;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
unsigned int gettid();
int /*<<< orphan*/ notify_gdb_of_libraries();
int read(int, unsigned int *, int);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int socket_abstract_client(char *, int /*<<< orphan*/);
int write(int, unsigned int *, int);
void debugger_signal_handler(int n) {
  unsigned tid;
  int s;
  signal(SIGUSR1, SIG_IGN);
  tid = gettid();
  s = socket_abstract_client("android:debuggerd", SOCK_STREAM);
  if (s >= 0) {
    int ret;
    do {
      ret = (write(s, &tid, sizeof(unsigned)));
    } while (ret < 0 && errno == EINTR);
    if (ret == sizeof(unsigned)) {
      do {
        ret = (read(s, &tid, 1));
      } while (ret < 0 && errno == EINTR);
      notify_gdb_of_libraries();
    }
    close(s);
  }
  signal(n, SIG_IGN);
}


x86 assembly (clang, -O0):
<debugger_signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
call   1090 <socket_abstract_client@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1275 <debugger_signal_handler+0x105>
jmp    11c3 <debugger_signal_handler+0x53>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jge    1200 <debugger_signal_handler+0x90>
mov    0x2ddb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11c3 <debugger_signal_handler+0x53>
movslq -0x10(%rbp),%rax
cmp    $0x4,%rax
jne    126d <debugger_signal_handler+0xfd>
jmp    121e <debugger_signal_handler+0xae>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x12(%rbp)
jge    125b <debugger_signal_handler+0xeb>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    121e <debugger_signal_handler+0xae>
mov    $0x0,%al
call   1040 <notify_gdb_of_libraries@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debugger_signal_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e52(%rip),%r15
mov    (%r15),%esi
call   1080 <signal@plt>
xor    %eax,%eax
call   1030 <gettid@plt>
mov    %eax,0x4(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1090 <socket_abstract_client@plt>
test   %eax,%eax
js     122a <debugger_signal_handler+0xba>
mov    %eax,%ebp
lea    0x4(%rsp),%r13
mov    0x2e00(%rip),%r12
mov    0x2df1(%rip),%rbx
nop
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1050 <write@plt>
test   %eax,%eax
jns    11ec <debugger_signal_handler+0x7c>
mov    (%rbx),%rcx
cmp    %rcx,(%r12)
je     11d0 <debugger_signal_handler+0x60>
cmp    $0x4,%eax
jne    1223 <debugger_signal_handler+0xb3>
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    121c <debugger_signal_handler+0xac>
mov    (%rbx),%rax
cmp    %rax,(%r12)
je     1200 <debugger_signal_handler+0x90>
xor    %eax,%eax
call   1040 <notify_gdb_of_libraries@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%r15),%esi
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <signal@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@.str = [18 x i8] c"android:debuggerd\00"
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @debugger_signal_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @SIGUSR1
%7 = load i32, i32* @SIG_IGN
%8 = call i32 @signal(i32 %6, i32 %7)
%9 = call i32 (...) @gettid()
store i32 %9, i32* %3
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket_abstract_client(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %49
14: ; preds = %1
br label %15
15: ; preds = %25, %14
%16 = load i32, i32* %4
%17 = call i32 @write(i32 %16, i32* %3, i32 4)
store i32 %17, i32* %5
br label %18
18: ; preds = %15
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %15, label %27
27: ; preds = %25
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = icmp eq i64 %29, 4
br i1 %30, label %31, label %46
31: ; preds = %27
br label %32
32: ; preds = %42, %31
%33 = load i32, i32* %4
%34 = call i32 @read(i32 %33, i32* %3, i32 1)
store i32 %34, i32* %5
br label %35
35: ; preds = %32
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %32, label %44
44: ; preds = %42
%45 = call i32 (...) @notify_gdb_of_libraries()
br label %46
46: ; preds = %44, %27
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %1
%50 = load i32, i32* %2
%51 = load i32, i32* @SIG_IGN
%52 = call i32 @signal(i32 %50, i32 %51)
ret void
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@.str = [18 x i8] c"android:debuggerd\00"
@SOCK_STREAM = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @debugger_signal_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @SIGUSR1
%5 = load i32, i32* @SIG_IGN
%6 = tail call i32 @signal(i32 %4, i32 %5)
%7 = tail call i32 (...) @gettid()
store i32 %7, i32* %2
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket_abstract_client(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %31
11: ; preds = %1, %11
%12 = call i32 @write(i32 %9, i32* %2, i32 4)
%13 = icmp slt i32 %12, 0
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
%19 = icmp eq i32 %12, 4
br i1 %19, label %20, label %29
20: ; preds = %18, %20
%21 = call i32 @read(i32 %9, i32* %2, i32 1)
%22 = icmp slt i32 %21, 0
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
%26 = select i1 %22, i1 %25, i1 false
br i1 %26, label %20, label %27
27: ; preds = %20
%28 = tail call i32 (...) @notify_gdb_of_libraries()
br label %29
29: ; preds = %27, %18
%30 = tail call i32 @close(i32 %9)
br label %31
31: ; preds = %29, %1
%32 = load i32, i32* @SIG_IGN
%33 = tail call i32 @signal(i32 %0, i32 %32)
ret void
}