#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*/ intmax_t;
typedef scalar_t__ gid_t;

/* Variables and functions */
scalar_t__ choose_gid();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ getuid();
int /*<<< orphan*/ printf(char *, ...);
int run_executable_sgid(scalar_t__);
char *secure_getenv(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int do_test(void) {
  if (getenv("PATH") == NULL) {
    printf("PATH not set\n");
    exit(1);
  }
  if (secure_getenv("PATH") == NULL) {
    printf("PATH not set according to secure_getenv\n");
    exit(1);
  }
  if (strcmp(getenv("PATH"), secure_getenv("PATH")) != 0) {
    printf("PATH mismatch (%s, %s)\n", getenv("PATH"), secure_getenv("PATH"));
    exit(1);
  }
  gid_t target = choose_gid();
  if (target == 0) {
    fprintf(stderr,
            "Could not find a suitable GID for user %jd, skipping test\n",
            (intmax_t)getuid());
    exit(0);
  }
  return run_executable_sgid(target);
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
jne    11c6 <do_test+0x36>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe33(%rip),%rdi
call   1040 <secure_getenv@plt>
cmp    $0x0,%rax
jne    11f4 <do_test+0x64>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe05(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
lea    0xdf5(%rip),%rdi
call   1040 <secure_getenv@plt>
mov    -0x10(%rbp),%rdi
mov    %rax,%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1261 <do_test+0xd1>
lea    0xdd3(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xdc3(%rip),%rdi
call   1040 <secure_getenv@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10a0 <choose_gid@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    12a2 <do_test+0x112>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xdbe(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1090 <run_executable_sgid@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
push   %rax
lea    0xe55(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     120b <do_test+0x6b>
lea    0xe44(%rip),%rdi
call   1040 <secure_getenv@plt>
test   %rax,%rax
je     1214 <do_test+0x74>
lea    0xe33(%rip),%r14
mov    %r14,%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <secure_getenv@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    122a <do_test+0x8a>
xor    %eax,%eax
call   10b0 <choose_gid@plt>
test   %rax,%rax
je     1262 <do_test+0xc2>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10a0 <run_executable_sgid@plt>
lea    0xe6e(%rip),%rdi
jmp    121b <do_test+0x7b>
lea    0xe3d(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdcf(%rip),%r14
mov    %r14,%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <secure_getenv@plt>
lea    0xdba(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1060 <getuid@plt>
lea    0xda4(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [14 x i8] c"PATH not set\0A\00"
@.str.2 = [41 x i8] c"PATH not set according to secure_getenv\0A\00"
@.str.3 = [24 x i8] c"PATH mismatch (%s, %s)\0A\00"
@stderr = global i32 0
@.str.4 = [59 x i8] c"Could not find a suitable GID for user %jd, skipping test\0A\00"
define i32 @do_test() {
%1 = alloca i64
%2 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %7
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i64 @strcmp(i8* %14, i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %19, i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %13
%24 = call i64 (...) @choose_gid()
store i64 %24, i64* %1
%25 = load i64, i64* %1
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i64 (...) @getuid()
%30 = trunc i64 %29 to i32
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %23
%34 = load i64, i64* %1
%35 = call i32 @run_executable_sgid(i64 %34)
ret i32 %35
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.3 = [24 x i8] c"PATH mismatch (%s, %s)\0A\00"
@stderr = global i32 0
@.str.4 = [59 x i8] c"Could not find a suitable GID for user %jd, skipping test\0A\00"
@str = [40 x i8] c"PATH not set according to secure_getenv\00"
@str.5 = [13 x i8] c"PATH not set\00"
define i32 @do_test() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = tail call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = tail call i64 @strcmp(i8* %13, i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = tail call i8* @secure_getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = tail call i64 (...) @choose_gid()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i64 (...) @getuid()
%28 = trunc i64 %27 to i32
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = tail call i32 @exit(i32 0)
unreachable
31: ; preds = %22
%32 = tail call i32 @run_executable_sgid(i64 %23)
ret i32 %32
}