#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 dummy;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ NGROUPS_MAX;
int getgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ *malloc(int);
int setgroups(int, int /*<<< orphan*/ *);
int my_initgroups(const char *user, gid_t group) {
  struct passwd *pwd;
  gid_t *grouplist, read_grouplist[NGROUPS_MAX + 1];
  int num_groups = 0, i = 0;
  pwd = getpwnam(user);
  if (pwd != NULL) {
    num_groups = getgroups(NGROUPS_MAX, read_grouplist);
    grouplist = malloc((num_groups + 1) * sizeof(gid_t));
    if (grouplist != NULL) {
      for (; i < num_groups; i++) {
        grouplist[i] = read_grouplist[i];
      }
      grouplist[i] = group;
      if (setgroups(num_groups + 1, grouplist) != -1) {
        return 0;
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<my_initgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1254 <my_initgroups+0x114>
mov    -0x50(%rbp),%rsi
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1050 <getgroups@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     124f <my_initgroups+0x10f>
jmp    11e1 <my_initgroups+0xa1>
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1211 <my_initgroups+0xd1>
mov    -0x50(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11e1 <my_initgroups+0xa1>
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x3c(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%rsi
call   1030 <setgroups@plt>
cmp    $0xffffffff,%eax
je     124a <my_initgroups+0x10a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1262 <my_initgroups+0x122>
jmp    124f <my_initgroups+0x10f>
jmp    1254 <my_initgroups+0x114>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_initgroups>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    0x2e80(%rip),%r14
mov    (%r14),%eax
add    $0x1,%eax
mov    %rsp,%rbx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11f2 <my_initgroups+0xa2>
mov    (%r14),%edi
mov    %rbx,%rsi
call   1060 <getgroups@plt>
mov    %eax,%r14d
lea    0x4(,%r14,4),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f2 <my_initgroups+0xa2>
mov    %rax,%r12
mov    %r15d,-0x2c(%rbp)
lea    0x1(%r14),%r13d
xor    %r15d,%r15d
test   %r14d,%r14d
jle    11d8 <my_initgroups+0x88>
mov    %r14d,%r14d
lea    0x0(,%r14,4),%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
jmp    11db <my_initgroups+0x8b>
xor    %r14d,%r14d
mov    -0x2c(%rbp),%eax
mov    %eax,(%r12,%r14,4)
mov    %r13d,%edi
mov    %r12,%rsi
call   1030 <setgroups@plt>
cmp    $0xffffffff,%eax
jne    11f8 <my_initgroups+0xa8>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@NGROUPS_MAX = global i32 0
define i32 @my_initgroups(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.passwd*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i32, i32* @NGROUPS_MAX
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i32, i64 %15
store i64 %15, i64* %9
store i32 0, i32* %10
store i32 0, i32* %11
%18 = load i8*, i8** %4
%19 = call %struct.passwd* @getpwnam(i8* %18)
store %struct.passwd* %19, %struct.passwd** %6
%20 = load %struct.passwd*, %struct.passwd** %6
%21 = icmp ne %struct.passwd* %20, null
br i1 %21, label %22, label %64
22: ; preds = %2
%23 = load i32, i32* @NGROUPS_MAX
%24 = call i32 @getgroups(i32 %23, i32* %17)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 4
%29 = trunc i64 %28 to i32
%30 = call i32* @malloc(i32 %29)
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %63
33: ; preds = %22
br label %34
34: ; preds = %47, %33
%35 = load i32, i32* %11
%36 = load i32, i32* %10
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %50
38: ; preds = %34
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %17, i64 %40
%42 = load i32, i32* %41
%43 = load i32*, i32** %7
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
store i32 %42, i32* %46
br label %47
47: ; preds = %38
%48 = load i32, i32* %11
%49 = add nsw i32 %48, 1
store i32 %49, i32* %11
br label %34
50: ; preds = %34
%51 = load i32, i32* %5
%52 = load i32*, i32** %7
%53 = load i32, i32* %11
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
store i32 %51, i32* %55
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
%58 = load i32*, i32** %7
%59 = call i32 @setgroups(i32 %57, i32* %58)
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %50
store i32 0, i32* %3
store i32 1, i32* %12
br label %65
62: ; preds = %50
br label %63
63: ; preds = %62, %22
br label %64
64: ; preds = %63, %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %65
65: ; preds = %64, %61
%66 = load i8*, i8** %8
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@NGROUPS_MAX = global i32 0
define i32 @my_initgroups(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @NGROUPS_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i32, i64 %5
%7 = bitcast i32* %6 to i8*
%8 = tail call %struct.passwd* @getpwnam(i8* %0)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %29, label %10
10: ; preds = %2
%11 = load i32, i32* @NGROUPS_MAX
%12 = call i32 @getgroups(i32 %11, i32* %6)
%13 = add nsw i32 %12, 1
%14 = shl i32 %13, 2
%15 = call i32* @malloc(i32 %14)
%16 = bitcast i32* %15 to i8*
%17 = icmp eq i32* %15, null
br i1 %17, label %29, label %18
18: ; preds = %10
%19 = icmp sgt i32 %12, 0
br i1 %19, label %20, label %24
20: ; preds = %18
%21 = zext i32 %12 to i64
%22 = shl nuw nsw i64 %21, 2
%23 = zext i32 %12 to i64
br label %24
24: ; preds = %20, %18
%25 = phi i64 [ 0, %18 ], [ %23, %20 ]
%26 = getelementptr inbounds i32, i32* %15, i64 %25
store i32 %1, i32* %26
%27 = call i32 @setgroups(i32 %13, i32* %15)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %10, %24, %2
br label %30
30: ; preds = %24, %29
%31 = phi i32 [ -1, %29 ], [ 0, %24 ]
ret i32 %31
}