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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ atol(char const *);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/);
int fclose_mem(const char *pid, FILE *f, bool stop_process) {
  int rc = 0;
  if (f) {
    int r = fclose(f);
    if (r) {
      perror(0);
      rc = -1;
    }
  }
  if (stop_process) {
    pid_t p = atol(pid);
    long r = ptrace(PTRACE_DETACH, p, 0, 0);
    if (r == -1) {
      perror(0);
      return -1;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<fclose_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118e <fclose_mem+0x4e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1189 <fclose_mem+0x49>
xor    %edi,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    118e <fclose_mem+0x4e>
cmpl   $0x0,-0x1c(%rbp)
je     11e0 <fclose_mem+0xa0>
mov    -0x10(%rbp),%rdi
call   1050 <atol@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <ptrace@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11db <fclose_mem+0x9b>
xor    %edi,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <fclose_mem+0xa6>
jmp    11e0 <fclose_mem+0xa0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fclose_mem>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
test   %rsi,%rsi
je     1168 <fclose_mem+0x28>
mov    %rsi,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
je     1168 <fclose_mem+0x28>
xor    %edi,%edi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
test   %ebp,%ebp
je     119a <fclose_mem+0x5a>
mov    %r14,%rdi
call   1050 <atol@plt>
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
jne    119a <fclose_mem+0x5a>
xor    %edi,%edi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_DETACH = global i32 0
define i32 @fclose_mem(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %22
14: ; preds = %3
%15 = load i32*, i32** %6
%16 = call i32 @fclose(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i32 0)
store i32 -1, i32* %8
br label %21
21: ; preds = %19, %14
br label %22
22: ; preds = %21, %3
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = call i32 @atol(i8* %26)
store i32 %27, i32* %10
%28 = load i32, i32* @PTRACE_DETACH
%29 = load i32, i32* %10
%30 = call i64 @ptrace(i32 %28, i32 %29, i32 0, i32 0)
store i64 %30, i64* %11
%31 = load i64, i64* %11
%32 = icmp eq i64 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @perror(i32 0)
store i32 -1, i32* %4
br label %38
35: ; preds = %25
br label %36
36: ; preds = %35, %22
%37 = load i32, i32* %8
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@PTRACE_DETACH = global i32 0
define i32 @fclose_mem(i8* nocapture %0, i32* %1, i32 %2) {
%4 = icmp eq i32* %1, null
br i1 %4, label %10, label %5
5: ; preds = %3
%6 = tail call i32 @fclose(i32* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @perror(i32 0)
br label %10
10: ; preds = %5, %8, %3
%11 = phi i32 [ 0, %3 ], [ -1, %8 ], [ 0, %5 ]
%12 = icmp eq i32 %2, 0
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = tail call i32 @atol(i8* %0)
%15 = load i32, i32* @PTRACE_DETACH
%16 = tail call i64 @ptrace(i32 %15, i32 %14, i32 0, i32 0)
%17 = icmp eq i64 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @perror(i32 0)
br label %20
20: ; preds = %10, %13, %18
%21 = phi i32 [ -1, %18 ], [ %11, %13 ], [ %11, %10 ]
ret i32 %21
}