#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*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int pread(int, void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
ssize_t ignotum_mem_read(pid_t pid, void *buf, size_t n, off_t addr) {
  char pathbuf[32], *filename;
  ssize_t ret;
  if (!pid) {
    filename = "/proc/self/mem";
  } else {
    filename = pathbuf;
    sprintf(pathbuf, "/proc/%d/mem", pid);
  }
  int fd = open(filename, O_RDONLY);
  if (fd == -1) {
    ret = -1;
    goto end;
  }
  ret = pread(fd, buf, n, addr);
  close(fd);
end:
  return ret;
}


x86 assembly (clang, -O0):
<ignotum_mem_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    1170 <ignotum_mem_read+0x30>
lea    0xe99(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    118b <ignotum_mem_read+0x4b>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe89(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x48(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    11b6 <ignotum_mem_read+0x76>
movl   $0xffffffff,-0x4c(%rbp)
jmp    11d4 <ignotum_mem_read+0x94>
mov    -0x50(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <pread@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%edi
call   1030 <close@plt>
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ignotum_mem_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
test   %edi,%edi
je     116d <ignotum_mem_read+0x2d>
mov    %edi,%edx
mov    %rsp,%rbx
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
jmp    1174 <ignotum_mem_read+0x34>
lea    0xe8c(%rip),%rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11a9 <ignotum_mem_read+0x69>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1050 <pread@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
jmp    11ae <ignotum_mem_read+0x6e>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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