#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 */
struct stat {
  long st_size;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int shm_open(char const *, int, int);
long get_memory_size(const char *designator) {
  int md;
  struct stat file_stat;
  md = shm_open(designator, O_CREAT | O_RDWR, 0600);
  if (md == -1) {
    printf("Got an error opening shm\n");
    return -1;
  }
  if (fstat(md, &file_stat)) {
    perror("fstat error\n");
    return -1;
  }
  return file_stat.st_size;
}


x86 assembly (clang, -O0):
<get_memory_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1192 <get_memory_size+0x52>
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c9 <get_memory_size+0x89>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     11c1 <get_memory_size+0x81>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c9 <get_memory_size+0x89>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_memory_size>:
push   %rax
mov    0x2eb0(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1177 <get_memory_size+0x37>
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
jne    118c <get_memory_size+0x4c>
mov    (%rsp),%rax
pop    %rcx
ret
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%rax
pop    %rcx
ret
lea    0xe6d(%rip),%rdi
call   1040 <perror@plt>
jmp    1183 <get_memory_size+0x43>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [26 x i8] c"Got an error opening shm\0A\00"
@.str.1 = [13 x i8] c"fstat error\0A\00"
define i64 @get_memory_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = call i32 @shm_open(i8* %6, i32 %9, i32 384)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %24
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i64 @fstat(i32 %16, %struct.stat* %5)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %2
br label %24
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%23 = load i64, i64* %22
store i64 %23, i64* %2
br label %24
24: ; preds = %21, %19, %13
%25 = load i64, i64* %2
ret i64 %25
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"fstat error\0A\00"
@str = [25 x i8] c"Got an error opening shm\00"
define i64 @get_memory_size(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = tail call i32 @shm_open(i8* %0, i32 %6, i32 384)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %19
11: ; preds = %1
%12 = call i64 @fstat(i32 %7, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i64, i64* %17
br label %19
19: ; preds = %16, %14, %9
%20 = phi i64 [ -1, %9 ], [ -1, %14 ], [ %18, %16 ]
ret i64 %20
}