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

/* Variables and functions */
int /*<<< orphan*/ PR_GET_DUMPABLE;
char *__secure_getenv(char const *);
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ getgid();
scalar_t__ getuid();
scalar_t__ prctl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                 int /*<<< orphan*/, int /*<<< orphan*/);
char *safe_getenv(const char *arg) {
  if ((getuid() != geteuid()) || (getgid() != getegid()))
    return NULL;
  if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
    return NULL;
  return __secure_getenv(arg);
}


x86 assembly (clang, -O0):
<safe_getenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jne    11b0 <safe_getenv+0x50>
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getegid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
je     11bd <safe_getenv+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    11fb <safe_getenv+0x9b>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
xor    %r8d,%r8d
mov    %r8d,%esi
mov    %r8d,%edx
mov    %r8d,%ecx
call   1050 <prctl@plt>
cmp    $0x0,%rax
jne    11ee <safe_getenv+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    11fb <safe_getenv+0x9b>
mov    -0x10(%rbp),%rdi
call   1080 <__secure_getenv@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_getenv>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <geteuid@plt>
cmp    %rax,%rbx
jne    11be <safe_getenv+0x5e>
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getegid@plt>
cmp    %rax,%rbx
jne    11be <safe_getenv+0x5e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <prctl@plt>
test   %rax,%rax
je     11be <safe_getenv+0x5e>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <__secure_getenv@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PR_GET_DUMPABLE = global i32 0
define i8* @safe_getenv(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %3
%4 = call i64 (...) @getuid()
%5 = call i64 (...) @geteuid()
%6 = icmp ne i64 %4, %5
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = call i64 (...) @getgid()
%9 = call i64 (...) @getegid()
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7, %1
store i8* null, i8** %2
br label %20
12: ; preds = %7
%13 = load i32, i32* @PR_GET_DUMPABLE
%14 = call i64 @prctl(i32 %13, i32 0, i32 0, i32 0, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i8* null, i8** %2
br label %20
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i8* @__secure_getenv(i8* %18)
store i8* %19, i8** %2
br label %20
20: ; preds = %17, %16, %11
%21 = load i8*, i8** %2
ret i8* %21
}

LLVM IR (-O2):
@PR_GET_DUMPABLE = global i32 0
define i8* @safe_getenv(i8* %0) {
%2 = tail call i64 (...) @getuid()
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %2, %3
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = tail call i64 (...) @getgid()
%7 = tail call i64 (...) @getegid()
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = load i32, i32* @PR_GET_DUMPABLE
%11 = tail call i64 @prctl(i32 %10, i32 0, i32 0, i32 0, i32 0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i8* @__secure_getenv(i8* %0)
br label %15
15: ; preds = %9, %1, %5, %13
%16 = phi i8* [ %14, %13 ], [ null, %5 ], [ null, %1 ], [ null, %9 ]
ret i8* %16
}