#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*/ program;
typedef int pid_t;

/* Variables and functions */
int getpid();
size_t readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
unsigned int string_hash(char *);
unsigned int get_share_mem_key() {
  pid_t pid = getpid();
  char proc[256] = {0};
  char program[256] = {0};
  size_t program_len = 0;
  sprintf(proc, "/proc/%d/exe", pid);
  program_len = readlink(proc, program, sizeof(program));
  if ((size_t)-1 == program_len) {
    return (unsigned int)-1;
  }
  return string_hash(program);
}


x86 assembly (clang, -O0):
<get_share_mem_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movq   $0x0,-0x218(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe59(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %rax,-0x218(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x218(%rbp),%rax
jne    11eb <get_share_mem_key+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <get_share_mem_key+0xaa>
lea    -0x210(%rbp),%rdi
call   1050 <string_hash@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_share_mem_key>:
push   %rbx
sub    $0x200,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xdc0(%rip),%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x100,%edx
call   1030 <readlink@plt>
cmp    $0xffffffffffffffff,%rax
je     1272 <get_share_mem_key+0x132>
mov    %rsp,%rdi
call   1050 <string_hash@plt>
jmp    1277 <get_share_mem_key+0x137>
mov    $0xffffffff,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/exe\00"
define i32 @get_share_mem_key() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = call i32 (...) @getpid()
store i32 %6, i32* %2
%7 = bitcast [256 x i8]* %3 to i8*
%8 = bitcast [256 x i8]* %4 to i8*
store i64 0, i64* %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = call i64 @readlink(i8* %12, i8* %13, i32 256)
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = icmp eq i64 -1, %15
br i1 %16, label %17, label %18
17: ; preds = %0
store i32 -1, i32* %1
br label %21
18: ; preds = %0
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%20 = call i32 @string_hash(i8* %19)
store i32 %20, i32* %1
br label %21
21: ; preds = %18, %17
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/exe\00"
define i32 @get_share_mem_key() {
%1 = alloca [256 x i8]
%2 = alloca [256 x i8]
%3 = tail call i32 (...) @getpid()
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%7 = call i64 @readlink(i8* %4, i8* %5, i32 256)
%8 = icmp eq i64 %7, -1
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @string_hash(i8* %5)
br label %11
11: ; preds = %0, %9
%12 = phi i32 [ %10, %9 ], [ -1, %0 ]
ret i32 %12
}