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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
struct group *getgrgid(int /*<<< orphan*/);
int getgroups(int, int /*<<< orphan*/ *);
struct passwd *getpwnam(char *);
int initgroups(char *, int);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int result;
  struct passwd *pwd;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s username\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  pwd = getpwnam(argv[1]);
  if (pwd == NULL) {
    perror("getpwnam");
    exit(EXIT_FAILURE);
  }
  printf("pwd name %s, uid %d, gid %d\n", pwd->pw_name, pwd->pw_uid,
         pwd->pw_gid);
  result = initgroups(pwd->pw_name, pwd->pw_gid);
  if (result == -1) {
    perror("initgroups");
    exit(EXIT_FAILURE);
  }
  printf("setgroups OK, username : %s\n", pwd->pw_name);
  int num = getgroups(0, NULL);
  gid_t *grps = malloc(sizeof(gid_t) * num);
  printf("group count = %d\n", num);
  num = getgroups(num, grps);
  if (num == -1) {
    perror("getgroups");
    exit(EXIT_FAILURE);
  }
  struct group *grp;
  for (int i = 0; i < num; ++i) {
    grp = getgrgid(grps[i]);
    printf("  [%d] %s\n", grp->gr_gid, grp->gr_name);
  }
  free(grps);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ea <main+0x4a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1220 <main+0x80>
lea    0xe07(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%ecx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
call   10c0 <initgroups@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    127d <main+0xdd>
lea    0xdd0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <getgroups@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1080 <getgroups@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12f7 <main+0x157>
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1348 <main+0x1a8>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <getgrgid@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12fe <main+0x15e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11c7 <main+0x27>
mov    0x8(%rsi),%rdi
call   1060 <getpwnam@plt>
test   %rax,%rax
jne    11e4 <main+0x44>
lea    0xe52(%rip),%rdi
jmp    126c <main+0xcc>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe26(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1271 <main+0xd1>
mov    %rax,%rbx
mov    (%rax),%rsi
mov    0x8(%rax),%edx
mov    0xc(%rax),%ecx
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rdi
mov    0xc(%rbx),%esi
call   10c0 <initgroups@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0x77>
lea    0xe25(%rip),%rdi
jmp    126c <main+0xcc>
mov    (%rbx),%rsi
lea    0xe24(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1080 <getgroups@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
lea    0xe17(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1080 <getgroups@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xdf>
lea    0xe08(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
test   %eax,%eax
jle    12b2 <main+0x112>
mov    %eax,%r12d
lea    0xdf1(%rip),%r14
xor    %ebx,%ebx
nop
mov    (%r15,%rbx,4),%edi
call   1050 <getgrgid@plt>
mov    (%rax),%esi
mov    0x8(%rax),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1290 <main+0xf0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.passwd = type { i8*, i32, i32 }
%struct.group = type { i32, i8* }
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s username\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"getpwnam\00"
@.str.2 = [29 x i8] c"pwd name %s, uid %d, gid %d\0A\00"
@.str.3 = [11 x i8] c"initgroups\00"
@.str.4 = [29 x i8] c"setgroups OK, username : %s\0A\00"
@.str.5 = [18 x i8] c"group count = %d\0A\00"
@.str.6 = [10 x i8] c"getgroups\00"
@.str.7 = [11 x i8] c" [%d] %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call %struct.passwd* @getpwnam(i8* %12)
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 2
%25 = load i32, i32* %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %23, i32 %25)
%27 = load i8*, i8** %20
%28 = load i32, i32* %24
%29 = tail call i32 @initgroups(i8* %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %19
%32 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %19
%36 = load i8*, i8** %20
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = tail call i32 @getgroups(i32 0, i32* null)
%39 = shl i32 %38, 2
%40 = tail call i32* @malloc(i32 %39)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%42 = tail call i32 @getgroups(i32 %38, i32* %40)
%43 = icmp eq i32 %42, -1
br i1 %43, label %48, label %44
44: ; preds = %35
%45 = icmp sgt i32 %42, 0
br i1 %45, label %46, label %52
46: ; preds = %44
%47 = zext i32 %42 to i64
br label %56
48: ; preds = %35
%49 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = tail call i32 @exit(i32 %50)
unreachable
52: ; preds = %56, %44
%53 = tail call i32 @free(i32* %40)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = tail call i32 @exit(i32 %54)
unreachable
56: ; preds = %46, %56
%57 = phi i64 [ 0, %46 ], [ %66, %56 ]
%58 = getelementptr inbounds i32, i32* %40, i64 %57
%59 = load i32, i32* %58
%60 = tail call %struct.group* @getgrgid(i32 %59)
%61 = getelementptr inbounds %struct.group, %struct.group* %60, i64 0, i32 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.group, %struct.group* %60, i64 0, i32 1
%64 = load i8*, i8** %63
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %62, i8* %64)
%66 = add nuw nsw i64 %57, 1
%67 = icmp eq i64 %66, %47
br i1 %67, label %52, label %56
}