#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 */
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int EINVAL;
size_t PTHREAD_STACK_MIN;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ posix_memalign(void **, size_t, size_t);
int /*<<< orphan*/ printf(char *);
int pthread_attr_destroy(int /*<<< orphan*/ *);
int pthread_attr_init(int /*<<< orphan*/ *);
int pthread_attr_setstacksize(int /*<<< orphan*/ *, size_t);
size_t sysconf(int /*<<< orphan*/);
int main(void) {
  pthread_attr_t attr;
  void *saddr;
  size_t stack_size;
  int rc;
  rc = pthread_attr_init(&attr);
  if (rc != 0) {
    perror("unexpected error: "
           "pthread_attr_setstacksize"
           " "
           "4-1"
           ": "
           "pthread_attr_init");
    exit(2);
  }
  stack_size = PTHREAD_STACK_MIN - sysconf(_SC_PAGE_SIZE);
  if (posix_memalign(&saddr, sysconf(_SC_PAGE_SIZE), stack_size) != 0) {
    perror("unexpected error: "
           "pthread_attr_setstacksize"
           " "
           "4-1"
           ": "
           "out of memory while "
           "allocating the stack memory");
    exit(2);
  }
  rc = pthread_attr_setstacksize(&attr, stack_size);
  if (rc != EINVAL) {
    perror("unexpected error: "
           "pthread_attr_setstacksize"
           " "
           "4-1"
           ": "
           "Got the wrong return value");
    exit(1);
  }
  rc = pthread_attr_destroy(&attr);
  if (rc != 0) {
    perror("unexpected error: "
           "pthread_attr_setstacksize"
           " "
           "4-1"
           ": "
           "pthread_attr_destroy");
    exit(2);
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <pthread_attr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11bb <main+0x3b>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%rsi
mov    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rdi
call   1090 <posix_memalign@plt>
cmp    $0x0,%rax
je     1223 <main+0xa3>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    -0x8(%rbp),%rdi
call   10a0 <pthread_attr_setstacksize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%eax
je     125b <main+0xdb>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1070 <pthread_attr_destroy@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1287 <main+0x107>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xea8(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xc(%rsp),%rdi
call   1040 <pthread_attr_init@plt>
test   %eax,%eax
jne    120a <main+0x8a>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rbx
mov    0x2e2a(%rip),%r14
mov    (%r14),%edi
call   1060 <sysconf@plt>
sub    %rax,%rbx
mov    (%r14),%edi
call   1060 <sysconf@plt>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1090 <posix_memalign@plt>
test   %rax,%rax
jne    1213 <main+0x93>
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
call   10a0 <pthread_attr_setstacksize@plt>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%eax
jne    121c <main+0x9c>
lea    0xc(%rsp),%rdi
call   1070 <pthread_attr_destroy@plt>
test   %eax,%eax
jne    1232 <main+0xb2>
lea    0xf3b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdef(%rip),%rdi
jmp    1239 <main+0xb9>
lea    0xe29(%rip),%rdi
jmp    1239 <main+0xb9>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xeb7(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [67 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: pthread_attr_init\00"
@PTHREAD_STACK_MIN = global i64 0
@_SC_PAGE_SIZE = global i32 0
@.str.1 = [97 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: out of memory while allocating the stack memory\00"
@EINVAL = global i32 0
@.str.2 = [76 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: Got the wrong return value\00"
@.str.3 = [70 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: pthread_attr_destroy\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @pthread_attr_init(i32* %2)
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 2)
unreachable
12: ; preds = %0
%13 = load i64, i64* @PTHREAD_STACK_MIN
%14 = load i32, i32* @_SC_PAGE_SIZE
%15 = call i64 @sysconf(i32 %14)
%16 = sub i64 %13, %15
store i64 %16, i64* %4
%17 = load i32, i32* @_SC_PAGE_SIZE
%18 = call i64 @sysconf(i32 %17)
%19 = load i64, i64* %4
%20 = call i64 @posix_memalign(i8** %3, i64 %18, i64 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %12
%26 = load i64, i64* %4
%27 = call i32 @pthread_attr_setstacksize(i32* %2, i64 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = load i32, i32* @EINVAL
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = call i32 @pthread_attr_destroy(i32* %2)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [67 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: pthread_attr_init\00"
@PTHREAD_STACK_MIN = global i64 0
@_SC_PAGE_SIZE = global i32 0
@.str.1 = [97 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: out of memory while allocating the stack memory\00"
@EINVAL = global i32 0
@.str.2 = [76 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: Got the wrong return value\00"
@.str.3 = [70 x i8] c"unexpected error: pthread_attr_setstacksize 4-1: pthread_attr_destroy\00"
@str = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = bitcast i32* %1 to i8*
%4 = bitcast i8** %2 to i8*
%5 = call i32 @pthread_attr_init(i32* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 2)
unreachable
10: ; preds = %0
%11 = load i64, i64* @PTHREAD_STACK_MIN
%12 = load i32, i32* @_SC_PAGE_SIZE
%13 = call i64 @sysconf(i32 %12)
%14 = sub i64 %11, %13
%15 = load i32, i32* @_SC_PAGE_SIZE
%16 = call i64 @sysconf(i32 %15)
%17 = call i64 @posix_memalign(i8** %2, i64 %16, i64 %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([97 x i8], [97 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %10
%23 = call i32 @pthread_attr_setstacksize(i32* %1, i64 %14)
%24 = load i32, i32* @EINVAL
%25 = icmp eq i32 %23, %24
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = call i32 @pthread_attr_destroy(i32* %1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %29
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
ret i32 0
}