#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 */
struct sigaction {
  int /*<<< orphan*/ sa_handler;
};
struct ptrace_child {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ __NR_rt_sigaction;
int ptrace_memcpy_to_child(struct ptrace_child *, unsigned long,
                           struct sigaction *, int);
int ptrace_remote_syscall(struct ptrace_child *, int /*<<< orphan*/,
                          int /*<<< orphan*/, unsigned long, int /*<<< orphan*/,
                          int, int /*<<< orphan*/, int /*<<< orphan*/);
int ignore_hup(struct ptrace_child *child, unsigned long scratch_page) {
  int err;
  struct sigaction act = {
      .sa_handler = SIG_IGN,
  };
  err = ptrace_memcpy_to_child(child, scratch_page, &act, sizeof act);
  if (err < 0)
    return err;
  err = ptrace_remote_syscall(child, __NR_rt_sigaction, SIGHUP, scratch_page, 0,
                              8, 0, 0);
  return err;
}


x86 assembly (clang, -O0):
<ignore_hup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x4,%ecx
call   1030 <ptrace_memcpy_to_child@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116a <ignore_hup+0x4a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <ignore_hup+0x8a>
mov    -0x10(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rcx
xor    %r8d,%r8d
mov    $0x8,%r9d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1040 <ptrace_remote_syscall@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ignore_hup>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdx
mov    $0x4,%ecx
call   1030 <ptrace_memcpy_to_child@plt>
test   %eax,%eax
js     1175 <ignore_hup+0x55>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rcx
xor    %r8d,%r8d
mov    $0x8,%r9d
push   $0x0
push   $0x0
call   1040 <ptrace_remote_syscall@plt>
add    $0x10,%rsp
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ptrace_child = type { i32 }
%struct.sigaction = type { i32 }
@SIG_IGN = global i32 0
@__NR_rt_sigaction = global i32 0
@SIGHUP = global i32 0
define i32 @ignore_hup(%struct.ptrace_child* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.ptrace_child*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.sigaction
store %struct.ptrace_child* %0, %struct.ptrace_child** %4
store i64 %1, i64* %5
%8 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %7, i32 0, i32 0
%9 = load i32, i32* @SIG_IGN
store i32 %9, i32* %8
%10 = load %struct.ptrace_child*, %struct.ptrace_child** %4
%11 = load i64, i64* %5
%12 = call i32 @ptrace_memcpy_to_child(%struct.ptrace_child* %10, i64 %11, %struct.sigaction* %7, i32 4)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32, i32* %6
store i32 %16, i32* %3
br label %24
17: ; preds = %2
%18 = load %struct.ptrace_child*, %struct.ptrace_child** %4
%19 = load i32, i32* @__NR_rt_sigaction
%20 = load i32, i32* @SIGHUP
%21 = load i64, i64* %5
%22 = call i32 @ptrace_remote_syscall(%struct.ptrace_child* %18, i32 %19, i32 %20, i64 %21, i32 0, i32 8, i32 0, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %24
24: ; preds = %17, %15
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.ptrace_child = type { i32 }
%struct.sigaction = type { i32 }
@SIG_IGN = global i32 0
@__NR_rt_sigaction = global i32 0
@SIGHUP = global i32 0
define i32 @ignore_hup(%struct.ptrace_child* %0, i64 %1) {
%3 = alloca %struct.sigaction
%4 = bitcast %struct.sigaction* %3 to i8*
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %3, i64 0, i32 0
%6 = load i32, i32* @SIG_IGN
store i32 %6, i32* %5
%7 = call i32 @ptrace_memcpy_to_child(%struct.ptrace_child* %0, i64 %1, %struct.sigaction* %3, i32 4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @__NR_rt_sigaction
%11 = load i32, i32* @SIGHUP
%12 = call i32 @ptrace_remote_syscall(%struct.ptrace_child* %0, i32 %10, i32 %11, i64 %1, i32 0, i32 8, i32 0, i32 0)
br label %13
13: ; preds = %2, %9
%14 = phi i32 [ %12, %9 ], [ %7, %2 ]
ret i32 %14
}