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

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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
scalar_t__ MINSIGSTKSZ;
int SS_DISABLE;
int SS_ONSTACK;
int /*<<< orphan*/ errno;
int syscall(int, TYPE_1__ const *, TYPE_1__ *);
int sigaltstack(const stack_t *restrict ss, stack_t *restrict old) {
  if (ss) {
    if (!(ss->ss_flags & SS_DISABLE) && ss->ss_size < MINSIGSTKSZ) {
      errno = ENOMEM;
      return -1;
    }
    if (ss->ss_flags & SS_ONSTACK) {
      errno = EINVAL;
      return -1;
    }
  }
  return syscall(186, ss, old);
}


x86 assembly (clang, -O0):
<sigaltstack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b4 <sigaltstack+0xa4>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2ea8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1179 <sigaltstack+0x69>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
jge    1179 <sigaltstack+0x69>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <sigaltstack+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11af <sigaltstack+0x9f>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <sigaltstack+0xb9>
jmp    11b4 <sigaltstack+0xa4>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0xba,%edi
call   1030 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigaltstack>:
mov    %rsi,%rdx
mov    %rdi,%rsi
test   %rdi,%rdi
je     1164 <sigaltstack+0x54>
mov    (%rsi),%eax
mov    0x2ebc(%rip),%rcx
test   %eax,(%rcx)
jne    1141 <sigaltstack+0x31>
mov    0x8(%rsi),%rcx
mov    0x2eb5(%rip),%rdi
cmp    (%rdi),%rcx
jge    1141 <sigaltstack+0x31>
mov    0x2e89(%rip),%rax
jmp    1153 <sigaltstack+0x43>
mov    0x2e90(%rip),%rcx
test   %eax,(%rcx)
je     1164 <sigaltstack+0x54>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    0x2e64(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret
mov    $0xba,%edi
jmp    1030 <syscall@plt>

LLVM IR (-O0):
%struct.TYPE_6__ = type { i32, i64 }
@SS_DISABLE = global i32 0
@MINSIGSTKSZ = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@SS_ONSTACK = global i32 0
@EINVAL = global i32 0
define i32 @sigaltstack(%struct.TYPE_6__* noalias %0, %struct.TYPE_6__* noalias %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_6__*
%5 = alloca %struct.TYPE_6__*
store %struct.TYPE_6__* %0, %struct.TYPE_6__** %4
store %struct.TYPE_6__* %1, %struct.TYPE_6__** %5
%6 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%7 = icmp ne %struct.TYPE_6__* %6, null
br i1 %7, label %8, label %33
8: ; preds = %2
%9 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%10 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @SS_DISABLE
%13 = and i32 %11, %12
%14 = icmp ne i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %8
%16 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%17 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %16, i32 0, i32 1
%18 = load i64, i64* %17
%19 = load i64, i64* @MINSIGSTKSZ
%20 = icmp slt i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = load i32, i32* @ENOMEM
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %37
23: ; preds = %15, %8
%24 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%25 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @SS_ONSTACK
%28 = and i32 %26, %27
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i32, i32* @EINVAL
store i32 %31, i32* @errno
store i32 -1, i32* %3
br label %37
32: ; preds = %23
br label %33
33: ; preds = %32, %2
%34 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%35 = load %struct.TYPE_6__*, %struct.TYPE_6__** %5
%36 = call i32 @syscall(i32 186, %struct.TYPE_6__* %34, %struct.TYPE_6__* %35)
store i32 %36, i32* %3
br label %37
37: ; preds = %33, %30, %21
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.TYPE_6__ = type { i32, i64 }
@SS_DISABLE = global i32 0
@MINSIGSTKSZ = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@SS_ONSTACK = global i32 0
@EINVAL = global i32 0
define i32 @sigaltstack(%struct.TYPE_6__* noalias %0, %struct.TYPE_6__* noalias %1) {
%3 = icmp eq %struct.TYPE_6__* %0, null
br i1 %3, label %23, label %4
4: ; preds = %2
%5 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = load i32, i32* @SS_DISABLE
%8 = and i32 %7, %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %4
%11 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 1
%12 = load i64, i64* %11
%13 = load i64, i64* @MINSIGSTKSZ
%14 = icmp slt i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
br label %25
17: ; preds = %10, %4
%18 = load i32, i32* @SS_ONSTACK
%19 = and i32 %18, %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
br label %25
23: ; preds = %17, %2
%24 = tail call i32 @syscall(i32 186, %struct.TYPE_6__* %0, %struct.TYPE_6__* %1)
br label %25
25: ; preds = %23, %21, %15
%26 = phi i32 [ -1, %21 ], [ %24, %23 ], [ -1, %15 ]
ret i32 %26
}