#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 gid_t;

/* Variables and functions */
struct group *getgrgid(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
void gid_to_name(gid_t gid, char *group_id) {
  struct group *group;
  if ((group = getgrgid(gid)) == NULL) {
    sprintf(group_id, "%08d", gid);
  } else {
    strncpy(group_id, group->gr_name, 8);
  }
  return;
}


x86 assembly (clang, -O0):
<gid_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    116d <gid_to_name+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe9d(%rip),%rsi
call   1050 <sprintf@plt>
jmp    1181 <gid_to_name+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x8,%edx
call   1030 <strncpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gid_to_name>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <getgrgid@plt>
test   %rax,%rax
je     1157 <gid_to_name+0x27>
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x8,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <strncpy@plt>
lea    0xea2(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <sprintf@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [5 x i8] c"%08d\00"
define void @gid_to_name(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.group*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = call %struct.group* @getgrgid(i32 %6)
store %struct.group* %7, %struct.group** %5
%8 = icmp eq %struct.group* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i32, i32* %3
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %19
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load %struct.group*, %struct.group** %5
%16 = getelementptr inbounds %struct.group, %struct.group* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strncpy(i8* %14, i32 %17, i32 8)
br label %19
19: ; preds = %13, %9
ret void
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [5 x i8] c"%08d\00"
define void @gid_to_name(i32 %0, i8* %1) {
%3 = tail call %struct.group* @getgrgid(i32 %0)
%4 = icmp eq %struct.group* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %11
7: ; preds = %2
%8 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @strncpy(i8* %1, i32 %9, i32 8)
br label %11
11: ; preds = %7, %5
ret void
}