#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 */
struct passwd {
  char *pw_name;
};

/* Variables and functions */
int execve(char *, char **, char **);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ printf(char *, int);
int setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int main(int argc, char **argv) {
  struct passwd *passwd = getpwuid(getuid());
  int errno = setreuid(geteuid(), geteuid());
  if (errno == 0 && passwd != 0) {
    char user_evar[100];
    snprintf(user_evar, 80, "CHECKIT_USER=%s", passwd->pw_name);
    char *envp[] = {"PATH=/bin:/usr/bin",
                    "HOME=/home2/ling572_00",
                    "JAVA_HOME=/usr/java/latest",
                    "JAVA_OPTS=-Xmx300m -Xms140m",
                    "LC_ALL=en_US.UTF-8",
                    user_evar,
                    (char *)0};
    errno = execve("/home2/ling572_00/Projects/CheckIt/bin/check_it.groovy",
                   argv, envp);
  }
  printf("An error occured %d\n", errno);
  return errno;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0xc4(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    -0xc4(%rbp),%edi
mov    %eax,%esi
call   1090 <setreuid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1266 <main+0xf6>
cmpq   $0x0,-0x18(%rbp)
je     1266 <main+0xf6>
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    $0x50,%esi
lea    0xe15(%rip),%rdx
call   1060 <snprintf@plt>
lea    0xe19(%rip),%rax
mov    %rax,-0xc0(%rbp)
lea    0xe1e(%rip),%rax
mov    %rax,-0xb8(%rbp)
lea    0xe27(%rip),%rax
mov    %rax,-0xb0(%rbp)
lea    0xe34(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xe42(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x98(%rbp)
movq   $0x0,-0x90(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0xc0(%rbp),%rdx
lea    0xe26(%rip),%rdi
call   1080 <execve@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xb0,%rsp
mov    %rsi,%r14
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1090 <setreuid@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    122c <main+0xbc>
test   %rbx,%rbx
je     122c <main+0xbc>
mov    (%rbx),%rcx
lea    0xe43(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1060 <snprintf@plt>
lea    0xe3a(%rip),%rax
mov    %rax,(%rsp)
lea    0xe42(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe4d(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe5c(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe6c(%rip),%rax
mov    %rax,0x20(%rsp)
mov    %rbx,0x28(%rsp)
movq   $0x0,0x30(%rsp)
lea    0xe65(%rip),%rdi
mov    %rsp,%rdx
mov    %r14,%rsi
call   1080 <execve@plt>
mov    %eax,%ebp
lea    0xe88(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [16 x i8] c"CHECKIT_USER=%s\00"
@.str.1 = [19 x i8] c"PATH=/bin:/usr/bin\00"
@.str.2 = [23 x i8] c"HOME=/home2/ling572_00\00"
@.str.3 = [27 x i8] c"JAVA_HOME=/usr/java/latest\00"
@.str.4 = [28 x i8] c"JAVA_OPTS=-Xmx300m -Xms140m\00"
@.str.5 = [19 x i8] c"LC_ALL=en_US.UTF-8\00"
@.str.6 = [55 x i8] c"/home2/ling572_00/Projects/CheckIt/bin/check_it.groovy\00"
@.str.7 = [21 x i8] c"An error occured %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca [7 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getuid()
%11 = call %struct.passwd* @getpwuid(i32 %10)
store %struct.passwd* %11, %struct.passwd** %6
%12 = call i32 (...) @geteuid()
%13 = call i32 (...) @geteuid()
%14 = call i32 @setreuid(i32 %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %37
17: ; preds = %2
%18 = load %struct.passwd*, %struct.passwd** %6
%19 = icmp ne %struct.passwd* %18, null
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%22 = load %struct.passwd*, %struct.passwd** %6
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @snprintf(i8* %21, i32 80, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [7 x i8*], [7 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %26
%27 = getelementptr inbounds i8*, i8** %26, i64 1
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %27
%28 = getelementptr inbounds i8*, i8** %27, i64 1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8** %28
%29 = getelementptr inbounds i8*, i8** %28, i64 1
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8** %29
%30 = getelementptr inbounds i8*, i8** %29, i64 1
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8** %30
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
store i8* %32, i8** %31
%33 = getelementptr inbounds i8*, i8** %31, i64 1
store i8* null, i8** %33
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds [7 x i8*], [7 x i8*]* %9, i64 0, i64 0
%36 = call i32 @execve(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0), i8** %34, i8** %35)
store i32 %36, i32* %7
br label %37
37: ; preds = %20, %17, %2
%38 = load i32, i32* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [16 x i8] c"CHECKIT_USER=%s\00"
@.str.1 = [19 x i8] c"PATH=/bin:/usr/bin\00"
@.str.2 = [23 x i8] c"HOME=/home2/ling572_00\00"
@.str.3 = [27 x i8] c"JAVA_HOME=/usr/java/latest\00"
@.str.4 = [28 x i8] c"JAVA_OPTS=-Xmx300m -Xms140m\00"
@.str.5 = [19 x i8] c"LC_ALL=en_US.UTF-8\00"
@.str.6 = [55 x i8] c"/home2/ling572_00/Projects/CheckIt/bin/check_it.groovy\00"
@.str.7 = [21 x i8] c"An error occured %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [100 x i8]
%4 = alloca [7 x i8*]
%5 = tail call i32 (...) @getuid()
%6 = tail call %struct.passwd* @getpwuid(i32 %5)
%7 = tail call i32 (...) @geteuid()
%8 = tail call i32 (...) @geteuid()
%9 = tail call i32 @setreuid(i32 %7, i32 %8)
%10 = icmp eq i32 %9, 0
%11 = icmp ne %struct.passwd* %6, null
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %27
13: ; preds = %2
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @snprintf(i8* %14, i32 80, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = bitcast [7 x i8*]* %4 to i8*
%19 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8** %21
%22 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8** %23
%24 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 5
store i8* %14, i8** %24
%25 = getelementptr inbounds [7 x i8*], [7 x i8*]* %4, i64 0, i64 6
store i8* null, i8** %25
%26 = call i32 @execve(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0), i8** %1, i8** %19)
br label %27
27: ; preds = %13, %2
%28 = phi i32 [ %26, %13 ], [ %9, %2 ]
%29 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %28)
ret i32 %28
}