#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 uint32_t;
struct stat {
  int st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int shm_open(char *, int /*<<< orphan*/, int);
int sys_clipboard_paste(char **s, int *size) {
  int fd, bytes;
  struct stat stat;
  void *ptr;
  *size = 0;
  if ((fd = shm_open("sys_clipboard_shm", O_RDONLY,
                     (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1)
    return -1;
  if (fstat(fd, &stat) == -1 || stat.st_size < sizeof(uint32_t))
    goto paste_err;
  if ((ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
      MAP_FAILED)
    goto paste_err;
  close(fd);
  if ((bytes = *((uint32_t *)ptr)) <= 0)
    goto paste_err;
  *size = bytes;
  *s = (char *)ptr + (sizeof(uint32_t) + sizeof(uint32_t));
  return 0;
paste_err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<sys_clipboard_paste>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    0x2e5d(%rip),%rax
or     (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e33(%rip),%rax
or     (%rax),%edx
lea    0xe72(%rip),%rdi
call   1050 <shm_open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ab <sys_clipboard_paste+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <sys_clipboard_paste+0x123>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ce <sys_clipboard_paste+0x8e>
movslq -0x28(%rbp),%rax
cmp    $0x4,%rax
jae    11d3 <sys_clipboard_paste+0x93>
jmp    1254 <sys_clipboard_paste+0x114>
mov    -0x28(%rbp),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <sys_clipboard_paste+0xd1>
jmp    1254 <sys_clipboard_paste+0x114>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     1230 <sys_clipboard_paste+0xf0>
jmp    1254 <sys_clipboard_paste+0x114>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rcx
add    $0x8,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1263 <sys_clipboard_paste+0x123>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_clipboard_paste>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
movl   $0x0,(%rsi)
mov    0x2e51(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e67(%rip),%rax
mov    0x2e48(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%edx
lea    0xe74(%rip),%rdi
call   1050 <shm_open@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     120a <sys_clipboard_paste+0xca>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     1203 <sys_clipboard_paste+0xc3>
mov    0x8(%rsp),%esi
cmp    $0x4,%esi
jb     1203 <sys_clipboard_paste+0xc3>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dc7(%rip),%rax
cmp    (%rax),%rbp
je     1203 <sys_clipboard_paste+0xc3>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x0(%rbp),%eax
test   %eax,%eax
jle    1203 <sys_clipboard_paste+0xc3>
mov    %eax,(%r15)
add    $0x8,%rbp
mov    %rbp,(%r14)
xor    %r12d,%r12d
jmp    120a <sys_clipboard_paste+0xca>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"sys_clipboard_shm\00"
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @sys_clipboard_paste(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
store i8** %0, i8*** %4
store i32* %1, i32** %5
%10 = load i32*, i32** %5
store i32 0, i32* %10
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i32 @shm_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %18)
store i32 %19, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 -1, i32* %3
br label %59
22: ; preds = %2
%23 = load i32, i32* %6
%24 = call i32 @fstat(i32 %23, %struct.stat* %8)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = icmp ult i64 %29, 4
br i1 %30, label %31, label %32
31: ; preds = %26, %22
br label %56
32: ; preds = %26
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %6
%38 = call i8* @mmap(i32* null, i32 %34, i32 %35, i32 %36, i32 %37, i32 0)
store i8* %38, i8** %9
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %42
41: ; preds = %32
br label %56
42: ; preds = %32
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %9
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
store i32 %47, i32* %7
%48 = icmp sle i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %42
br label %56
50: ; preds = %42
%51 = load i32, i32* %7
%52 = load i32*, i32** %5
store i32 %51, i32* %52
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i64 8
%55 = load i8**, i8*** %4
store i8* %54, i8** %55
store i32 0, i32* %3
br label %59
56: ; preds = %49, %41, %31
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %3
br label %59
59: ; preds = %56, %50, %21
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"sys_clipboard_shm\00"
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @sys_clipboard_paste(i8** nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @shm_open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %37, label %15
15: ; preds = %2
%16 = call i32 @fstat(i32 %13, %struct.stat* %3)
%17 = icmp eq i32 %16, -1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = icmp ult i32 %19, 4
%21 = select i1 %17, i1 true, i1 %20
br i1 %21, label %35, label %22
22: ; preds = %15
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i32* null, i32 %19, i32 %23, i32 %24, i32 %13, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %35, label %28
28: ; preds = %22
%29 = tail call i32 @close(i32 %13)
%30 = bitcast i8* %25 to i32*
%31 = load i32, i32* %30
%32 = icmp slt i32 %31, 1
br i1 %32, label %35, label %33
33: ; preds = %28
store i32 %31, i32* %1
%34 = getelementptr inbounds i8, i8* %25, i64 8
store i8* %34, i8** %0
br label %37
35: ; preds = %28, %22, %15
%36 = tail call i32 @close(i32 %13)
br label %37
37: ; preds = %2, %35, %33
%38 = phi i32 [ -1, %35 ], [ 0, %33 ], [ -1, %2 ]
ret i32 %38
}