#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 {
  scalar_t__ sa_handler;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGABRT;
scalar_t__ SIG_DFL;
scalar_t__ SIG_IGN;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             struct sigaction *);
int /*<<< orphan*/ sigdelset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
void apue_abort() {
  sigset_t mask;
  struct sigaction action;
  sigaction(SIGABRT, NULL, &action);
  if (action.sa_handler == SIG_IGN) {
    printf("change ignore to default\n");
    action.sa_handler = SIG_DFL;
    sigaction(SIGABRT, &action, NULL);
  }
  if (action.sa_handler == SIG_DFL)
    fflush(NULL);
  sigfillset(&mask);
  sigdelset(&mask, SIGABRT);
  sigprocmask(SIG_SETMASK, &mask, NULL);
  kill(getpid(), SIGABRT);
  printf("after execute user handler\n");
  fflush(NULL);
  action.sa_handler = SIG_DFL;
  sigaction(SIGABRT, &action, NULL);
  sigprocmask(SIG_SETMASK, &mask, NULL);
  kill(getpid(), SIGABRT);
  printf("never get here!");
  exit(1);
}


x86 assembly (clang, -O0):
<apue_abort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x10(%rbp),%rdx
call   1040 <sigaction@plt>
mov    -0x10(%rbp),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11f2 <apue_abort+0x62>
lea    0xe37(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    -0x10(%rbp),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <apue_abort+0x7f>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
lea    -0x4(%rbp),%rdi
call   1070 <sigfillset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdi
call   10a0 <sigdelset@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    -0x4(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xdbc(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    -0x4(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xd72(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<apue_abort>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e16(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rdx
xor    %esi,%esi
call   1050 <sigaction@plt>
mov    0x8(%rsp),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <apue_abort+0x61>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    0x8(%rsp),%rax
mov    0x2dc8(%rip),%r15
cmp    (%r15),%rax
jne    1214 <apue_abort+0x74>
xor    %edi,%edi
call   10a0 <fflush@plt>
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigfillset@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   10b0 <sigdelset@plt>
mov    0x2db5(%rip),%r12
mov    (%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    (%r14),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xdb7(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    (%r15),%rax
mov    %rax,0x8(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    (%r14),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xd5e(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i64 }
@SIGABRT = global i32 0
@SIG_IGN = global i64 0
@.str = [26 x i8] c"change ignore to default\0A\00"
@SIG_DFL = global i64 0
@SIG_SETMASK = global i32 0
@.str.1 = [28 x i8] c"after execute user handler\0A\00"
@.str.2 = [16 x i8] c"never get here!\00"
define void @apue_abort() {
%1 = alloca i32
%2 = alloca %struct.sigaction
%3 = load i32, i32* @SIGABRT
%4 = call i32 @sigaction(i32 %3, %struct.sigaction* null, %struct.sigaction* %2)
%5 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @SIG_IGN
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%11 = load i64, i64* @SIG_DFL
%12 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
store i64 %11, i64* %12
%13 = load i32, i32* @SIGABRT
%14 = call i32 @sigaction(i32 %13, %struct.sigaction* %2, %struct.sigaction* null)
br label %15
15: ; preds = %9, %0
%16 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @SIG_DFL
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @fflush(i32* null)
br label %22
22: ; preds = %20, %15
%23 = call i32 @sigfillset(i32* %1)
%24 = load i32, i32* @SIGABRT
%25 = call i32 @sigdelset(i32* %1, i32 %24)
%26 = load i32, i32* @SIG_SETMASK
%27 = call i32 @sigprocmask(i32 %26, i32* %1, i32* null)
%28 = call i32 (...) @getpid()
%29 = load i32, i32* @SIGABRT
%30 = call i32 @kill(i32 %28, i32 %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @fflush(i32* null)
%33 = load i64, i64* @SIG_DFL
%34 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
store i64 %33, i64* %34
%35 = load i32, i32* @SIGABRT
%36 = call i32 @sigaction(i32 %35, %struct.sigaction* %2, %struct.sigaction* null)
%37 = load i32, i32* @SIG_SETMASK
%38 = call i32 @sigprocmask(i32 %37, i32* %1, i32* null)
%39 = call i32 (...) @getpid()
%40 = load i32, i32* @SIGABRT
%41 = call i32 @kill(i32 %39, i32 %40)
%42 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.sigaction = type { i64 }
@SIGABRT = global i32 0
@SIG_IGN = global i64 0
@SIG_DFL = global i64 0
@SIG_SETMASK = global i32 0
@.str.2 = [16 x i8] c"never get here!\00"
@str = [27 x i8] c"after execute user handler\00"
@str.3 = [25 x i8] c"change ignore to default\00"
define void @apue_abort() {
%1 = alloca i32
%2 = alloca %struct.sigaction
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.sigaction* %2 to i8*
%5 = load i32, i32* @SIGABRT
%6 = call i32 @sigaction(i32 %5, %struct.sigaction* null, %struct.sigaction* %2)
%7 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @SIG_IGN
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.3, i64 0, i64 0))
%13 = load i64, i64* @SIG_DFL
store i64 %13, i64* %7
%14 = load i32, i32* @SIGABRT
%15 = call i32 @sigaction(i32 %14, %struct.sigaction* %2, %struct.sigaction* null)
%16 = load i64, i64* %7
br label %17
17: ; preds = %11, %0
%18 = phi i64 [ %16, %11 ], [ %8, %0 ]
%19 = load i64, i64* @SIG_DFL
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @fflush(i32* null)
br label %23
23: ; preds = %21, %17
%24 = call i32 @sigfillset(i32* %1)
%25 = load i32, i32* @SIGABRT
%26 = call i32 @sigdelset(i32* %1, i32 %25)
%27 = load i32, i32* @SIG_SETMASK
%28 = call i32 @sigprocmask(i32 %27, i32* %1, i32* null)
%29 = call i32 (...) @getpid()
%30 = load i32, i32* @SIGABRT
%31 = call i32 @kill(i32 %29, i32 %30)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%33 = call i32 @fflush(i32* null)
%34 = load i64, i64* @SIG_DFL
store i64 %34, i64* %7
%35 = load i32, i32* @SIGABRT
%36 = call i32 @sigaction(i32 %35, %struct.sigaction* %2, %struct.sigaction* null)
%37 = load i32, i32* @SIG_SETMASK
%38 = call i32 @sigprocmask(i32 %37, i32* %1, i32* null)
%39 = call i32 (...) @getpid()
%40 = load i32, i32* @SIGABRT
%41 = call i32 @kill(i32 %39, i32 %40)
%42 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
}