#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_name;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int _getgrgid_r(int /*<<< orphan*/, struct group *, char *, int,
                struct group **);
char *xstrdup(char *);
char *gid_to_string(gid_t gid) {
  struct group grp, *result;
  char buffer[65536], *gstring;
  int rc;
  rc = _getgrgid_r(gid, &grp, buffer, 65536, &result);
  if (rc == 0 && result)
    gstring = xstrdup(result->gr_name);
  else
    gstring = xstrdup("nobody");
  return gstring;
}


x86 assembly (clang, -O0):
<gid_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10020(%rbp),%rdx
lea    -0x10(%rbp),%rsi
mov    $0x10000,%ecx
lea    -0x18(%rbp),%r8
call   1040 <_getgrgid_r@plt>
mov    %eax,-0x1002c(%rbp)
cmpl   $0x0,-0x1002c(%rbp)
jne    1180 <gid_to_string+0x60>
cmpq   $0x0,-0x18(%rbp)
je     1180 <gid_to_string+0x60>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <xstrdup@plt>
mov    %rax,-0x10028(%rbp)
jmp    1193 <gid_to_string+0x73>
lea    0xe79(%rip),%rdi
call   1030 <xstrdup@plt>
mov    %rax,-0x10028(%rbp)
mov    -0x10028(%rbp),%rax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gid_to_string>:
sub    $0x10018,%rsp
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%r8
mov    $0x10000,%ecx
call   1040 <_getgrgid_r@plt>
lea    0xebb(%rip),%rdi
test   %eax,%eax
jne    1155 <gid_to_string+0x35>
mov    (%rsp),%rax
test   %rax,%rax
je     1155 <gid_to_string+0x35>
mov    (%rax),%rdi
call   1030 <xstrdup@plt>
add    $0x10018,%rsp
ret

LLVM IR (-O0):
%struct.group = type { i8* }
@.str = [7 x i8] c"nobody\00"
define i8* @gid_to_string(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.group
%4 = alloca %struct.group*
%5 = alloca [65536 x i8]
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%10 = call i32 @_getgrgid_r(i32 %8, %struct.group* %3, i8* %9, i32 65536, %struct.group** %4)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %1
%14 = load %struct.group*, %struct.group** %4
%15 = icmp ne %struct.group* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load %struct.group*, %struct.group** %4
%18 = getelementptr inbounds %struct.group, %struct.group* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i8* @xstrdup(i8* %19)
store i8* %20, i8** %6
br label %23
21: ; preds = %13, %1
%22 = call i8* @xstrdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %6
br label %23
23: ; preds = %21, %16
%24 = load i8*, i8** %6
ret i8* %24
}

LLVM IR (-O2):
%struct.group = type { i8* }
@.str = [7 x i8] c"nobody\00"
define i8* @gid_to_string(i32 %0) {
%2 = alloca %struct.group
%3 = alloca %struct.group*
%4 = alloca [65536 x i8]
%5 = bitcast %struct.group* %2 to i8*
%6 = bitcast %struct.group** %3 to i8*
%7 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%8 = call i32 @_getgrgid_r(i32 %0, %struct.group* %2, i8* %7, i32 65536, %struct.group** %3)
%9 = icmp eq i32 %8, 0
%10 = load %struct.group*, %struct.group** %3
%11 = icmp ne %struct.group* %10, null
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = getelementptr inbounds %struct.group, %struct.group* %10, i64 0, i32 0
%15 = load i8*, i8** %14
br label %16
16: ; preds = %1, %13
%17 = phi i8* [ %15, %13 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %1 ]
%18 = call i8* @xstrdup(i8* %17)
ret i8* %18
}