#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 */

/* Variables and functions */
int /*<<< orphan*/ __softboundmpx_abort();
scalar_t__ __softboundmpx_load_base_shadow_stack(int);
scalar_t__ __softboundmpx_load_bound_shadow_stack(int);
int /*<<< orphan*/ __softboundmpx_printf(char *);
int /*<<< orphan*/
__softboundmpx_propagate_metadata_shadow_stack_from(int, int /*<<< orphan*/);
char *getcwd(char *, size_t);
int /*<<< orphan*/ printf(char *);
__attribute__((__weak__, __always___)) char *softboundmpx_getcwd(char *buf,
                                                                 size_t size) {
  if (buf == NULL) {
    printf("This case not handled, requesting memory from system\n");
    __softboundmpx_abort();
  }
  char *base = (char *)__softboundmpx_load_base_shadow_stack(1);
  char *bound = (char *)__softboundmpx_load_bound_shadow_stack(1);
  if (buf < base || buf + size > bound) {
    __softboundmpx_printf("[getcwd], overflow in buf in getcwd\n");
    __softboundmpx_abort();
  }
  char *ret_ptr = getcwd(buf, size);
  __softboundmpx_propagate_metadata_shadow_stack_from(1, 0);
  return ret_ptr;
}


x86 assembly (clang, -O0):
<softboundmpx_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119e <softboundmpx_getcwd+0x2e>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <__softboundmpx_abort@plt>
mov    $0x1,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%edi
call   1050 <__softboundmpx_load_bound_shadow_stack@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11da <softboundmpx_getcwd+0x6a>
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11ed <softboundmpx_getcwd+0x7d>
lea    0xe55(%rip),%rdi
call   1080 <__softboundmpx_printf@plt>
mov    $0x0,%al
call   1090 <__softboundmpx_abort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <getcwd@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1030 <__softboundmpx_propagate_metadata_shadow_stack_from@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<softboundmpx_getcwd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
jne    1193 <softboundmpx_getcwd+0x23>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <__softboundmpx_abort@plt>
mov    $0x1,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   1050 <__softboundmpx_load_bound_shadow_stack@plt>
cmp    %r15,%rbx
ja     11b8 <softboundmpx_getcwd+0x48>
lea    (%r15,%r14,1),%rcx
cmp    %rax,%rcx
jbe    11cb <softboundmpx_getcwd+0x5b>
lea    0xe41(%rip),%rdi
call   1080 <__softboundmpx_printf@plt>
xor    %eax,%eax
call   1090 <__softboundmpx_abort@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <getcwd@plt>
mov    %rax,%rbx
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <__softboundmpx_propagate_metadata_shadow_stack_from@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [54 x i8] c"This case not handled, requesting memory from system\0A\00"
@.str.1 = [37 x i8] c"[getcwd], overflow in buf in getcwd\0A\00"
define weak i8* @softboundmpx_getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @__softboundmpx_abort()
br label %13
13: ; preds = %10, %2
%14 = call i64 @__softboundmpx_load_base_shadow_stack(i32 1)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
%16 = call i64 @__softboundmpx_load_bound_shadow_stack(i32 1)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %6
%18 = load i8*, i8** %3
%19 = load i8*, i8** %5
%20 = icmp ult i8* %18, %19
br i1 %20, label %27, label %21
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8*, i8** %6
%26 = icmp ugt i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %21, %13
%28 = call i32 @__softboundmpx_printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @__softboundmpx_abort()
br label %30
30: ; preds = %27, %21
%31 = load i8*, i8** %3
%32 = load i64, i64* %4
%33 = call i8* @getcwd(i8* %31, i64 %32)
store i8* %33, i8** %7
%34 = call i32 @__softboundmpx_propagate_metadata_shadow_stack_from(i32 1, i32 0)
%35 = load i8*, i8** %7
ret i8* %35
}

LLVM IR (-O2):
@.str.1 = [37 x i8] c"[getcwd], overflow in buf in getcwd\0A\00"
@str = [53 x i8] c"This case not handled, requesting memory from system\00"
define weak i8* @softboundmpx_getcwd(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (...) @__softboundmpx_abort()
br label %7
7: ; preds = %4, %2
%8 = tail call i64 @__softboundmpx_load_base_shadow_stack(i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i64 @__softboundmpx_load_bound_shadow_stack(i32 1)
%11 = icmp ugt i8* %9, %0
%12 = inttoptr i64 %10 to i8*
%13 = getelementptr inbounds i8, i8* %0, i64 %1
%14 = icmp ugt i8* %13, %12
%15 = select i1 %11, i1 true, i1 %14
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = tail call i32 @__softboundmpx_printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 (...) @__softboundmpx_abort()
br label %19
19: ; preds = %7, %16
%20 = tail call i8* @getcwd(i8* %0, i64 %1)
%21 = tail call i32 @__softboundmpx_propagate_metadata_shadow_stack_from(i32 1, i32 0)
ret i8* %20
}