#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 scalar_t__ uid_t;
typedef scalar_t__ gid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int enter_ns(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ get_process_info(int, scalar_t__ *, scalar_t__ *, int *);
int /*<<< orphan*/ get_tmp_path(int);
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ is_openj9_process(int);
int jattach_hotspot(int, int, int, char **);
int jattach_openj9(int, int, int, char **);
int /*<<< orphan*/ perror(char *);
scalar_t__ setegid(scalar_t__);
scalar_t__ seteuid(scalar_t__);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
__attribute__((visibility("default"))) int jattach(int pid, int argc,
                                                   char **argv) {
  uid_t my_uid = geteuid();
  gid_t my_gid = getegid();
  uid_t target_uid = my_uid;
  gid_t target_gid = my_gid;
  int nspid;
  if (get_process_info(pid, &target_uid, &target_gid, &nspid) < 0) {
    fprintf(stderr, "Process %d not found\n", pid);
    return 1;
  }
  enter_ns(pid, "net");
  enter_ns(pid, "ipc");
  int mnt_changed = enter_ns(pid, "mnt");
  if ((my_gid != target_gid && setegid(target_gid) != 0) ||
      (my_uid != target_uid && seteuid(target_uid) != 0)) {
    perror("Failed to change credentials to match the target process");
    return 1;
  }
  get_tmp_path(mnt_changed > 0 ? nspid : pid);
  signal(SIGPIPE, SIG_IGN);
  if (is_openj9_process(nspid)) {
    return jattach_openj9(pid, nspid, argc, argv);
  } else {
    return jattach_hotspot(pid, nspid, argc, argv);
  }
}


x86 assembly (clang, -O0):
<jattach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <getegid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
call   10b0 <get_process_info@plt>
cmp    $0x0,%rax
jge    124a <jattach+0x7a>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdc7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134f <jattach+0x17f>
mov    -0x8(%rbp),%edi
lea    0xdc2(%rip),%rsi
call   1060 <enter_ns@plt>
mov    -0x8(%rbp),%edi
lea    0xdb7(%rip),%rsi
call   1060 <enter_ns@plt>
mov    -0x8(%rbp),%edi
lea    0xdac(%rip),%rsi
call   1060 <enter_ns@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     129b <jattach+0xcb>
mov    -0x38(%rbp),%rdi
call   10e0 <setegid@plt>
cmp    $0x0,%rax
jne    12bc <jattach+0xec>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12d4 <jattach+0x104>
mov    -0x30(%rbp),%rdi
call   10f0 <seteuid@plt>
cmp    $0x0,%rax
je     12d4 <jattach+0x104>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134f <jattach+0x17f>
cmpl   $0x0,-0x40(%rbp)
jle    12e9 <jattach+0x119>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    12ef <jattach+0x11f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <get_tmp_path@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    -0x3c(%rbp),%edi
call   10d0 <is_openj9_process@plt>
cmp    $0x0,%rax
je     133a <jattach+0x16a>
mov    -0x8(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1070 <jattach_openj9@plt>
mov    %eax,-0x4(%rbp)
jmp    134f <jattach+0x17f>
mov    -0x8(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   10c0 <jattach_hotspot@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jattach>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1090 <getegid@plt>
mov    %rax,%rbx
mov    %r12,0x10(%rsp)
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
call   10b0 <get_process_info@plt>
test   %rax,%rax
js     12c4 <jattach+0xf4>
lea    0xdec(%rip),%rsi
mov    %ebp,%edi
call   1060 <enter_ns@plt>
lea    0xde2(%rip),%rsi
mov    %ebp,%edi
call   1060 <enter_ns@plt>
lea    0xdd8(%rip),%rsi
mov    %ebp,%edi
call   1060 <enter_ns@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rdi
cmp    %rdi,%rbx
je     1268 <jattach+0x98>
call   10e0 <setegid@plt>
test   %rax,%rax
jne    12f6 <jattach+0x126>
mov    0x10(%rsp),%rdi
cmp    %rdi,%r12
je     127c <jattach+0xac>
call   10f0 <seteuid@plt>
test   %rax,%rax
jne    12f6 <jattach+0x126>
mov    %ebp,%edi
test   %r13d,%r13d
jle    1287 <jattach+0xb7>
mov    0x4(%rsp),%edi
call   1080 <get_tmp_path@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x4(%rsp),%edi
call   10d0 <is_openj9_process@plt>
mov    0x4(%rsp),%esi
mov    %ebp,%edi
mov    %r15d,%edx
mov    %r14,%rcx
test   %rax,%rax
je     12e2 <jattach+0x112>
call   1070 <jattach_openj9@plt>
jmp    12e7 <jattach+0x117>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    12e7 <jattach+0x117>
call   10c0 <jattach_hotspot@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd25(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    12e7 <jattach+0x117>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Process %d not found\0A\00"
@.str.1 = [4 x i8] c"net\00"
@.str.2 = [4 x i8] c"ipc\00"
@.str.3 = [4 x i8] c"mnt\00"
@.str.4 = [57 x i8] c"Failed to change credentials to match the target process\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @jattach(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%14 = call i64 (...) @geteuid()
store i64 %14, i64* %8
%15 = call i64 (...) @getegid()
store i64 %15, i64* %9
%16 = load i64, i64* %8
store i64 %16, i64* %10
%17 = load i64, i64* %9
store i64 %17, i64* %11
%18 = load i32, i32* %5
%19 = call i64 @get_process_info(i32 %18, i64* %10, i64* %11, i32* %12)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i32, i32* %5
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 1, i32* %4
br label %77
25: ; preds = %3
%26 = load i32, i32* %5
%27 = call i32 @enter_ns(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %5
%29 = call i32 @enter_ns(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %5
%31 = call i32 @enter_ns(i32 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i32 %31, i32* %13
%32 = load i64, i64* %9
%33 = load i64, i64* %11
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %25
%36 = load i64, i64* %11
%37 = call i64 @setegid(i64 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %47, label %39
39: ; preds = %35, %25
%40 = load i64, i64* %8
%41 = load i64, i64* %10
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i64, i64* %10
%45 = call i64 @seteuid(i64 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43, %35
%48 = call i32 @perror(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %4
br label %77
49: ; preds = %43, %39
%50 = load i32, i32* %13
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i32, i32* %12
br label %56
54: ; preds = %49
%55 = load i32, i32* %5
br label %56
56: ; preds = %54, %52
%57 = phi i32 [ %53, %52 ], [ %55, %54 ]
%58 = call i32 @get_tmp_path(i32 %57)
%59 = load i32, i32* @SIGPIPE
%60 = load i32, i32* @SIG_IGN
%61 = call i32 @signal(i32 %59, i32 %60)
%62 = load i32, i32* %12
%63 = call i64 @is_openj9_process(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %56
%66 = load i32, i32* %5
%67 = load i32, i32* %12
%68 = load i32, i32* %6
%69 = load i8**, i8*** %7
%70 = call i32 @jattach_openj9(i32 %66, i32 %67, i32 %68, i8** %69)
store i32 %70, i32* %4
br label %77
71: ; preds = %56
%72 = load i32, i32* %5
%73 = load i32, i32* %12
%74 = load i32, i32* %6
%75 = load i8**, i8*** %7
%76 = call i32 @jattach_hotspot(i32 %72, i32 %73, i32 %74, i8** %75)
store i32 %76, i32* %4
br label %77
77: ; preds = %71, %65, %47, %21
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Process %d not found\0A\00"
@.str.1 = [4 x i8] c"net\00"
@.str.2 = [4 x i8] c"ipc\00"
@.str.3 = [4 x i8] c"mnt\00"
@.str.4 = [57 x i8] c"Failed to change credentials to match the target process\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
define i32 @jattach(i32 %0, i32 %1, i8** %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = tail call i64 (...) @geteuid()
%8 = tail call i64 (...) @getegid()
%9 = bitcast i64* %4 to i8*
store i64 %7, i64* %4
%10 = bitcast i64* %5 to i8*
store i64 %8, i64* %5
%11 = bitcast i32* %6 to i8*
%12 = call i64 @get_process_info(i32 %0, i64* %4, i64* %5, i32* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %50
17: ; preds = %3
%18 = call i32 @enter_ns(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @enter_ns(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @enter_ns(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%21 = load i64, i64* %5
%22 = icmp eq i64 %8, %21
br i1 %22, label %26, label %23
23: ; preds = %17
%24 = call i64 @setegid(i64 %21)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23, %17
%27 = load i64, i64* %4
%28 = icmp eq i64 %7, %27
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = call i64 @seteuid(i64 %27)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29, %23
%33 = call i32 @perror(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.4, i64 0, i64 0))
br label %50
34: ; preds = %29, %26
%35 = icmp sgt i32 %20, 0
%36 = load i32, i32* %6
%37 = select i1 %35, i32 %36, i32 %0
%38 = call i32 @get_tmp_path(i32 %37)
%39 = load i32, i32* @SIGPIPE
%40 = load i32, i32* @SIG_IGN
%41 = call i32 @signal(i32 %39, i32 %40)
%42 = load i32, i32* %6
%43 = call i64 @is_openj9_process(i32 %42)
%44 = icmp eq i64 %43, 0
%45 = load i32, i32* %6
br i1 %44, label %48, label %46
46: ; preds = %34
%47 = call i32 @jattach_openj9(i32 %0, i32 %45, i32 %1, i8** %2)
br label %50
48: ; preds = %34
%49 = call i32 @jattach_hotspot(i32 %0, i32 %45, i32 %1, i8** %2)
br label %50
50: ; preds = %32, %46, %48, %14
%51 = phi i32 [ 1, %14 ], [ 1, %32 ], [ %47, %46 ], [ %49, %48 ]
ret i32 %51
}