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

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
struct group *getgrgid(int /*<<< orphan*/);
int getgrouplist(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, int *);
struct passwd *getpwnam(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int user_is_member_of_group(char *user, char *group) {
  struct passwd *pw = NULL;
  struct group *grp = NULL;
  gid_t *groups = NULL;
  int i, ngroups = 1000;
  groups = (gid_t *)malloc(sizeof(gid_t) * ngroups);
  if (groups == NULL) {
    perror("malloc error");
    return -1;
  }
  pw = getpwnam(user);
  if (pw == NULL) {
    perror("Could not get passwd from given name");
    free(groups);
    return -1;
  }
  int count = getgrouplist(user, pw->pw_gid, groups, &ngroups);
  if (count == -1) {
    perror("Error occured while getting groups.");
    return -1;
  }
  for (i = 0; i <= count; grp = getgrgid(groups[i++]))
    if (grp != NULL && strcmp(grp->gr_name, group) == 0) {
      free(groups);
      return 1;
    }
  free(groups);
  return 0;
}


x86 assembly (clang, -O0):
<user_is_member_of_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x3e8,-0x38(%rbp)
movslq -0x38(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d5 <user_is_member_of_group+0x65>
lea    0xe3c(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <user_is_member_of_group+0x15e>
mov    -0x10(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120e <user_is_member_of_group+0x9e>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <user_is_member_of_group+0x15e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <getgrouplist@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    124a <user_is_member_of_group+0xda>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <user_is_member_of_group+0x15e>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jg     12be <user_is_member_of_group+0x14e>
cmpq   $0x0,-0x28(%rbp)
je     1296 <user_is_member_of_group+0x126>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1296 <user_is_member_of_group+0x126>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ce <user_is_member_of_group+0x15e>
jmp    129b <user_is_member_of_group+0x12b>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x28(%rbp)
jmp    1251 <user_is_member_of_group+0xe1>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user_is_member_of_group>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x3e8,0x4(%rsp)
mov    $0xfa0,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122b <user_is_member_of_group+0xbb>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <getpwnam@plt>
test   %rax,%rax
je     1234 <user_is_member_of_group+0xc4>
mov    (%rax),%esi
lea    0x4(%rsp),%rcx
mov    %rbx,%rdi
mov    %r15,%rdx
call   1080 <getgrouplist@plt>
cmp    $0xffffffff,%eax
je     124f <user_is_member_of_group+0xdf>
mov    %eax,%r12d
test   %eax,%eax
js     1215 <user_is_member_of_group+0xa5>
add    $0x1,%r12d
xor    %ebx,%ebx
xor    %eax,%eax
jmp    11f2 <user_is_member_of_group+0x82>
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%edi
add    $0x1,%rbx
call   1040 <getgrgid@plt>
cmp    %rbx,%r12
je     1215 <user_is_member_of_group+0xa5>
test   %rax,%rax
je     11e0 <user_is_member_of_group+0x70>
mov    (%rax),%edi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11e0 <user_is_member_of_group+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    121f <user_is_member_of_group+0xaf>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdce(%rip),%rdi
jmp    1256 <user_is_member_of_group+0xe6>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    121f <user_is_member_of_group+0xaf>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    121f <user_is_member_of_group+0xaf>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [13 x i8] c"malloc error\00"
@.str.1 = [37 x i8] c"Could not get passwd from given name\00"
@.str.2 = [36 x i8] c"Error occured while getting groups.\00"
define i32 @user_is_member_of_group(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.passwd*
%7 = alloca %struct.group*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.passwd* null, %struct.passwd** %6
store %struct.group* null, %struct.group** %7
store i32* null, i32** %8
store i32 1000, i32* %10
%12 = load i32, i32* %10
%13 = sext i32 %12 to i64
%14 = mul i64 4, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %8
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
22: ; preds = %2
%23 = load i8*, i8** %4
%24 = call %struct.passwd* @getpwnam(i8* %23)
store %struct.passwd* %24, %struct.passwd** %6
%25 = load %struct.passwd*, %struct.passwd** %6
%26 = icmp eq %struct.passwd* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32*, i32** %8
%30 = call i32 @free(i32* %29)
store i32 -1, i32* %3
br label %72
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = load %struct.passwd*, %struct.passwd** %6
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %8
%37 = call i32 @getgrouplist(i8* %32, i32 %35, i32* %36, i32* %10)
store i32 %37, i32* %11
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
42: ; preds = %31
store i32 0, i32* %9
br label %43
43: ; preds = %61, %42
%44 = load i32, i32* %9
%45 = load i32, i32* %11
%46 = icmp sle i32 %44, %45
br i1 %46, label %47, label %69
47: ; preds = %43
%48 = load %struct.group*, %struct.group** %7
%49 = icmp ne %struct.group* %48, null
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load %struct.group*, %struct.group** %7
%52 = getelementptr inbounds %struct.group, %struct.group* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i8*, i8** %5
%55 = call i64 @strcmp(i32 %53, i8* %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = load i32*, i32** %8
%59 = call i32 @free(i32* %58)
store i32 1, i32* %3
br label %72
60: ; preds = %50, %47
br label %61
61: ; preds = %60
%62 = load i32*, i32** %8
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i32, i32* %62, i64 %65
%67 = load i32, i32* %66
%68 = call %struct.group* @getgrgid(i32 %67)
store %struct.group* %68, %struct.group** %7
br label %43
69: ; preds = %43
%70 = load i32*, i32** %8
%71 = call i32 @free(i32* %70)
store i32 0, i32* %3
br label %72
72: ; preds = %69, %57, %40, %27, %20
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [13 x i8] c"malloc error\00"
@.str.1 = [37 x i8] c"Could not get passwd from given name\00"
@.str.2 = [36 x i8] c"Error occured while getting groups.\00"
define i32 @user_is_member_of_group(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 1000, i32* %3
%5 = tail call i64 @malloc(i32 4000)
%6 = inttoptr i64 %5 to i32*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %47
10: ; preds = %2
%11 = tail call %struct.passwd* @getpwnam(i8* %0)
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @free(i32* %6)
br label %47
16: ; preds = %10
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @getgrouplist(i8* %0, i32 %18, i32* %6, i32* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = icmp slt i32 %19, 0
br i1 %22, label %45, label %23
23: ; preds = %21
%24 = add nuw i32 %19, 1
%25 = zext i32 %24 to i64
br label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %47
28: ; preds = %23, %39
%29 = phi i64 [ 0, %23 ], [ %40, %39 ]
%30 = phi %struct.group* [ null, %23 ], [ %43, %39 ]
%31 = icmp eq %struct.group* %30, null
br i1 %31, label %39, label %32
32: ; preds = %28
%33 = getelementptr inbounds %struct.group, %struct.group* %30, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i64 @strcmp(i32 %34, i8* %1)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @free(i32* %6)
br label %47
39: ; preds = %28, %32
%40 = add nuw nsw i64 %29, 1
%41 = getelementptr inbounds i32, i32* %6, i64 %29
%42 = load i32, i32* %41
%43 = call %struct.group* @getgrgid(i32 %42)
%44 = icmp eq i64 %40, %25
br i1 %44, label %45, label %28
45: ; preds = %39, %21
%46 = call i32 @free(i32* %6)
br label %47
47: ; preds = %26, %37, %45, %13, %8
%48 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %26 ], [ 1, %37 ], [ 0, %45 ]
ret i32 %48
}