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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
char *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void readMem(int pid, unsigned int start, unsigned int len, FILE *out_file) {
  FILE *pid_mem_file;
  char *temp_buffer = calloc(1, len);
  char *tmp_name = calloc(1, 256);
  snprintf(tmp_name, 255, "/proc/%d/mem", pid);
  pid_mem_file = fopen(tmp_name, "r");
  if (pid_mem_file == NULL) {
    perror("Error opening mem for process.");
    exit(-1);
  }
  fseek(pid_mem_file, start, SEEK_SET);
  fread(temp_buffer, 1, len, pid_mem_file);
  fwrite(temp_buffer, 1, len, out_file);
  fclose(pid_mem_file);
  free(tmp_name);
  free(temp_buffer);
}


x86 assembly (clang, -O0):
<readMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x1,%edi
mov    $0x100,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0xff,%esi
lea    0xe14(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1226 <readMem+0x86>
lea    0xdf8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r13d
mov    %esi,%r12d
mov    %edi,%ebp
mov    $0x1,%edi
mov    %edx,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
mov    $0x1,%edi
mov    $0x100,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
lea    0xe22(%rip),%rdx
mov    %rax,%rdi
mov    $0xff,%esi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1264 <readMem+0xc4>
mov    %rax,%rbp
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r12d,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/mem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Error opening mem for process.\00"
@SEEK_SET = global i32 0
define void @readMem(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%12 = load i32, i32* %7
%13 = call i8* @calloc(i32 1, i32 %12)
store i8* %13, i8** %10
%14 = call i8* @calloc(i32 1, i32 256)
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = load i32, i32* %5
%17 = call i32 @snprintf(i8* %15, i32 255, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %11
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %4
%26 = load i32*, i32** %9
%27 = load i32, i32* %6
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %26, i32 %27, i32 %28)
%30 = load i8*, i8** %10
%31 = load i32, i32* %7
%32 = load i32*, i32** %9
%33 = call i32 @fread(i8* %30, i32 1, i32 %31, i32* %32)
%34 = load i8*, i8** %10
%35 = load i32, i32* %7
%36 = load i32*, i32** %8
%37 = call i32 @fwrite(i8* %34, i32 1, i32 %35, i32* %36)
%38 = load i32*, i32** %9
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %11
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %10
%43 = call i32 @free(i8* %42)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/mem\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Error opening mem for process.\00"
@SEEK_SET = global i32 0
define void @readMem(i32 %0, i32 %1, i32 %2, i32* nocapture %3) {
%5 = tail call i8* @calloc(i32 1, i32 %2)
%6 = tail call dereferenceable_or_null(256) i8* @calloc(i32 1, i32 256)
%7 = tail call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %4
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %8, i32 %1, i32 %14)
%16 = tail call i32 @fread(i8* %5, i32 1, i32 %2, i32* %8)
%17 = tail call i32 @fwrite(i8* %5, i32 1, i32 %2, i32* %3)
%18 = tail call i32 @fclose(i32* %8)
%19 = tail call i32 @free(i8* %6)
%20 = tail call i32 @free(i8* %5)
ret void
}