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

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ SIGABRT;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char const *,
                           char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ sigdelset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, char const *,
                          char *);
void __stack_smash_handler(char func[], int damaged) {
  extern char *__progname;
  const char message[] = ": stack smashing attack in function ";
  struct sigaction sa;
  sigset_t mask;
  sigfillset(&mask);
  sigdelset(&mask, SIGABRT);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  fprintf(stderr, "%s%s%s()\n", __progname, message, func);
  syslog(LOG_INFO, "%s%s%s()", __progname, message, func);
  memset(&sa, 0, sizeof(struct sigaction));
  sigfillset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = SIG_DFL;
  sigaction(SIGABRT, &sa, NULL);
  (void)kill(getpid(), SIGABRT);
  _exit(127);
}


x86 assembly (clang, -O0):
<__stack_smash_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe36(%rip),%rsi
mov    $0x25,%edx
call   10a0 <memcpy@plt>
lea    -0x5c(%rbp),%rdi
call   1090 <sigfillset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    -0x5c(%rbp),%rdi
call   10d0 <sigdelset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    -0x5c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%rdx
lea    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xdfe(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%rdx
lea    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r8
lea    0xde1(%rip),%rsi
call   10c0 <syslog@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
lea    -0x58(%rbp),%rdi
add    $0x10,%rdi
call   1090 <sigfillset@plt>
movq   $0x0,-0x50(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sigaction@plt>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

x86 assembly (clang, -O2):
<__stack_smash_handler>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%r14
movaps 0xe5b(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe3f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movabs $0x206e6f6974636e,%rax
mov    %rax,0x3d(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigfillset@plt>
mov    0x2ddf(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   10c0 <sigdelset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2d99(%rip),%rbx
mov    (%rbx),%rdx
lea    0xe04(%rip),%rsi
lea    0x20(%rsp),%r12
mov    %r12,%rcx
mov    %r14,%r8
call   1080 <fprintf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdeb(%rip),%rsi
mov    %r12,%rcx
mov    %r14,%r8
call   10b0 <syslog@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1070 <memset@plt>
lea    0x18(%rsp),%rdi
call   1090 <sigfillset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1050 <sigaction@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    (%r15),%esi
mov    %eax,%edi
call   10a0 <kill@plt>
mov    $0x7f,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@__const.__stack_smash_handler.message = [37 x i8] c": stack smashing attack in function \00"
@SIGABRT = global i32 0
@SIG_BLOCK = global i32 0
@stderr = global i32 0
@.str = [10 x i8] c"%s%s%s()\0A\00"
@__progname = external global i8*
@LOG_INFO = global i32 0
@.str.1 = [9 x i8] c"%s%s%s()\00"
@SIG_DFL = global i32 0
define void @__stack_smash_handler(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [37 x i8]
%6 = alloca %struct.sigaction
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = bitcast [37 x i8]* %5 to i8*
%9 = call i32 @sigfillset(i32* %7)
%10 = load i32, i32* @SIGABRT
%11 = call i32 @sigdelset(i32* %7, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %7, i32* null)
%14 = load i32, i32* @stderr
%15 = load i8*, i8** @__progname
%16 = getelementptr inbounds [37 x i8], [37 x i8]* %5, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16, i8* %17)
%19 = load i32, i32* @LOG_INFO
%20 = load i8*, i8** @__progname
%21 = getelementptr inbounds [37 x i8], [37 x i8]* %5, i64 0, i64 0
%22 = load i8*, i8** %3
%23 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %21, i8* %22)
%24 = call i32 @memset(%struct.sigaction* %6, i32 0, i32 24)
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 2
%26 = call i32 @sigfillset(i32* %25)
%27 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 1
store i64 0, i64* %27
%28 = load i32, i32* @SIG_DFL
%29 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @SIGABRT
%31 = call i32 @sigaction(i32 %30, %struct.sigaction* %6, i32* null)
%32 = call i32 (...) @getpid()
%33 = load i32, i32* @SIGABRT
%34 = call i32 @kill(i32 %32, i32 %33)
%35 = call i32 @_exit(i32 127)
unreachable
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@__const.__stack_smash_handler.message = [37 x i8] c": stack smashing attack in function \00"
@SIGABRT = global i32 0
@SIG_BLOCK = global i32 0
@stderr = global i32 0
@.str = [10 x i8] c"%s%s%s()\0A\00"
@__progname = external global i8*
@LOG_INFO = global i32 0
@.str.1 = [9 x i8] c"%s%s%s()\00"
@SIG_DFL = global i32 0
define void @__stack_smash_handler(i8* %0, i32 %1) {
%3 = alloca [37 x i8]
%4 = alloca %struct.sigaction
%5 = alloca i32
%6 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sigaction* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @sigfillset(i32* %5)
%10 = load i32, i32* @SIGABRT
%11 = call i32 @sigdelset(i32* %5, i32 %10)
%12 = load i32, i32* @SIG_BLOCK
%13 = call i32 @sigprocmask(i32 %12, i32* %5, i32* null)
%14 = load i32, i32* @stderr
%15 = load i8*, i8** @__progname
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %6, i8* %0)
%17 = load i32, i32* @LOG_INFO
%18 = load i8*, i8** @__progname
%19 = call i32 @syslog(i32 %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %6, i8* %0)
%20 = call i32 @memset(%struct.sigaction* %4, i32 0, i32 24)
%21 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
%22 = call i32 @sigfillset(i32* %21)
%23 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* @SIG_DFL
%25 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @SIGABRT
%27 = call i32 @sigaction(i32 %26, %struct.sigaction* %4, i32* null)
%28 = call i32 (...) @getpid()
%29 = load i32, i32* @SIGABRT
%30 = call i32 @kill(i32 %28, i32 %29)
%31 = call i32 @_exit(i32 127)
unreachable
}