#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();
char *__softboundmpx_load_base_shadow_stack(int);
char *__softboundmpx_load_bound_shadow_stack(int);
int /*<<< orphan*/
__softboundmpx_propagate_metadata_shadow_stack_from(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
char *strncpy(char *, char *, size_t);
__attribute__((__weak__, __always___)) char *
softboundmpx_strncpy(char *dest, char *src, size_t n) {
  char *dest_base = __softboundmpx_load_base_shadow_stack(1);
  char *dest_bound = __softboundmpx_load_bound_shadow_stack(1);
  char *src_base = __softboundmpx_load_base_shadow_stack(2);
  char *src_bound = __softboundmpx_load_bound_shadow_stack(2);
  if (dest < dest_base || (dest > dest_bound - n) || (n > (size_t)dest_bound)) {
    printf("[strncpy] overflow in strncpy with dest\n");
    __softboundmpx_abort();
  }
  if (src < src_base || (src > src_bound - n) || (n > (size_t)src_bound)) {
    __softboundmpx_abort();
  }
  char *ret_ptr = strncpy(dest, src, n);
  __softboundmpx_propagate_metadata_shadow_stack_from(1, 0);
  return ret_ptr;
}


x86 assembly (clang, -O0):
<softboundmpx_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1060 <__softboundmpx_load_bound_shadow_stack@plt>
mov    %rax,-0x28(%rbp)
mov    $0x2,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,-0x30(%rbp)
mov    $0x2,%edi
call   1060 <__softboundmpx_load_bound_shadow_stack@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11e5 <softboundmpx_strncpy+0x85>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
xor    %edx,%edx
sub    -0x18(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     11e5 <softboundmpx_strncpy+0x85>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jbe    11f8 <softboundmpx_strncpy+0x98>
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <__softboundmpx_abort@plt>
mov    -0x10(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     1231 <softboundmpx_strncpy+0xd1>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
xor    %edx,%edx
sub    -0x18(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     1231 <softboundmpx_strncpy+0xd1>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmp    %rcx,%rax
jbe    1238 <softboundmpx_strncpy+0xd8>
mov    $0x0,%al
call   1080 <__softboundmpx_abort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    %rax,-0x40(%rbp)
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <__softboundmpx_propagate_metadata_shadow_stack_from@plt>
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<softboundmpx_strncpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,%r13
mov    $0x1,%edi
call   1060 <__softboundmpx_load_bound_shadow_stack@plt>
mov    %rax,%rbp
mov    $0x2,%edi
call   1070 <__softboundmpx_load_base_shadow_stack@plt>
mov    %rax,(%rsp)
mov    $0x2,%edi
call   1060 <__softboundmpx_load_bound_shadow_stack@plt>
mov    %rax,%r12
cmp    %r14,%r13
ja     11be <softboundmpx_strncpy+0x5e>
mov    %rbp,%rax
sub    %rbx,%rax
cmp    %r14,%rax
jb     11be <softboundmpx_strncpy+0x5e>
cmp    %rbx,%rbp
jae    11d1 <softboundmpx_strncpy+0x71>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <__softboundmpx_abort@plt>
cmp    %r15,(%rsp)
ja     11ea <softboundmpx_strncpy+0x8a>
mov    %rbx,%rax
neg    %rax
add    %r12,%rax
cmp    %r15,%rax
jb     11ea <softboundmpx_strncpy+0x8a>
cmp    %rbx,%r12
jae    11f1 <softboundmpx_strncpy+0x91>
xor    %eax,%eax
call   1080 <__softboundmpx_abort@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1050 <__softboundmpx_propagate_metadata_shadow_stack_from@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"[strncpy] overflow in strncpy with dest\0A\00"
define weak i8* @softboundmpx_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%12 = call i8* @__softboundmpx_load_base_shadow_stack(i32 1)
store i8* %12, i8** %7
%13 = call i8* @__softboundmpx_load_bound_shadow_stack(i32 1)
store i8* %13, i8** %8
%14 = call i8* @__softboundmpx_load_base_shadow_stack(i32 2)
store i8* %14, i8** %9
%15 = call i8* @__softboundmpx_load_bound_shadow_stack(i32 2)
store i8* %15, i8** %10
%16 = load i8*, i8** %4
%17 = load i8*, i8** %7
%18 = icmp ult i8* %16, %17
br i1 %18, label %31, label %19
19: ; preds = %3
%20 = load i8*, i8** %4
%21 = load i8*, i8** %8
%22 = load i64, i64* %6
%23 = sub i64 0, %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = icmp ugt i8* %20, %24
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = load i64, i64* %6
%28 = load i8*, i8** %8
%29 = ptrtoint i8* %28 to i64
%30 = icmp ugt i64 %27, %29
br i1 %30, label %31, label %34
31: ; preds = %26, %19, %3
%32 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%33 = call i32 (...) @__softboundmpx_abort()
br label %34
34: ; preds = %31, %26
%35 = load i8*, i8** %5
%36 = load i8*, i8** %9
%37 = icmp ult i8* %35, %36
br i1 %37, label %50, label %38
38: ; preds = %34
%39 = load i8*, i8** %5
%40 = load i8*, i8** %10
%41 = load i64, i64* %6
%42 = sub i64 0, %41
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = icmp ugt i8* %39, %43
br i1 %44, label %50, label %45
45: ; preds = %38
%46 = load i64, i64* %6
%47 = load i8*, i8** %10
%48 = ptrtoint i8* %47 to i64
%49 = icmp ugt i64 %46, %48
br i1 %49, label %50, label %52
50: ; preds = %45, %38, %34
%51 = call i32 (...) @__softboundmpx_abort()
br label %52
52: ; preds = %50, %45
%53 = load i8*, i8** %4
%54 = load i8*, i8** %5
%55 = load i64, i64* %6
%56 = call i8* @strncpy(i8* %53, i8* %54, i64 %55)
store i8* %56, i8** %11
%57 = call i32 @__softboundmpx_propagate_metadata_shadow_stack_from(i32 1, i32 0)
%58 = load i8*, i8** %11
ret i8* %58
}

LLVM IR (-O2):
@str = [40 x i8] c"[strncpy] overflow in strncpy with dest\00"
define weak i8* @softboundmpx_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i8* @__softboundmpx_load_base_shadow_stack(i32 1)
%5 = tail call i8* @__softboundmpx_load_bound_shadow_stack(i32 1)
%6 = tail call i8* @__softboundmpx_load_base_shadow_stack(i32 2)
%7 = tail call i8* @__softboundmpx_load_bound_shadow_stack(i32 2)
%8 = icmp ugt i8* %4, %0
%9 = sub i64 0, %2
%10 = getelementptr inbounds i8, i8* %5, i64 %9
%11 = icmp ult i8* %10, %0
%12 = select i1 %8, i1 true, i1 %11
%13 = ptrtoint i8* %5 to i64
%14 = icmp ult i64 %13, %2
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 (...) @__softboundmpx_abort()
br label %19
19: ; preds = %3, %16
%20 = icmp ugt i8* %6, %1
%21 = getelementptr inbounds i8, i8* %7, i64 %9
%22 = icmp ult i8* %21, %1
%23 = select i1 %20, i1 true, i1 %22
%24 = ptrtoint i8* %7 to i64
%25 = icmp ult i64 %24, %2
%26 = select i1 %23, i1 true, i1 %25
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 (...) @__softboundmpx_abort()
br label %29
29: ; preds = %19, %27
%30 = tail call i8* @strncpy(i8* dereferenceable(1) %0, i8* %1, i64 %2)
%31 = tail call i32 @__softboundmpx_propagate_metadata_shadow_stack_from(i32 1, i32 0)
ret i8* %0
}