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

/* Variables and functions */
struct group *getgrgid(int /*<<< orphan*/);
int getgrouplist(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, int *);
struct passwd *getpwuid(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int print_group_list(uid_t uid) {
  int ret, cnt, i, nrgroups = 10;
  gid_t *group_list = NULL;
  struct passwd *pwd = NULL;
  group_list = (gid_t *)malloc(nrgroups * sizeof(gid_t));
  if (group_list == NULL) {
    perror("malloc:");
    return -1;
  }
  pwd = getpwuid(uid);
  if (pwd == NULL) {
    perror("getpwuid:");
    return -1;
  }
  printf("%s :", pwd->pw_name);
  ret = getgrouplist(pwd->pw_name, pwd->pw_gid, group_list, &nrgroups);
  if (ret == -1) {
    perror("getgrouplist:");
    return -1;
  }
  for (i = 0; i < nrgroups; i++) {
    struct group *grp = NULL;
    grp = getgrgid(group_list[i]);
    if (grp != NULL) {
      printf(" %s", grp->gr_name);
    } else {
      perror("getgrgid:");
      return -1;
    }
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<print_group_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xa,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <print_group_list+0x58>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <print_group_list+0x169>
mov    -0x8(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <print_group_list+0x87>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <print_group_list+0x169>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x20(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1070 <getgrouplist@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    123c <print_group_list+0xdc>
lea    0xdec(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <print_group_list+0x169>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12bb <print_group_list+0x15b>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <getgrgid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1290 <print_group_list+0x130>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12a8 <print_group_list+0x148>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <print_group_list+0x169>
jmp    12ad <print_group_list+0x14d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1243 <print_group_list+0xe3>
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_group_list>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movl   $0xa,0xc(%rsp)
mov    $0x28,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1230 <print_group_list+0xc0>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     123e <print_group_list+0xce>
mov    %rax,%r14
mov    (%rax),%rsi
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%rdi
mov    0x8(%r14),%esi
lea    0xc(%rsp),%rcx
mov    %rbx,%rdx
call   1080 <getgrouplist@plt>
cmp    $0xffffffff,%eax
je     124c <print_group_list+0xdc>
cmpl   $0x0,0xc(%rsp)
jle    1218 <print_group_list+0xa8>
lea    0xe42(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%rbp,4),%edi
call   1060 <getgrgid@plt>
test   %rax,%rax
je     125a <print_group_list+0xea>
mov    (%rax),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
movslq 0xc(%rsp),%rax
cmp    %rax,%rbp
jl     11f0 <print_group_list+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
jmp    1222 <print_group_list+0xb2>
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
jmp    1222 <print_group_list+0xb2>
lea    0xdc4(%rip),%rdi
call   1090 <perror@plt>
jmp    1222 <print_group_list+0xb2>
lea    0xdc8(%rip),%rdi
call   1090 <perror@plt>
jmp    1222 <print_group_list+0xb2>

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

LLVM IR (-O2):
%struct.passwd = type { i8*, i32 }
%struct.group = type { i8* }
@.str = [8 x i8] c"malloc:\00"
@.str.1 = [10 x i8] c"getpwuid:\00"
@.str.2 = [5 x i8] c"%s :\00"
@.str.3 = [14 x i8] c"getgrouplist:\00"
@.str.4 = [4 x i8] c" %s\00"
@.str.5 = [10 x i8] c"getgrgid:\00"
define i32 @print_group_list(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 10, i32* %2
%4 = tail call i64 @malloc(i32 40)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %46
9: ; preds = %1
%10 = tail call %struct.passwd* @getpwuid(i32 %0)
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %46
14: ; preds = %9
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %15
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @getgrouplist(i8* %18, i32 %20, i32* %5, i32* %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %14
%24 = load i32, i32* %2
%25 = icmp sgt i32 %24, 0
br i1 %25, label %28, label %44
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %46
28: ; preds = %23, %36
%29 = phi i64 [ %40, %36 ], [ 0, %23 ]
%30 = getelementptr inbounds i32, i32* %5, i64 %29
%31 = load i32, i32* %30
%32 = call %struct.group* @getgrgid(i32 %31)
%33 = icmp eq %struct.group* %32, null
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %46
36: ; preds = %28
%37 = getelementptr inbounds %struct.group, %struct.group* %32, i64 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = add nuw nsw i64 %29, 1
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %40, %42
br i1 %43, label %28, label %44
44: ; preds = %36, %23
%45 = call i32 @putchar(i32 10)
br label %46
46: ; preds = %34, %44, %26, %12, %7
ret i32 -1
}