#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 ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ mem;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int pread(int, void *, size_t, unsigned long);
int /*<<< orphan*/ show_error(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
ssize_t readregion(pid_t target, void *buf, size_t count,
                   unsigned long offset) {
  char mem[32];
  int fd;
  ssize_t len;
  snprintf(mem, sizeof(mem), "/proc/%d/mem", target);
  if ((fd = open(mem, O_RDONLY)) == -1) {
    show_error("unable to open %s.\n", mem);
    return -1;
  }
  len = pread(fd, buf, count, offset);
  close(fd);
  return len;
}


x86 assembly (clang, -O0):
<readregion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe86(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <readregion+0x69>
lea    -0x40(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1050 <show_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <readregion+0x8e>
mov    -0x44(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <pread@plt>
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <close@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readregion>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xe94(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11af <readregion+0x5f>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <pread@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11c3 <readregion+0x73>
lea    0xe57(%rip),%rdi
mov    %rsp,%rsi
call   1050 <show_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"unable to open %s.\0A\00"
define i32 @readregion(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca [32 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%14 = load i32, i32* %6
%15 = call i32 @snprintf(i8* %13, i32 32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %11
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%22 = call i32 @show_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %32
23: ; preds = %4
%24 = load i32, i32* %11
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = load i64, i64* %9
%28 = call i32 @pread(i32 %24, i8* %25, i64 %26, i64 %27)
store i32 %28, i32* %12
%29 = load i32, i32* %11
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %12
store i32 %31, i32* %5
br label %32
32: ; preds = %23, %20
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"unable to open %s.\0A\00"
define i32 @readregion(i32 %0, i8* nocapture %1, i64 %2, i64 %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = call i32 @show_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %16
13: ; preds = %4
%14 = call i32 @pread(i32 %9, i8* %1, i64 %2, i64 %3)
%15 = call i32 @close(i32 %9)
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ -1, %11 ], [ %14, %13 ]
ret i32 %17
}