#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 {
  int /*<<< orphan*/ gr_name;
};
typedef int /*<<< orphan*/ id;

/* Variables and functions */
struct group *getgrgid(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *get_gid_str(int gid) {
  struct group *grp;
  static char id[10];
  snprintf(id, sizeof(id), "%i", gid);
  if ((grp = getgrgid(gid)) != NULL)
    strncpy(id, grp->gr_name, sizeof(id));
  id[sizeof(id) - 1] = '\0';
  return id;
}


x86 assembly (clang, -O0):
<get_gid_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%ecx
lea    0x2ef4(%rip),%rdi
mov    $0xa,%esi
lea    0xeaf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x4(%rbp),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1183 <get_gid_str+0x53>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0x2ec0(%rip),%rdi
mov    $0xa,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2eb8(%rip)
lea    0x2ea8(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gid_str>:
push   %rbx
mov    %edi,%ebx
lea    0x2eff(%rip),%rdi
lea    0xebf(%rip),%rdx
mov    $0xa,%esi
mov    %ebx,%ecx
call   1050 <snprintf@plt>
mov    %ebx,%edi
call   1040 <getgrgid@plt>
test   %rax,%rax
je     116c <get_gid_str+0x3c>
mov    (%rax),%esi
lea    0x2ed7(%rip),%rdi
mov    $0xa,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2ecf(%rip)
lea    0x2ebf(%rip),%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@get_gid_str.id = internal global [10 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @get_gid_str(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.group*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @snprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0), i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* %2
%7 = call %struct.group* @getgrgid(i32 %6)
store %struct.group* %7, %struct.group** %3
%8 = icmp ne %struct.group* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load %struct.group*, %struct.group** %3
%11 = getelementptr inbounds %struct.group, %struct.group* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strncpy(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0), i32 %12, i32 10)
br label %14
14: ; preds = %9, %1
store i8 0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 9)
ret i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0)
}

LLVM IR (-O2):
%struct.group = type { i32 }
@get_gid_str.id = internal global [10 x i8] zeroinitializer
@.str = [3 x i8] c"%i\00"
define i8* @get_gid_str(i32 %0) {
%2 = tail call i32 @snprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0), i32 10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call %struct.group* @getgrgid(i32 %0)
%4 = icmp eq %struct.group* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @strncpy(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0), i32 %7, i32 10)
br label %9
9: ; preds = %5, %1
store i8 0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 9)
ret i8* getelementptr inbounds ([10 x i8], [10 x i8]* @get_gid_str.id, i64 0, i64 0)
}