#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 {
  char *gr_name;
};
typedef int gid_t;

/* Variables and functions */
struct group *getgrgid(int);
int getgroups(int, int *);
struct passwd *getpwnam(char *);
int initgroups(char *, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int result, ngroups, i;
  gid_t *groups;
  struct passwd *pw;
  if (!(pw = getpwnam(argv[1]))) {
    printf("FAIL: no passwd entry for %s\n", argv[1]);
    return 1;
  }
  result = initgroups(argv[1], pw->pw_gid);
  if (result == -1) {
    printf("FAIL");
    return 1;
  }
  ngroups = getgroups(0, NULL);
  groups = (gid_t *)malloc(sizeof(gid_t) * ngroups);
  ngroups = getgroups(ngroups, groups);
  printf("%s is a member of groups:\n", argv[1]);
  for (i = 0; i < ngroups; i++) {
    struct group *grp;
    grp = getgrgid(groups[i]);
    printf("%d (%s)\n", groups[i], grp ? grp->gr_name : "?");
  }
  printf("PASS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11b3 <main+0x53>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x16b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
call   1080 <initgroups@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ed <main+0x8d>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x16b>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <getgroups@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <getgroups@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b6 <main+0x156>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1283 <main+0x123>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1293 <main+0x133>
lea    0xdbd(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1293 <main+0x133>
mov    -0x3c(%rbp),%esi
mov    -0x48(%rbp),%rdx
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    123a <main+0xda>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
call   1060 <getpwnam@plt>
mov    0x8(%r15),%rcx
test   %rax,%rax
je     1239 <main+0xc9>
mov    (%rax),%esi
mov    %rcx,%rdi
call   1090 <initgroups@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xdc>
xor    %r14d,%r14d
xor    %edi,%edi
xor    %esi,%esi
call   1070 <getgroups@plt>
mov    %eax,%ebp
lea    0x0(,%rbp,4),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
call   1070 <getgroups@plt>
mov    %eax,%ebp
mov    0x8(%r15),%rsi
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    122b <main+0xbb>
mov    %ebp,%r13d
lea    0xe57(%rip),%r12
lea    0xe47(%rip),%r15
xor    %ebp,%ebp
jmp    1216 <main+0xa6>
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     122b <main+0xbb>
mov    (%rbx,%rbp,4),%edi
call   1050 <getgrgid@plt>
mov    %r12,%rdx
test   %rax,%rax
je     1200 <main+0x90>
mov    (%rax),%rdx
jmp    1200 <main+0x90>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
jmp    1260 <main+0xf0>
lea    0xdc0(%rip),%rdi
mov    %rcx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    125a <main+0xea>
lea    0xdcb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i8* }
@.str = [30 x i8] c"FAIL: no passwd entry for %s\0A\00"
@.str.1 = [5 x i8] c"FAIL\00"
@.str.2 = [27 x i8] c"%s is a member of groups:\0A\00"
@.str.3 = [9 x i8] c"%d (%s)\0A\00"
@.str.4 = [2 x i8] c"?\00"
@.str.5 = [6 x i8] c"PASS\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.passwd*
%11 = alloca %struct.group*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call %struct.passwd* @getpwnam(i8* %14)
store %struct.passwd* %15, %struct.passwd** %10
%16 = icmp ne %struct.passwd* %15, null
br i1 %16, label %22, label %17
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %80
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load %struct.passwd*, %struct.passwd** %10
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @initgroups(i8* %25, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %80
34: ; preds = %22
%35 = call i32 @getgroups(i32 0, i32* null)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = mul i64 4, %37
%39 = trunc i64 %38 to i32
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i32*
store i32* %41, i32** %9
%42 = load i32, i32* %7
%43 = load i32*, i32** %9
%44 = call i32 @getgroups(i32 %42, i32* %43)
store i32 %44, i32* %7
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %47)
store i32 0, i32* %8
br label %49
49: ; preds = %75, %34
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %78
53: ; preds = %49
%54 = load i32*, i32** %9
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = call %struct.group* @getgrgid(i32 %58)
store %struct.group* %59, %struct.group** %11
%60 = load i32*, i32** %9
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = load %struct.group*, %struct.group** %11
%66 = icmp ne %struct.group* %65, null
br i1 %66, label %67, label %71
67: ; preds = %53
%68 = load %struct.group*, %struct.group** %11
%69 = getelementptr inbounds %struct.group, %struct.group* %68, i32 0, i32 0
%70 = load i8*, i8** %69
br label %72
71: ; preds = %53
br label %72
72: ; preds = %71, %67
%73 = phi i8* [ %70, %67 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %71 ]
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %64, i8* %73)
br label %75
75: ; preds = %72
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
br label %49
78: ; preds = %49
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %80
80: ; preds = %78, %32, %17
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i8* }
@.str = [30 x i8] c"FAIL: no passwd entry for %s\0A\00"
@.str.1 = [5 x i8] c"FAIL\00"
@.str.2 = [27 x i8] c"%s is a member of groups:\0A\00"
@.str.3 = [9 x i8] c"%d (%s)\0A\00"
@.str.4 = [2 x i8] c"?\00"
@str = [5 x i8] c"PASS\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call %struct.passwd* @getpwnam(i8* %4)
%6 = icmp eq %struct.passwd* %5, null
%7 = load i8*, i8** %3
br i1 %6, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %45
10: ; preds = %2
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @initgroups(i8* %7, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
17: ; preds = %10
%18 = tail call i32 @getgroups(i32 0, i32* null)
%19 = shl i32 %18, 2
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
%22 = tail call i32 @getgroups(i32 %18, i32* %21)
%23 = load i8*, i8** %3
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = icmp sgt i32 %22, 0
br i1 %25, label %26, label %43
26: ; preds = %17
%27 = zext i32 %22 to i64
br label %28
28: ; preds = %26, %38
%29 = phi i64 [ 0, %26 ], [ %41, %38 ]
%30 = getelementptr inbounds i32, i32* %21, i64 %29
%31 = load i32, i32* %30
%32 = tail call %struct.group* @getgrgid(i32 %31)
%33 = load i32, i32* %30
%34 = icmp eq %struct.group* %32, null
br i1 %34, label %38, label %35
35: ; preds = %28
%36 = getelementptr inbounds %struct.group, %struct.group* %32, i64 0, i32 0
%37 = load i8*, i8** %36
br label %38
38: ; preds = %28, %35
%39 = phi i8* [ %37, %35 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %28 ]
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %33, i8* %39)
%41 = add nuw nsw i64 %29, 1
%42 = icmp eq i64 %41, %27
br i1 %42, label %43, label %28
43: ; preds = %38, %17
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %43, %15, %8
%46 = phi i32 [ 1, %15 ], [ 0, %43 ], [ 1, %8 ]
ret i32 %46
}