#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_3__ TYPE_1__;

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

/* Variables and functions */
int /*<<< orphan*/ SIGSTKSZ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sigaltstack(TYPE_1__ *, int /*<<< orphan*/ *);
void handler() {
  stack_t altstack2;
  if ((altstack2.ss_sp = malloc(SIGSTKSZ)) == NULL) {
    perror("Unexpected error while attempting to setup test pre-conditions");
    exit(2);
  }
  altstack2.ss_flags = 0;
  altstack2.ss_size = SIGSTKSZ;
  if (sigaltstack(&altstack2, NULL) != -1) {
    printf("Test FAILED: Attempt to set change alternate stack while inside "
           "handler succeeded.\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    118a <handler+0x3a>
lea    0xe85(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
movq   $0x0,-0x10(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <sigaltstack@plt>
cmp    $0xffffffff,%eax
je     11ca <handler+0x7a>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handler>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     1193 <handler+0x43>
movq   $0x0,0x10(%rsp)
mov    %ebx,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1070 <sigaltstack@plt>
cmp    $0xffffffff,%eax
jne    11a9 <handler+0x59>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i64, i32* }
@SIGSTKSZ = global i32 0
@.str = [63 x i8] c"Unexpected error while attempting to setup test pre-conditions\00"
@.str.1 = [84 x i8] c"Test FAILED: Attempt to set change alternate stack while inside handler succeeded.\0A\00"
define void @handler() {
%1 = alloca %struct.TYPE_3__
%2 = load i32, i32* @SIGSTKSZ
%3 = call i32* @malloc(i32 %2)
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i32 0, i32 2
store i32* %3, i32** %4
%5 = icmp eq i32* %3, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 2)
unreachable
9: ; preds = %0
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* @SIGSTKSZ
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i32 0, i32 0
store i32 %11, i32* %12
%13 = call i32 @sigaltstack(%struct.TYPE_3__* %1, i32* null)
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = call i32 @printf(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %9
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i64, i32* }
@SIGSTKSZ = global i32 0
@.str = [63 x i8] c"Unexpected error while attempting to setup test pre-conditions\00"
@str = [83 x i8] c"Test FAILED: Attempt to set change alternate stack while inside handler succeeded.\00"
define void @handler() {
%1 = alloca %struct.TYPE_3__
%2 = bitcast %struct.TYPE_3__* %1 to i8*
%3 = load i32, i32* @SIGSTKSZ
%4 = tail call i32* @malloc(i32 %3)
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
store i32* %4, i32** %5
%6 = icmp eq i32* %4, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %0
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
store i32 %3, i32* %12
%13 = call i32 @sigaltstack(%struct.TYPE_3__* %1, i32* null)
%14 = icmp eq i32 %13, -1
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
ret void
}