#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*/ M_MMAP_MAX;
int /*<<< orphan*/ __softboundmpx_allocate_shadow_stack_space(int);
int /*<<< orphan*/ __softboundmpx_deallocate_shadow_stack_space();
int /*<<< orphan*/ __softboundmpx_metadata_store(char **, char *, char *,
                                                 char *);
int /*<<< orphan*/ __softboundmpx_store_base_shadow_stack(char **, int);
int /*<<< orphan*/ __softboundmpx_store_bound_shadow_stack(char *, int);
int /*<<< orphan*/ mallopt(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ softboundmpx_init_ctype();
int softboundmpx_pseudo_main(int, char **);
int strlen(char *);
int main(int argc, char **argv) {
  char **new_argv = argv;
  int i;
  char *temp_ptr;
  int return_value;
  mallopt(M_MMAP_MAX, 0);
  for (i = 0; i < argc; i++) {
    __softboundmpx_metadata_store(&new_argv[i], new_argv[i],
                                  new_argv[i] + strlen(new_argv[i]) + 1,
                                  new_argv[i]);
  }
  softboundmpx_init_ctype();
  temp_ptr = ((char *)&new_argv[argc]) + 8;
  __softboundmpx_allocate_shadow_stack_space(2);
  __softboundmpx_store_base_shadow_stack(&new_argv[0], 1);
  __softboundmpx_store_bound_shadow_stack(temp_ptr, 1);
  return_value = softboundmpx_pseudo_main(argc, new_argv);
  __softboundmpx_deallocate_shadow_stack_space();
  return return_value;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <mallopt@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1249 <main+0xb9>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
cltq
add    %rax,%rdx
add    $0x1,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
call   1070 <__softboundmpx_metadata_store@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c5 <main+0x35>
mov    $0x0,%al
call   1060 <softboundmpx_init_ctype@plt>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    $0x2,%edi
call   1090 <__softboundmpx_allocate_shadow_stack_space@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <__softboundmpx_store_base_shadow_stack@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   10b0 <__softboundmpx_store_bound_shadow_stack@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <softboundmpx_pseudo_main@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1080 <__softboundmpx_deallocate_shadow_stack_space@plt>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <mallopt@plt>
test   %r15d,%r15d
jle    11ed <main+0x5d>
mov    %r15d,%r12d
mov    %r14,%rbx
nopl   0x0(%rax)
mov    (%rbx),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
lea    (%rax,%rbp,1),%rdx
add    $0x1,%rdx
mov    (%rbx),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <__softboundmpx_metadata_store@plt>
add    $0x8,%rbx
add    $0xffffffffffffffff,%r12
jne    11c0 <main+0x30>
xor    %eax,%eax
call   1060 <softboundmpx_init_ctype@plt>
movslq %r15d,%rax
lea    (%r14,%rax,8),%rbx
add    $0x8,%rbx
mov    $0x2,%edi
call   1090 <__softboundmpx_allocate_shadow_stack_space@plt>
mov    %r14,%rdi
mov    $0x1,%esi
call   10a0 <__softboundmpx_store_base_shadow_stack@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   10b0 <__softboundmpx_store_bound_shadow_stack@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <softboundmpx_pseudo_main@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <__softboundmpx_deallocate_shadow_stack_space@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@M_MMAP_MAX = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
store i8** %10, i8*** %6
%11 = load i32, i32* @M_MMAP_MAX
%12 = call i32 @mallopt(i32 %11, i32 0)
store i32 0, i32* %7
br label %13
13: ; preds = %47, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %50
17: ; preds = %13
%18 = load i8**, i8*** %6
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8**, i8*** %6
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = load i8**, i8*** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = load i8**, i8*** %6
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %31, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8**, i8*** %6
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = call i32 @__softboundmpx_metadata_store(i8** %21, i8* %26, i8* %40, i8* %45)
br label %47
47: ; preds = %17
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %13
50: ; preds = %13
%51 = call i32 (...) @softboundmpx_init_ctype()
%52 = load i8**, i8*** %6
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = bitcast i8** %55 to i8*
%57 = getelementptr inbounds i8, i8* %56, i64 8
store i8* %57, i8** %8
%58 = call i32 @__softboundmpx_allocate_shadow_stack_space(i32 2)
%59 = load i8**, i8*** %6
%60 = getelementptr inbounds i8*, i8** %59, i64 0
%61 = call i32 @__softboundmpx_store_base_shadow_stack(i8** %60, i32 1)
%62 = load i8*, i8** %8
%63 = call i32 @__softboundmpx_store_bound_shadow_stack(i8* %62, i32 1)
%64 = load i32, i32* %4
%65 = load i8**, i8*** %6
%66 = call i32 @softboundmpx_pseudo_main(i32 %64, i8** %65)
store i32 %66, i32* %9
%67 = call i32 (...) @__softboundmpx_deallocate_shadow_stack_space()
%68 = load i32, i32* %9
ret i32 %68
}

LLVM IR (-O2):
@M_MMAP_MAX = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @M_MMAP_MAX
%4 = tail call i32 @mallopt(i32 %3, i32 0)
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %20
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %18, %8 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = getelementptr inbounds i8, i8* %14, i64 %13
%16 = load i8*, i8** %10
%17 = tail call i32 @__softboundmpx_metadata_store(i8** %10, i8* %11, i8* %15, i8* %16)
%18 = add nuw nsw i64 %9, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %20, label %8
20: ; preds = %8, %2
%21 = tail call i32 (...) @softboundmpx_init_ctype()
%22 = sext i32 %0 to i64
%23 = getelementptr inbounds i8*, i8** %1, i64 %22
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = bitcast i8** %24 to i8*
%26 = tail call i32 @__softboundmpx_allocate_shadow_stack_space(i32 2)
%27 = tail call i32 @__softboundmpx_store_base_shadow_stack(i8** %1, i32 1)
%28 = tail call i32 @__softboundmpx_store_bound_shadow_stack(i8* %25, i32 1)
%29 = tail call i32 @softboundmpx_pseudo_main(i32 %0, i8** %1)
%30 = tail call i32 (...) @__softboundmpx_deallocate_shadow_stack_space()
ret i32 %29
}