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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ P_PID;
int /*<<< orphan*/ WSTOPPED;
int /*<<< orphan*/ atol(char const *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
long waitid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
            int /*<<< orphan*/);
FILE *fopen_mem(const char *pid, bool stop_process) {
  if (stop_process) {
    pid_t p = atol(pid);
    long r = ptrace(PTRACE_ATTACH, p, 0, 0);
    if (r == -1) {
      perror("ptrace attach failed");
      return 0;
    }
    siginfo_t info;
    r = waitid(P_PID, p, &info, WSTOPPED);
    if (r == -1) {
      perror(0);
      return 0;
    }
  }
  char filename[64];
  sprintf(filename, "/proc/%s/mem", pid);
  FILE *f = fopen(filename, "rb");
  if (!f) {
    perror("open /proc/$pid/mem failed");
  }
  return f;
}


x86 assembly (clang, -O0):
<fopen_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     120d <fopen_mem+0xad>
mov    -0x10(%rbp),%rdi
call   1040 <atol@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <ptrace@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11c5 <fopen_mem+0x65>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1254 <fopen_mem+0xf4>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
lea    -0x24(%rbp),%rdx
call   1080 <waitid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1208 <fopen_mem+0xa8>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1254 <fopen_mem+0xf4>
jmp    120d <fopen_mem+0xad>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdf9(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    124c <fopen_mem+0xec>
lea    0xdde(%rip),%rdi
call   1060 <perror@plt>
mov    -0x78(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_mem>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
test   %esi,%esi
je     11b3 <fopen_mem+0x53>
mov    %rbx,%rdi
call   1040 <atol@plt>
mov    %eax,%ebp
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
je     11f2 <fopen_mem+0x92>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %ebp,%esi
call   1080 <waitid@plt>
cmp    $0xffffffffffffffff,%rax
je     1202 <fopen_mem+0xa2>
lea    0xe5b(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
lea    0xe53(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e9 <fopen_mem+0x89>
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe35(%rip),%rdi
jmp    1206 <fopen_mem+0xa6>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11df <fopen_mem+0x7f>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <perror@plt>
jmp    11df <fopen_mem+0x7f>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@.str = [21 x i8] c"ptrace attach failed\00"
@P_PID = global i32 0
@WSTOPPED = global i32 0
@.str.1 = [13 x i8] c"/proc/%s/mem\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [27 x i8] c"open /proc/$pid/mem failed\00"
define i32* @fopen_mem(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @atol(i8* %14)
store i32 %15, i32* %6
%16 = load i32, i32* @PTRACE_ATTACH
%17 = load i32, i32* %6
%18 = call i64 @ptrace(i32 %16, i32 %17, i32 0, i32 0)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %3
br label %45
23: ; preds = %13
%24 = load i32, i32* @P_PID
%25 = load i32, i32* %6
%26 = load i32, i32* @WSTOPPED
%27 = call i64 @waitid(i32 %24, i32 %25, i32* %8, i32 %26)
store i64 %27, i64* %7
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @perror(i8* null)
store i32* null, i32** %3
br label %45
32: ; preds = %23
br label %33
33: ; preds = %32, %2
%34 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%35 = load i8*, i8** %4
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %10
%39 = load i32*, i32** %10
%40 = icmp ne i32* %39, null
br i1 %40, label %43, label %41
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %33
%44 = load i32*, i32** %10
store i32* %44, i32** %3
br label %45
45: ; preds = %43, %30, %21
%46 = load i32*, i32** %3
ret i32* %46
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str = [21 x i8] c"ptrace attach failed\00"
@P_PID = global i32 0
@WSTOPPED = global i32 0
@.str.1 = [13 x i8] c"/proc/%s/mem\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [27 x i8] c"open /proc/$pid/mem failed\00"
define noalias i32* @fopen_mem(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = icmp eq i32 %1, 0
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = tail call i32 @atol(i8* %0)
%8 = load i32, i32* @PTRACE_ATTACH
%9 = tail call i64 @ptrace(i32 %8, i32 %7, i32 0, i32 0)
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %30
13: ; preds = %6
%14 = bitcast i32* %3 to i8*
%15 = load i32, i32* @P_PID
%16 = load i32, i32* @WSTOPPED
%17 = call i64 @waitid(i32 %15, i32 %7, i32* %3, i32 %16)
%18 = icmp eq i64 %17, -1
br i1 %18, label %20, label %19
19: ; preds = %13
br label %22
20: ; preds = %13
%21 = call i32 @perror(i8* null)
br label %30
22: ; preds = %19, %2
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%24 = call i32 @sprintf(i8* %23, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%25 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
br label %30
30: ; preds = %20, %11, %29
%31 = phi i32* [ null, %20 ], [ %25, %29 ], [ null, %11 ]
ret i32* %31
}