#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;
struct stat {
  scalar_t__ st_uid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ clean_environ();
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getegid();
scalar_t__ geteuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setregid(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setreuid(scalar_t__, scalar_t__);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ umask(int);
int main(int argc, char **argv) {
  struct stat statb;
  gid_t egid = getegid();
  uid_t euid = geteuid();
  if ("/usr/bin/gruntreceive"[0] != '/') {
    fprintf(stderr, "%s: %s is not a full path name\n", argv[0],
            "/usr/bin/gruntreceive");
    fprintf(stderr, "You can only use this wrapper if you\n");
    fprintf(stderr, "compile it with an absolute path.\n");
    exit(1);
  }
  if (stat("/usr/bin/gruntreceive", &statb) < 0) {
    perror("stat");
    exit(1);
  }
  if (statb.st_uid != 0 && statb.st_uid != euid) {
    fprintf(stderr, "%s: %s has the wrong owner\n", argv[0],
            "/usr/bin/gruntreceive");
    fprintf(stderr, "The script should be owned by root,\n");
    fprintf(stderr, "and shouldn't be writeable by anyone.\n");
    exit(1);
  }
  if (setregid(egid, egid) < 0)
    perror("setregid");
  if (setreuid(euid, euid) < 0)
    perror("setreuid");
  clean_environ();
  umask(077);
  while (**argv == '-')
    (*argv)++;
  execv("/usr/bin/gruntreceive", argv);
  fprintf(stderr, "%s: could not execute the script\n", argv[0]);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getegid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,-0x28(%rbp)
lea    0xe1e(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    120b <main+0x5b>
lea    0xe1a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     1281 <main+0xd1>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1281 <main+0xd1>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde0(%rip),%rsi
lea    0xdbe(%rip),%rcx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   10a0 <setregid@plt>
cmp    $0x0,%rax
jge    12a2 <main+0xf2>
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <setreuid@plt>
cmp    $0x0,%rax
jge    12c5 <main+0x115>
lea    0xdcc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1030 <clean_environ@plt>
mov    $0x3f,%edi
call   1060 <umask@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    12fc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    12d6 <main+0x126>
mov    -0x10(%rbp),%rsi
lea    0xcf9(%rip),%rdi
call   10d0 <execv@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1080 <getegid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %rax,%r14
lea    0xe2b(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jns    11fa <main+0x4a>
lea    0xe2b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1257 <main+0xa7>
cmp    %r14,%rax
je     1257 <main+0xa7>
mov    0x2dd0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    (%rbx),%rdx
lea    0xdfe(%rip),%rsi
lea    0xddc(%rip),%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe02(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   10a0 <setregid@plt>
test   %rax,%rax
jns    1271 <main+0xc1>
lea    0xe17(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14,%rdi
mov    %r14,%rsi
call   1090 <setreuid@plt>
test   %rax,%rax
jns    128d <main+0xdd>
lea    0xe04(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1030 <clean_environ@plt>
mov    $0x3f,%edi
call   1060 <umask@plt>
mov    (%rbx),%rax
cmpb   $0x2d,(%rax)
jne    12bc <main+0x10c>
add    $0x1,%rax
nopw   0x0(%rax,%rax,1)
mov    %rax,(%rbx)
cmpb   $0x2d,(%rax)
lea    0x1(%rax),%rax
je     12b0 <main+0x100>
lea    0xd3d(%rip),%rdi
mov    %rbx,%rsi
call   10d0 <execv@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdb7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [22 x i8] c"/usr/bin/gruntreceive\00"
@.str.1 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"%s: %s has the wrong owner\0A\00"
@.str.3 = [37 x i8] c"The script should be owned by root,\0A\00"
@.str.4 = [39 x i8] c"and shouldn't be writeable by anyone.\0A\00"
@.str.5 = [9 x i8] c"setregid\00"
@.str.6 = [9 x i8] c"setreuid\00"
@.str.7 = [34 x i8] c"%s: could not execute the script\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getegid()
store i32 %9, i32* %7
%10 = call i64 (...) @geteuid()
store i64 %10, i64* %8
%11 = call i64 @stat(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i64, i64* %17
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* %8
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %36
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %20, %16
%37 = load i32, i32* %7
%38 = load i32, i32* %7
%39 = call i64 @setregid(i32 %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i64, i64* %8
%45 = load i64, i64* %8
%46 = call i64 @setreuid(i64 %44, i64 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = call i32 (...) @clean_environ()
%52 = call i32 @umask(i32 63)
br label %53
53: ; preds = %59, %50
%54 = load i8**, i8*** %5
%55 = load i8*, i8** %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 45
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i8**, i8*** %5
%61 = load i8*, i8** %60
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %60
br label %53
63: ; preds = %53
%64 = load i8**, i8*** %5
%65 = call i32 @execv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %64)
%66 = load i32, i32* @stderr
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 0
%69 = load i8*, i8** %68
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [22 x i8] c"/usr/bin/gruntreceive\00"
@.str.1 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"%s: %s has the wrong owner\0A\00"
@.str.3 = [37 x i8] c"The script should be owned by root,\0A\00"
@.str.4 = [39 x i8] c"and shouldn't be writeable by anyone.\0A\00"
@.str.5 = [9 x i8] c"setregid\00"
@.str.6 = [9 x i8] c"setreuid\00"
@.str.7 = [34 x i8] c"%s: could not execute the script\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 (...) @getegid()
%6 = tail call i64 (...) @geteuid()
%7 = call i64 @stat(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
%16 = icmp eq i64 %14, %6
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %27, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %12
%28 = call i64 @setregid(i32 %5, i32 %5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = call i64 @setreuid(i64 %6, i64 %6)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i32 (...) @clean_environ()
%39 = call i32 @umask(i32 63)
%40 = load i8*, i8** %1
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 45
br i1 %42, label %43, label %48
43: ; preds = %37, %43
%44 = phi i8* [ %45, %43 ], [ %40, %37 ]
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8* %45, i8** %1
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 45
br i1 %47, label %43, label %48
48: ; preds = %43, %37
%49 = call i32 @execv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %1)
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %1
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
}