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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char const *, char *);
int pthread_attr_getstack(int /*<<< orphan*/ *, void **, size_t *);
int pthread_getattr_np(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_self();
char *strerror(int);
int get_self_pthread_attr(const char *id, void **stackaddr, size_t *stacksize) {
  pthread_attr_t attr;
  int ret;
  pthread_t me = pthread_self();
  if ((ret = pthread_getattr_np(me, &attr)) < 0) {
    printf("%s: pthread_getattr_np failed: %s\n", id, strerror(ret));
    return 1;
  }
  if ((ret = pthread_attr_getstack(&attr, stackaddr, stacksize)) < 0) {
    printf("%s: pthread_attr_getstack returned error: %s\n", id, strerror(ret));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_self_pthread_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <pthread_getattr_np@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11b5 <get_self_pthread_attr+0x65>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1208 <get_self_pthread_attr+0xb8>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rdi
call   1050 <pthread_attr_getstack@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1201 <get_self_pthread_attr+0xb1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1208 <get_self_pthread_attr+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_self_pthread_attr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <pthread_self@plt>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
call   1030 <pthread_getattr_np@plt>
test   %eax,%eax
js     1196 <get_self_pthread_attr+0x46>
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <pthread_attr_getstack@plt>
test   %eax,%eax
jns    11b7 <get_self_pthread_attr+0x67>
lea    0xe8f(%rip),%rbx
jmp    119d <get_self_pthread_attr+0x4d>
lea    0xe63(%rip),%rbx
mov    %eax,%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"%s: pthread_getattr_np failed: %s\0A\00"
@.str.1 = [46 x i8] c"%s: pthread_attr_getstack returned error: %s\0A\00"
define i32 @get_self_pthread_attr(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%11 = call i32 (...) @pthread_self()
store i32 %11, i32* %10
%12 = load i32, i32* %10
%13 = call i32 @pthread_getattr_np(i32 %12, i32* %8)
store i32 %13, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* %9
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 1, i32* %4
br label %31
20: ; preds = %3
%21 = load i8**, i8*** %6
%22 = load i64*, i64** %7
%23 = call i32 @pthread_attr_getstack(i32* %8, i8** %21, i64* %22)
store i32 %23, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = load i32, i32* %9
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %28)
store i32 1, i32* %4
br label %31
30: ; preds = %20
store i32 0, i32* %4
br label %31
31: ; preds = %30, %25, %15
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [35 x i8] c"%s: pthread_getattr_np failed: %s\0A\00"
@.str.1 = [46 x i8] c"%s: pthread_attr_getstack returned error: %s\0A\00"
define i32 @get_self_pthread_attr(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @pthread_self()
%7 = call i32 @pthread_getattr_np(i32 %6, i32* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = call i32 @pthread_attr_getstack(i32* %4, i8** %1, i64* %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9, %3
%13 = phi i32 [ %7, %3 ], [ %10, %9 ]
%14 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), %9 ]
%15 = call i8* @strerror(i32 %13)
%16 = call i32 @printf(i8* %14, i8* %0, i8* %15)
br label %17
17: ; preds = %12, %9
%18 = phi i32 [ 0, %9 ], [ 1, %12 ]
ret i32 %18
}