#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 group {
  char *gr_passwd;
  int /*<<< orphan*/ gr_mem;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int cm_join_members(int /*<<< orphan*/, char *);
int cm_replace_entry(char *, char *, char *, char *, char *);
int errno;
int /*<<< orphan*/ free(char *);
struct group *getgrnam(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, int, char *);
int cm_group_set_gid(char *group, int gid) {
  int i;
  static char buf[512], *membuf;
  struct group *g = getgrnam(group);
  membuf = malloc(512);
  errno = 0;
  if (g == NULL) {
    return -errno;
  }
  if ((i = cm_join_members(g->gr_mem, membuf)) != 0) {
    return i;
  }
  snprintf(buf, sizeof(buf), "%s:%s:%d:%s\n", group, g->gr_passwd, gid, membuf);
  if ((i = cm_replace_entry(group, "%[^:]:%*[^:]:%*d:%*[^:]", buf, "/etc/group",
                            "/etc/.group.cm")) != 0) {
    return i;
  }
  free(membuf);
  return 0;
}


x86 assembly (clang, -O0):
<cm_group_set_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,0x30d3(%rip)
mov    0x2e4c(%rip),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <cm_group_set_gid+0x58>
xor    %eax,%eax
mov    0x2e32(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1263 <cm_group_set_gid+0x103>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    0x309a(%rip),%rsi
call   1070 <cm_join_members@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     11e2 <cm_group_set_gid+0x82>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1263 <cm_group_set_gid+0x103>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8
mov    -0x14(%rbp),%r9d
mov    0x3068(%rip),%rax
lea    0x2e61(%rip),%rdi
mov    $0x200,%esi
lea    0xdf5(%rip),%rdx
mov    %rax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xdee(%rip),%rsi
lea    0x2e3a(%rip),%rdx
lea    0xdf8(%rip),%rcx
lea    0xdfc(%rip),%r8
call   1040 <cm_replace_entry@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1250 <cm_group_set_gid+0xf0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1263 <cm_group_set_gid+0x103>
mov    0x3009(%rip),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_group_set_gid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1060 <getgrnam@plt>
mov    %rax,%rbp
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rsi
mov    %rax,0x30d6(%rip)
mov    0x2e4f(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
test   %rbp,%rbp
je     120c <cm_group_set_gid+0xac>
mov    0x8(%rbp),%edi
call   1070 <cm_join_members@plt>
test   %eax,%eax
jne    120c <cm_group_set_gid+0xac>
mov    0x0(%rbp),%r8
mov    0x30ab(%rip),%rax
mov    %rax,(%rsp)
lea    0x2ea0(%rip),%rbp
lea    0xe39(%rip),%rdx
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rcx
mov    %r14d,%r9d
call   1050 <snprintf@plt>
lea    0xe2c(%rip),%rsi
lea    0xe3d(%rip),%rcx
lea    0xe41(%rip),%r8
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <cm_replace_entry@plt>
test   %eax,%eax
jne    120c <cm_group_set_gid+0xac>
mov    0x305b(%rip),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i8*, i32 }
@cm_group_set_gid.buf = internal global [512 x i8] zeroinitializer
@cm_group_set_gid.membuf = internal global i8* null
@errno = global i32 0
@.str = [13 x i8] c"%s:%s:%d:%s\0A\00"
@.str.1 = [24 x i8] c"%[^:]:%*[^:]:%*d:%*[^:]\00"
@.str.2 = [11 x i8] c"/etc/group\00"
@.str.3 = [15 x i8] c"/etc/.group.cm\00"
define i32 @cm_group_set_gid(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.group*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call %struct.group* @getgrnam(i8* %8)
store %struct.group* %9, %struct.group** %7
%10 = call i8* @malloc(i32 512)
store i8* %10, i8** @cm_group_set_gid.membuf
store i32 0, i32* @errno
%11 = load %struct.group*, %struct.group** %7
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %41
16: ; preds = %2
%17 = load %struct.group*, %struct.group** %7
%18 = getelementptr inbounds %struct.group, %struct.group* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** @cm_group_set_gid.membuf
%21 = call i32 @cm_join_members(i32 %19, i8* %20)
store i32 %21, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %41
25: ; preds = %16
%26 = load i8*, i8** %4
%27 = load %struct.group*, %struct.group** %7
%28 = getelementptr inbounds %struct.group, %struct.group* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = load i32, i32* %5
%31 = load i8*, i8** @cm_group_set_gid.membuf
%32 = call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cm_group_set_gid.buf, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %29, i32 %30, i8* %31)
%33 = load i8*, i8** %4
%34 = call i32 @cm_replace_entry(i8* %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cm_group_set_gid.buf, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 %34, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %41
38: ; preds = %25
%39 = load i8*, i8** @cm_group_set_gid.membuf
%40 = call i32 @free(i8* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %36, %23, %13
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.group = type { i8*, i32 }
@cm_group_set_gid.buf = internal global [512 x i8] zeroinitializer
@cm_group_set_gid.membuf = internal unnamed_addr global i8* null
@errno = global i32 0
@.str = [13 x i8] c"%s:%s:%d:%s\0A\00"
@.str.1 = [24 x i8] c"%[^:]:%*[^:]:%*d:%*[^:]\00"
@.str.2 = [11 x i8] c"/etc/group\00"
@.str.3 = [15 x i8] c"/etc/.group.cm\00"
define i32 @cm_group_set_gid(i8* %0, i32 %1) {
%3 = tail call %struct.group* @getgrnam(i8* %0)
%4 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
store i8* %4, i8** @cm_group_set_gid.membuf
store i32 0, i32* @errno
%5 = icmp eq %struct.group* %3, null
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i32 @cm_join_members(i32 %8, i8* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = load i8*, i8** @cm_group_set_gid.membuf
%15 = tail call i32 @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cm_group_set_gid.buf, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13, i32 %1, i8* %14)
%16 = tail call i32 @cm_replace_entry(i8* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cm_group_set_gid.buf, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i8*, i8** @cm_group_set_gid.membuf
%20 = tail call i32 @free(i8* %19)
br label %21
21: ; preds = %11, %6, %2, %18
%22 = phi i32 [ 0, %18 ], [ 0, %2 ], [ %9, %6 ], [ %16, %11 ]
ret i32 %22
}