#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 {
  int /*<<< orphan*/ pw_uid;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ chroot(char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
scalar_t__ initgroups(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void setup_environment(const char *root, char *user, char *group) {
  struct passwd *pw;
  struct group *gr;
  if (!user || !group) {
    fprintf(stderr, "use both of --user and --group\n");
    exit(1);
  }
  gr = getgrnam(group);
  if (!gr) {
    fprintf(stderr, "no such group: %s\n", group);
    exit(1);
  }
  if (setgid(gr->gr_gid) < 0) {
    perror("setgid(2)");
    exit(1);
  }
  if (initgroups(user, gr->gr_gid) < 0) {
    perror("initgroups(2)");
    exit(1);
  }
  pw = getpwnam(user);
  if (!pw) {
    fprintf(stderr, "no such user: %s\n", user);
    exit(1);
  }
  chroot(root);
  if (setuid(pw->pw_uid) < 0) {
    perror("setuid(2)");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<setup_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ba <setup_environment+0x2a>
cmpq   $0x0,-0x18(%rbp)
jne    11db <setup_environment+0x4b>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1218 <setup_environment+0x88>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jge    1243 <setup_environment+0xb3>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   10b0 <initgroups@plt>
cmp    $0x0,%rax
jge    1272 <setup_environment+0xe2>
lea    0xdda(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12af <setup_environment+0x11f>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1030 <chroot@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
jge    12e3 <setup_environment+0x153>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_environment>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1206 <setup_environment+0x76>
mov    %rdx,%r12
test   %rdx,%rdx
je     1206 <setup_environment+0x76>
mov    %rsi,%r15
mov    %rdi,%r14
mov    %r12,%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     1227 <setup_environment+0x97>
mov    %rax,%rbx
mov    (%rax),%edi
call   1070 <setgid@plt>
test   %rax,%rax
js     123c <setup_environment+0xac>
mov    (%rbx),%esi
mov    %r15,%rdi
call   10b0 <initgroups@plt>
test   %rax,%rax
js     1245 <setup_environment+0xb5>
mov    %r15,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     124e <setup_environment+0xbe>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <chroot@plt>
mov    (%rbx),%edi
call   10a0 <setuid@plt>
test   %rax,%rax
js     1272 <setup_environment+0xe2>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
mov    %r12,%rdx
jmp    1261 <setup_environment+0xd1>
lea    0xdf0(%rip),%rdi
jmp    1279 <setup_environment+0xe9>
lea    0xdf1(%rip),%rdi
jmp    1279 <setup_environment+0xe9>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xde4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"use both of --user and --group\0A\00"
@.str.1 = [19 x i8] c"no such group: %s\0A\00"
@.str.2 = [10 x i8] c"setgid(2)\00"
@.str.3 = [14 x i8] c"initgroups(2)\00"
@.str.4 = [18 x i8] c"no such user: %s\0A\00"
@.str.5 = [10 x i8] c"setuid(2)\00"
define void @setup_environment(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca %struct.group*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %18, label %14
14: ; preds = %11, %3
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i8*, i8** %6
%20 = call %struct.group* @getgrnam(i8* %19)
store %struct.group* %20, %struct.group** %8
%21 = load %struct.group*, %struct.group** %8
%22 = icmp ne %struct.group* %21, null
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %6
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load %struct.group*, %struct.group** %8
%30 = getelementptr inbounds %struct.group, %struct.group* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @setgid(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = load i8*, i8** %5
%39 = load %struct.group*, %struct.group** %8
%40 = getelementptr inbounds %struct.group, %struct.group* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i64 @initgroups(i8* %38, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
%48 = load i8*, i8** %5
%49 = call %struct.passwd* @getpwnam(i8* %48)
store %struct.passwd* %49, %struct.passwd** %7
%50 = load %struct.passwd*, %struct.passwd** %7
%51 = icmp ne %struct.passwd* %50, null
br i1 %51, label %57, label %52
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %5
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %47
%58 = load i8*, i8** %4
%59 = call i32 @chroot(i8* %58)
%60 = load %struct.passwd*, %struct.passwd** %7
%61 = getelementptr inbounds %struct.passwd, %struct.passwd* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i64 @setuid(i32 %62)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %57
ret void
}

LLVM IR (-O2):
%struct.group = type { i32 }
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"use both of --user and --group\0A\00"
@.str.1 = [19 x i8] c"no such group: %s\0A\00"
@.str.2 = [10 x i8] c"setgid(2)\00"
@.str.3 = [14 x i8] c"initgroups(2)\00"
@.str.4 = [18 x i8] c"no such user: %s\0A\00"
@.str.5 = [10 x i8] c"setuid(2)\00"
define void @setup_environment(i8* %0, i8* %1, i8* %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ne i8* %2, null
%6 = and i1 %4, %5
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call %struct.group* @getgrnam(i8* %2)
%13 = icmp eq %struct.group* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = getelementptr inbounds %struct.group, %struct.group* %12, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i64 @setgid(i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %19
%28 = tail call i64 @initgroups(i8* %1, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = tail call %struct.passwd* @getpwnam(i8* %1)
%35 = icmp eq %struct.passwd* %34, null
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = tail call i32 @chroot(i8* %0)
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i64 0, i32 0
%43 = load i32, i32* %42
%44 = tail call i64 @setuid(i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %40
ret void
}