#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 uid_t;
struct passwd {
  int pw_gid;
  int pw_uid;
};
struct group {
  int gr_gid;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct group *getgrnam(char const *);
struct passwd *getpwnam(char const *);
scalar_t__ initgroups(char const *, int);
scalar_t__ setgid(int);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(int);
int /*<<< orphan*/ stderr;
int strtoul(char const *, char **, int /*<<< orphan*/);
void change_user(const char *username, const char *groupname) {
  char *end;
  uid_t uid = -1;
  gid_t gid = -1;
  int aux_groups_set = 0;
  if (username) {
    uid = strtoul(username, &end, 0);
    if (*end != '\0') {
      struct passwd *pwd;
      pwd = getpwnam(username);
      if (pwd == NULL) {
        fprintf(stderr, "%s: no such user\n", username);
        exit(1);
      }
      if (initgroups(username, pwd->pw_gid) < 0) {
        fprintf(stderr, "initgroups(%s): %m\n", username);
        exit(1);
      }
      aux_groups_set = 1;
      gid = pwd->pw_gid;
      uid = pwd->pw_uid;
    }
  }
  if (groupname) {
    gid = strtoul(groupname, &end, 0);
    if (*end != '\0') {
      struct group *grp;
      grp = getgrnam(groupname);
      if (grp == NULL) {
        fprintf(stderr, "%s: no such group\n", groupname);
        exit(1);
      }
      gid = grp->gr_gid;
    }
  }
  if (!aux_groups_set && setgroups(0, NULL) < 0) {
    fprintf(stderr, "setgroups(0, NULL): %m\n");
    exit(1);
  }
  if (gid != -1 && setgid(gid) < 0) {
    fprintf(stderr, "setgid(%u): %m\n", gid);
    exit(1);
  }
  if (uid != -1 && setuid(uid) < 0) {
    fprintf(stderr, "setuid(%u): %m\n", uid);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<change_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     127c <change_user+0xec>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1277 <change_user+0xe7>
mov    -0x8(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121f <change_user+0x8f>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
call   10b0 <initgroups@plt>
cmp    $0x0,%rax
jge    125d <change_user+0xcd>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    127c <change_user+0xec>
cmpq   $0x0,-0x10(%rbp)
je     12f4 <change_user+0x164>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ef <change_user+0x15f>
mov    -0x10(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e6 <change_user+0x156>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    12f4 <change_user+0x164>
cmpl   $0x0,-0x24(%rbp)
jne    1334 <change_user+0x1a4>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setgroups@plt>
cmp    $0x0,%rax
jge    1334 <change_user+0x1a4>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x20(%rbp)
je     1374 <change_user+0x1e4>
mov    -0x20(%rbp),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jge    1374 <change_user+0x1e4>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xcee(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
je     13b4 <change_user+0x224>
mov    -0x1c(%rbp),%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
jge    13b4 <change_user+0x224>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xcbe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_user>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    $0xffffffff,%r12d
mov    $0x1,%bpl
test   %rdi,%rdi
je     1201 <change_user+0x71>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11fa <change_user+0x6a>
mov    %rbx,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     12dc <change_user+0x14c>
mov    %rax,%rbp
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <initgroups@plt>
test   %rax,%rax
js     12ee <change_user+0x15e>
mov    0x0(%rbp),%r12d
mov    0x4(%rbp),%r14d
xor    %ebp,%ebp
test   %r15,%r15
jne    120c <change_user+0x7c>
jmp    123c <change_user+0xac>
mov    $0xffffffff,%r14d
test   %r15,%r15
je     123c <change_user+0xac>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     123c <change_user+0xac>
mov    %r15,%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     1303 <change_user+0x173>
mov    (%rax),%r12d
test   %bpl,%bpl
je     124f <change_user+0xbf>
xor    %edi,%edi
xor    %esi,%esi
call   1030 <setgroups@plt>
test   %rax,%rax
js     12bb <change_user+0x12b>
cmp    $0xffffffff,%r12d
je     1262 <change_user+0xd2>
mov    %r12d,%edi
call   1070 <setgid@plt>
test   %rax,%rax
js     1282 <change_user+0xf2>
cmp    $0xffffffff,%r14d
je     1275 <change_user+0xe5>
mov    %r14d,%edi
call   10a0 <setuid@plt>
test   %rax,%rax
js     1297 <change_user+0x107>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    %r12d,%edx
jmp    12aa <change_user+0x11a>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    0xd14(%rip),%rsi
jmp    12fe <change_user+0x16e>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd14(%rip),%rsi
mov    %rbx,%rdx
jmp    1316 <change_user+0x186>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: no such user\0A\00"
@.str.1 = [20 x i8] c"initgroups(%s): %m\0A\00"
@.str.2 = [19 x i8] c"%s: no such group\0A\00"
@.str.3 = [24 x i8] c"setgroups(0, NULL): %m\0A\00"
@.str.4 = [16 x i8] c"setgid(%u): %m\0A\00"
@.str.5 = [16 x i8] c"setuid(%u): %m\0A\00"
define void @change_user(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.passwd*
%10 = alloca %struct.group*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %50
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 @strtoul(i8* %14, i8** %5, i32 0)
store i32 %15, i32* %6
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %49
20: ; preds = %13
%21 = load i8*, i8** %3
%22 = call %struct.passwd* @getpwnam(i8* %21)
store %struct.passwd* %22, %struct.passwd** %9
%23 = load %struct.passwd*, %struct.passwd** %9
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %3
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = load %struct.passwd*, %struct.passwd** %9
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @initgroups(i8* %31, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %3
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %30
store i32 1, i32* %8
%43 = load %struct.passwd*, %struct.passwd** %9
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %43, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %7
%46 = load %struct.passwd*, %struct.passwd** %9
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %46, i32 0, i32 1
%48 = load i32, i32* %47
store i32 %48, i32* %6
br label %49
49: ; preds = %42, %13
br label %50
50: ; preds = %49, %2
%51 = load i8*, i8** %4
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %75
53: ; preds = %50
%54 = load i8*, i8** %4
%55 = call i32 @strtoul(i8* %54, i8** %5, i32 0)
store i32 %55, i32* %7
%56 = load i8*, i8** %5
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %74
60: ; preds = %53
%61 = load i8*, i8** %4
%62 = call %struct.group* @getgrnam(i8* %61)
store %struct.group* %62, %struct.group** %10
%63 = load %struct.group*, %struct.group** %10
%64 = icmp eq %struct.group* %63, null
br i1 %64, label %65, label %70
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = load i8*, i8** %4
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %60
%71 = load %struct.group*, %struct.group** %10
%72 = getelementptr inbounds %struct.group, %struct.group* %71, i32 0, i32 0
%73 = load i32, i32* %72
store i32 %73, i32* %7
br label %74
74: ; preds = %70, %53
br label %75
75: ; preds = %74, %50
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %85, label %78
78: ; preds = %75
%79 = call i64 @setgroups(i32 0, i32* null)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %85
81: ; preds = %78
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %78, %75
%86 = load i32, i32* %7
%87 = icmp ne i32 %86, -1
br i1 %87, label %88, label %97
88: ; preds = %85
%89 = load i32, i32* %7
%90 = call i64 @setgid(i32 %89)
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %97
92: ; preds = %88
%93 = load i32, i32* @stderr
%94 = load i32, i32* %7
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %94)
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %88, %85
%98 = load i32, i32* %6
%99 = icmp ne i32 %98, -1
br i1 %99, label %100, label %109
100: ; preds = %97
%101 = load i32, i32* %6
%102 = call i64 @setuid(i32 %101)
%103 = icmp slt i64 %102, 0
br i1 %103, label %104, label %109
104: ; preds = %100
%105 = load i32, i32* @stderr
%106 = load i32, i32* %6
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %105, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %106)
%108 = call i32 @exit(i32 1)
unreachable
109: ; preds = %100, %97
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
%struct.group = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"%s: no such user\0A\00"
@.str.1 = [20 x i8] c"initgroups(%s): %m\0A\00"
@.str.2 = [19 x i8] c"%s: no such group\0A\00"
@.str.3 = [24 x i8] c"setgroups(0, NULL): %m\0A\00"
@.str.4 = [16 x i8] c"setgid(%u): %m\0A\00"
@.str.5 = [16 x i8] c"setuid(%u): %m\0A\00"
define void @change_user(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %31, label %6
6: ; preds = %2
%7 = call i32 @strtoul(i8* %0, i8** %3, i32 0)
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = tail call %struct.passwd* @getpwnam(i8* %0)
%13 = icmp eq %struct.passwd* %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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i64 @initgroups(i8* %0, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i32, i32* %19
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 1
%30 = load i32, i32* %29
br label %31
31: ; preds = %6, %27, %2
%32 = phi i1 [ true, %6 ], [ false, %27 ], [ true, %2 ]
%33 = phi i32 [ -1, %6 ], [ %28, %27 ], [ -1, %2 ]
%34 = phi i32 [ %7, %6 ], [ %30, %27 ], [ -1, %2 ]
%35 = icmp eq i8* %1, null
br i1 %35, label %51, label %36
36: ; preds = %31
%37 = call i32 @strtoul(i8* %1, i8** %3, i32 0)
%38 = load i8*, i8** %3
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 0
br i1 %40, label %51, label %41
41: ; preds = %36
%42 = tail call %struct.group* @getgrnam(i8* %1)
%43 = icmp eq %struct.group* %42, null
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = getelementptr inbounds %struct.group, %struct.group* %42, i64 0, i32 0
%50 = load i32, i32* %49
br label %51
51: ; preds = %36, %48, %31
%52 = phi i32 [ %50, %48 ], [ %37, %36 ], [ %33, %31 ]
br i1 %32, label %53, label %60
53: ; preds = %51
%54 = tail call i64 @setgroups(i32 0, i32* null)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%59 = tail call i32 @exit(i32 1)
unreachable
60: ; preds = %53, %51
%61 = icmp eq i32 %52, -1
br i1 %61, label %69, label %62
62: ; preds = %60
%63 = tail call i64 @setgid(i32 %52)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %62
%66 = load i32, i32* @stderr
%67 = tail call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%68 = tail call i32 @exit(i32 1)
unreachable
69: ; preds = %62, %60
%70 = icmp eq i32 %34, -1
br i1 %70, label %78, label %71
71: ; preds = %69
%72 = tail call i64 @setuid(i32 %34)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %71
%75 = load i32, i32* @stderr
%76 = tail call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%77 = tail call i32 @exit(i32 1)
unreachable
78: ; preds = %71, %69
ret void
}