#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int ss_size;
  int ss_flags;
  scalar_t__ ss_sp;
};
typedef TYPE_1__ stack_t;

/* Variables and functions */
int SIGSTKSZ;
int SS_DISABLE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, scalar_t__, unsigned char *, int, ...);
int sigaltstack(TYPE_1__ *, TYPE_1__ *);
void setup_signal_stack() {
  int ret_val;
  stack_t ss;
  stack_t oss;
  ss.ss_sp = malloc(SIGSTKSZ);
  assert(ss.ss_sp && "malloc failure");
  ss.ss_size = SIGSTKSZ;
  ss.ss_flags = 0;
  printf("ESP of alternate stack: %p, top: %p, size: %ld\n", ss.ss_sp,
         (unsigned char *)ss.ss_sp + ss.ss_size, ss.ss_size);
  ret_val = sigaltstack(&ss, &oss);
  if (ret_val) {
    perror("ERROR, sigaltstack failed");
    exit(1);
  }
  printf("ESP of original stack: %p, top: %p, size: %ld, disabled = %s\n",
         oss.ss_sp, (unsigned char *)oss.ss_sp + oss.ss_size, oss.ss_size,
         (oss.ss_flags & SS_DISABLE) ? "true" : "false");
}


x86 assembly (clang, -O0):
<setup_signal_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x29(%rbp)
je     1194 <setup_signal_stack+0x34>
mov    $0x1,%al
mov    %al,-0x29(%rbp)
jmp    1194 <setup_signal_stack+0x34>
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movslq -0x18(%rbp),%rax
add    %rax,%rdx
mov    -0x18(%rbp),%ecx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <sigaltstack@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1204 <setup_signal_stack+0xa4>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
movslq -0x28(%rbp),%rax
add    %rax,%rdx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%edi
mov    0x2dc8(%rip),%rax
and    (%rax),%edi
lea    0xe73(%rip),%r8
lea    0xe67(%rip),%rax
cmp    $0x0,%edi
cmovne %rax,%r8
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_signal_stack>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e62(%rip),%r14
mov    (%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x20(%rsp)
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
movslq (%r14),%rcx
mov    %ecx,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
mov    %rcx,%rdx
add    %rbx,%rdx
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x18(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1080 <sigaltstack@plt>
test   %eax,%eax
jne    120a <setup_signal_stack+0xaa>
mov    0x10(%rsp),%rsi
movslq 0x8(%rsp),%rcx
mov    %rcx,%rdx
add    %rsi,%rdx
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
and    0xc(%rsp),%eax
lea    0xea4(%rip),%rax
lea    0xe98(%rip),%r8
cmove  %rax,%r8
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i64 }
@SIGSTKSZ = global i32 0
@.str = [15 x i8] c"malloc failure\00"
@.str.1 = [48 x i8] c"ESP of alternate stack: %p, top: %p, size: %ld\0A\00"
@.str.2 = [26 x i8] c"ERROR, sigaltstack failed\00"
@.str.3 = [62 x i8] c"ESP of original stack: %p, top: %p, size: %ld, disabled = %s\0A\00"
@SS_DISABLE = global i32 0
@.str.4 = [5 x i8] c"true\00"
@.str.5 = [6 x i8] c"false\00"
define void @setup_signal_stack() {
%1 = alloca i32
%2 = alloca %struct.TYPE_4__
%3 = alloca %struct.TYPE_4__
%4 = load i32, i32* @SIGSTKSZ
%5 = call i64 @malloc(i32 %4)
%6 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 2
store i64 %5, i64* %6
%7 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 2
%8 = load i64, i64* %7
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
br label %11
11: ; preds = %10, %0
%12 = phi i1 [ false, %0 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @SIGSTKSZ
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 1
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 2
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 2
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i8*
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 (i8*, i64, i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i64 %19, i8* %26, i32 %28)
%30 = call i32 @sigaltstack(%struct.TYPE_4__* %2, %struct.TYPE_4__* %3)
store i32 %30, i32* %1
%31 = load i32, i32* %1
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %11
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %11
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 2
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 2
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
%42 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 0
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 0
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load i32, i32* @SS_DISABLE
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
%53 = zext i1 %52 to i64
%54 = select i1 %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0)
%55 = call i32 (i8*, i64, i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i64 %38, i8* %45, i32 %47, i8* %54)
ret void
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i64 }
@SIGSTKSZ = global i32 0
@.str.1 = [48 x i8] c"ESP of alternate stack: %p, top: %p, size: %ld\0A\00"
@.str.2 = [26 x i8] c"ERROR, sigaltstack failed\00"
@.str.3 = [62 x i8] c"ESP of original stack: %p, top: %p, size: %ld, disabled = %s\0A\00"
@SS_DISABLE = global i32 0
@.str.4 = [5 x i8] c"true\00"
@.str.5 = [6 x i8] c"false\00"
define void @setup_signal_stack() {
%1 = alloca %struct.TYPE_4__
%2 = alloca %struct.TYPE_4__
%3 = bitcast %struct.TYPE_4__* %1 to i8*
%4 = bitcast %struct.TYPE_4__* %2 to i8*
%5 = load i32, i32* @SIGSTKSZ
%6 = tail call i64 @malloc(i32 %5)
%7 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 2
store i64 %6, i64* %7
%8 = icmp ne i64 %6, 0
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @SIGSTKSZ
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 1
store i32 0, i32* %13
%14 = inttoptr i64 %6 to i8*
%15 = sext i32 %11 to i64
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = tail call i32 (i8*, i64, i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i64 %6, i8* %16, i32 %11)
%18 = call i32 @sigaltstack(%struct.TYPE_4__* %1, %struct.TYPE_4__* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 2
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i8*
%27 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
%32 = load i32, i32* %31
%33 = load i32, i32* @SS_DISABLE
%34 = and i32 %33, %32
%35 = icmp eq i32 %34, 0
%36 = select i1 %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0)
%37 = call i32 (i8*, i64, i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i64 %25, i8* %30, i32 %28, i8* %36)
ret void
}