#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 */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void __stack_smash_handler(char func[], int damaged) {
  const char message[] = ": stack smashing attack in function ";
  int bufsz = 512, len;
  char buf[bufsz];
  strcpy(buf, "<2>");
  len = 3;
  if (bufsz > len) {
    strncat(buf, message, bufsz - len - 1);
    len = strlen(buf);
  }
  if (bufsz > len) {
    strncat(buf, func, bufsz - len - 1);
    len = strlen(buf);
  }
  write(STDERR_FILENO, buf + 3, len - 3);
  _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)
movabs $0x206e6f6974636e,%rax
mov    %rax,-0x23(%rbp)
movaps 0xe9c(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movaps 0xe81(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movl   $0x200,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x58(%rbp)
lea    0xe6e(%rip),%rsi
call   1040 <strcpy@plt>
movl   $0x3,-0x48(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jle    11f1 <__stack_smash_handler+0xa1>
mov    -0x60(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
sub    -0x48(%rbp),%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jle    121f <__stack_smash_handler+0xcf>
mov    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x44(%rbp),%edx
sub    -0x48(%rbp),%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x60(%rbp),%rsi
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
add    $0x3,%rsi
mov    -0x48(%rbp),%edx
sub    $0x3,%edx
call   1050 <write@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<__stack_smash_handler>:
push   %r14
push   %rbx
sub    $0x238,%rsp
mov    %rdi,%r14
movaps 0xeac(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe90(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x206e6f6974636e,%rax
mov    %rax,0x1d(%rsp)
lea    0xe9b(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x1fc,%edx
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    $0x1ff,%eax
jg     11d5 <__stack_smash_handler+0x85>
mov    $0x1ff,%edx
sub    %eax,%edx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edi
lea    0x33(%rsp),%rsi
add    $0xfffffffd,%eax
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@__const.__stack_smash_handler.message = [37 x i8] c": stack smashing attack in function \00"
@.str = [4 x i8] c"<2>\00"
@STDERR_FILENO = 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 i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = bitcast [37 x i8]* %5 to i8*
store i32 512, i32* %6
%11 = load i32, i32* %6
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %7
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %2
%20 = getelementptr inbounds [37 x i8], [37 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = sub nsw i32 %21, %22
%24 = sub nsw i32 %23, 1
%25 = call i32 @strncat(i8* %14, i8* %20, i32 %24)
%26 = call i32 @strlen(i8* %14)
store i32 %26, i32* %7
br label %27
27: ; preds = %19, %2
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = sub nsw i32 %33, %34
%36 = sub nsw i32 %35, 1
%37 = call i32 @strncat(i8* %14, i8* %32, i32 %36)
%38 = call i32 @strlen(i8* %14)
store i32 %38, i32* %7
br label %39
39: ; preds = %31, %27
%40 = load i32, i32* @STDERR_FILENO
%41 = getelementptr inbounds i8, i8* %14, i64 3
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, 3
%44 = call i32 @write(i32 %40, i8* %41, i32 %43)
%45 = call i32 @_exit(i32 127)
unreachable
}

LLVM IR (-O2):
@__const.__stack_smash_handler.message = [37 x i8] c": stack smashing attack in function \00"
@.str = [4 x i8] c"<2>\00"
@STDERR_FILENO = global i32 0
define void @__stack_smash_handler(i8* %0, i32 %1) {
%3 = alloca [37 x i8]
%4 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 0
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strncat(i8* %6, i8* %4, i32 508)
%9 = call i32 @strlen(i8* %6)
%10 = icmp slt i32 %9, 512
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = sub i32 511, %9
%13 = call i32 @strncat(i8* %6, i8* %0, i32 %12)
%14 = call i32 @strlen(i8* %6)
br label %15
15: ; preds = %11, %2
%16 = phi i32 [ %14, %11 ], [ %9, %2 ]
%17 = load i32, i32* @STDERR_FILENO
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 3
%19 = add nsw i32 %16, -3
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
%21 = call i32 @_exit(i32 127)
unreachable
}