#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;
typedef int /*<<< orphan*/ gid_list;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(scalar_t__ *, int /*<<< orphan*/, int);
scalar_t__ setgroups(size_t, scalar_t__ *);
scalar_t__ setresgid(scalar_t__, scalar_t__, scalar_t__);
scalar_t__ setresuid(scalar_t__, scalar_t__, scalar_t__);
int /*<<< orphan*/ stderr;
scalar_t__ strtol(char *, char **, int);
int main(int argc, char **argv) {
  uid_t uid;
  gid_t gid, gid_list[128];
  int i;
  char *end_ptr;
  if (argc < 3) {
    fprintf(stderr, "Usage: %s uid:gid[:gid1:gid2:...] COMMAND [ARGS]\n",
            argv[0]);
    exit(-1);
  }
  memset(gid_list, 0, sizeof(gid_list));
  uid = (uid_t)strtol(argv[1], &end_ptr, 10);
  gid = (gid_t)strtol(end_ptr + 1, &end_ptr, 10);
  if (errno) {
    fprintf(stderr, "Usage: %s uid:gid[:gid1:gid2:...] COMMAND [ARGS]\n",
            argv[0]);
    exit(-1);
  }
  i = 0;
  while (*end_ptr != 0) {
    gid_list[i++] = (gid_t)strtol(end_ptr + 1, &end_ptr, 10);
  }
  if (setgroups((size_t)i, gid_list)) {
    error(-1, errno, "setgroups()");
  }
  if (setresgid(gid, gid, gid)) {
    error(-1, errno, "setresgid()");
  }
  if (setresuid(uid, uid, uid)) {
    error(-1, errno, "setresuid()");
  }
  execvp(argv[2], argv + 2);
  error(-1, errno, "execv()");
  return (-1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d9 <main+0x49>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe36(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x430(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
lea    -0x430(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2da8(%rip),%rax
cmpq   $0x0,(%rax)
je     1260 <main+0xd0>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdaf(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x424(%rbp)
mov    -0x430(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12bc <main+0x12c>
mov    -0x430(%rbp),%rdi
add    $0x1,%rdi
lea    -0x430(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%rcx
mov    -0x424(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x424(%rbp)
cltq
mov    %rcx,-0x420(%rbp,%rax,8)
ff
jmp    126a <main+0xda>
movslq -0x424(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1060 <setgroups@plt>
cmp    $0x0,%rax
je     12f4 <main+0x164>
mov    0x2cf8(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edi
lea    0xd43(%rip),%rdx
call   1090 <error@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <setresgid@plt>
cmp    $0x0,%rax
je     132a <main+0x19a>
mov    0x2cc2(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edi
lea    0xd19(%rip),%rdx
call   1090 <error@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <setresuid@plt>
cmp    $0x0,%rax
je     1360 <main+0x1d0>
mov    0x2c8c(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edi
lea    0xcef(%rip),%rdx
call   1090 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   10a0 <execvp@plt>
mov    0x2c5c(%rip),%rax
mov    (%rax),%rsi
mov    $0xffffffff,%edi
lea    0xccb(%rip),%rdx
call   1090 <error@plt>
mov    $0xffffffff,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jle    12fa <main+0x16a>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x8(%r15),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    0x2de4(%rip),%r13
cmpq   $0x0,0x0(%r13)
jne    12fa <main+0x16a>
mov    %rax,%r12
mov    0x8(%rsp),%rdi
cmpb   $0x0,(%rdi)
je     1248 <main+0xb8>
xor    %ebp,%ebp
lea    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,0x10(%rsp,%rbp,8)
add    $0x1,%rbp
mov    0x8(%rsp),%rdi
cmpb   $0x0,(%rdi)
jne    1220 <main+0x90>
mov    %ebp,%edi
jmp    124a <main+0xba>
xor    %edi,%edi
lea    0x10(%rsp),%rsi
call   1060 <setgroups@plt>
test   %rax,%rax
je     126e <main+0xde>
mov    0x0(%r13),%rsi
lea    0xdce(%rip),%rdx
mov    $0xffffffff,%edi
call   1090 <error@plt>
mov    %r12,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1040 <setresgid@plt>
test   %rax,%rax
je     1296 <main+0x106>
mov    0x0(%r13),%rsi
lea    0xdb2(%rip),%rdx
mov    $0xffffffff,%edi
call   1090 <error@plt>
mov    %r14,%rdi
mov    %r14,%rsi
mov    %r14,%rdx
call   1030 <setresuid@plt>
test   %rax,%rax
je     12be <main+0x12e>
mov    0x0(%r13),%rsi
lea    0xd96(%rip),%rdx
mov    $0xffffffff,%edi
call   1090 <error@plt>
mov    0x10(%r15),%rdi
add    $0x10,%r15
mov    %r15,%rsi
call   10a0 <execvp@plt>
mov    0x0(%r13),%rsi
lea    0xd7d(%rip),%rdx
mov    $0xffffffff,%edi
call   1090 <error@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xcf3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"Usage: %s uid:gid[:gid1:gid2:...] COMMAND [ARGS]\0A\00"
@errno = global i64 0
@.str.1 = [12 x i8] c"setgroups()\00"
@.str.2 = [12 x i8] c"setresgid()\00"
@.str.3 = [12 x i8] c"setresuid()\00"
@.str.4 = [8 x i8] c"execv()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca [128 x i64]
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = getelementptr inbounds [128 x i64], [128 x i64]* %8, i64 0, i64 0
%22 = call i32 @memset(i64* %21, i32 0, i32 1024)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i64 @strtol(i8* %25, i8** %10, i32 10)
store i64 %26, i64* %6
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = call i64 @strtol(i8* %28, i8** %10, i32 10)
store i64 %29, i64* %7
%30 = load i64, i64* @errno
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %20
store i32 0, i32* %9
br label %40
40: ; preds = %45, %39
%41 = load i8*, i8** %10
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %53
45: ; preds = %40
%46 = load i8*, i8** %10
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = call i64 @strtol(i8* %47, i8** %10, i32 10)
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds [128 x i64], [128 x i64]* %8, i64 0, i64 %51
store i64 %48, i64* %52
br label %40
53: ; preds = %40
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [128 x i64], [128 x i64]* %8, i64 0, i64 0
%57 = call i64 @setgroups(i64 %55, i64* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i64, i64* @errno
%61 = call i32 @error(i32 -1, i64 %60, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %62
62: ; preds = %59, %53
%63 = load i64, i64* %7
%64 = load i64, i64* %7
%65 = load i64, i64* %7
%66 = call i64 @setresgid(i64 %63, i64 %64, i64 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load i64, i64* @errno
%70 = call i32 @error(i32 -1, i64 %69, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %68, %62
%72 = load i64, i64* %6
%73 = load i64, i64* %6
%74 = load i64, i64* %6
%75 = call i64 @setresuid(i64 %72, i64 %73, i64 %74)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %71
%78 = load i64, i64* @errno
%79 = call i32 @error(i32 -1, i64 %78, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %77, %71
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 2
%83 = load i8*, i8** %82
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 2
%86 = call i32 @execvp(i8* %83, i8** %85)
%87 = load i64, i64* @errno
%88 = call i32 @error(i32 -1, i64 %87, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
ret i32 -1
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"Usage: %s uid:gid[:gid1:gid2:...] COMMAND [ARGS]\0A\00"
@errno = global i64 0
@.str.1 = [12 x i8] c"setgroups()\00"
@.str.2 = [12 x i8] c"setresgid()\00"
@.str.3 = [12 x i8] c"setresuid()\00"
@.str.4 = [8 x i8] c"execv()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [128 x i64]
%4 = alloca i8*
%5 = bitcast [128 x i64]* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds [128 x i64], [128 x i64]* %3, i64 0, i64 0
%15 = call i32 @memset(i64* %14, i32 0, i32 1024)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strtol(i8* %17, i8** %4, i32 10)
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = call i64 @strtol(i8* %20, i8** %4, i32 10)
%22 = load i64, i64* @errno
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %45, label %33
28: ; preds = %13
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %1
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %24, %33
%34 = phi i64 [ %38, %33 ], [ 0, %24 ]
%35 = phi i8* [ %40, %33 ], [ %25, %24 ]
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = call i64 @strtol(i8* %36, i8** %4, i32 10)
%38 = add nuw i64 %34, 1
%39 = getelementptr inbounds [128 x i64], [128 x i64]* %3, i64 0, i64 %34
store i64 %37, i64* %39
%40 = load i8*, i8** %4
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 0
br i1 %42, label %43, label %33
43: ; preds = %33
%44 = and i64 %38, 4294967295
br label %45
45: ; preds = %43, %24
%46 = phi i64 [ 0, %24 ], [ %44, %43 ]
%47 = call i64 @setgroups(i64 %46, i64* %14)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = load i64, i64* @errno
%51 = call i32 @error(i32 -1, i64 %50, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %52
52: ; preds = %49, %45
%53 = call i64 @setresgid(i64 %21, i64 %21, i64 %21)
%54 = icmp eq i64 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %52
%56 = load i64, i64* @errno
%57 = call i32 @error(i32 -1, i64 %56, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %55, %52
%59 = call i64 @setresuid(i64 %18, i64 %18, i64 %18)
%60 = icmp eq i64 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %58
%62 = load i64, i64* @errno
%63 = call i32 @error(i32 -1, i64 %62, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %61, %58
%65 = getelementptr inbounds i8*, i8** %1, i64 2
%66 = load i8*, i8** %65
%67 = call i32 @execvp(i8* %66, i8** %65)
%68 = load i64, i64* @errno
%69 = call i32 @error(i32 -1, i64 %68, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
ret i32 -1
}