#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 dummy;
};

/* Variables and functions */
int ENOENT;
int ENOMEM;
struct group *getgrnam(char const *);
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t buflen,
               struct group **result) {
  struct group *gp, *bgp;
  *result = NULL;
  bgp = (struct group *)buffer;
  if (buflen < sizeof(struct group))
    return ENOMEM;
  gp = getgrnam(name);
  if (gp) {
    *bgp = *gp;
    *result = bgp;
  }
  return (gp) ? ENOENT : 0;
}


x86 assembly (clang, -O0):
<getgrnam_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x4,-0x28(%rbp)
jae    115b <getgrnam_r+0x4b>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <getgrnam_r+0xa6>
mov    -0x10(%rbp),%rdi
call   1030 <getgrnam@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     118a <getgrnam_r+0x7a>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x38(%rbp)
je     11a6 <getgrnam_r+0x96>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11b0 <getgrnam_r+0xa0>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    11b0 <getgrnam_r+0xa0>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrnam_r>:
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%r8)
cmp    $0x3,%rcx
ja     112a <getgrnam_r+0x1a>
mov    0x2eb8(%rip),%rax
jmp    1153 <getgrnam_r+0x43>
mov    %r8,%rbx
mov    %rdx,%r14
call   1030 <getgrnam@plt>
mov    %rax,%rcx
test   %rax,%rax
je     1145 <getgrnam_r+0x35>
mov    (%rcx),%eax
mov    %eax,(%r14)
mov    %r14,(%rbx)
xor    %eax,%eax
test   %rcx,%rcx
je     1155 <getgrnam_r+0x45>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@ENOMEM = global i32 0
@ENOENT = global i32 0
define i32 @getgrnam_r(i8* %0, %struct.group* %1, i8* %2, i64 %3, %struct.group** %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.group*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.group**
%12 = alloca %struct.group*
%13 = alloca %struct.group*
store i8* %0, i8** %7
store %struct.group* %1, %struct.group** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store %struct.group** %4, %struct.group*** %11
%14 = load %struct.group**, %struct.group*** %11
store %struct.group* null, %struct.group** %14
%15 = load i8*, i8** %9
%16 = bitcast i8* %15 to %struct.group*
store %struct.group* %16, %struct.group** %13
%17 = load i64, i64* %10
%18 = icmp ult i64 %17, 4
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = load i32, i32* @ENOMEM
store i32 %20, i32* %6
br label %41
21: ; preds = %5
%22 = load i8*, i8** %7
%23 = call %struct.group* @getgrnam(i8* %22)
store %struct.group* %23, %struct.group** %12
%24 = load %struct.group*, %struct.group** %12
%25 = icmp ne %struct.group* %24, null
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = load %struct.group*, %struct.group** %13
%28 = load %struct.group*, %struct.group** %12
%29 = bitcast %struct.group* %27 to i8*
%30 = bitcast %struct.group* %28 to i8*
%31 = load %struct.group*, %struct.group** %13
%32 = load %struct.group**, %struct.group*** %11
store %struct.group* %31, %struct.group** %32
br label %33
33: ; preds = %26, %21
%34 = load %struct.group*, %struct.group** %12
%35 = icmp ne %struct.group* %34, null
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i32, i32* @ENOENT
br label %39
38: ; preds = %33
br label %39
39: ; preds = %38, %36
%40 = phi i32 [ %37, %36 ], [ 0, %38 ]
store i32 %40, i32* %6
br label %41
41: ; preds = %39, %19
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
%struct.group = type { i32 }
@ENOMEM = global i32 0
@ENOENT = global i32 0
define i32 @getgrnam_r(i8* %0, %struct.group* nocapture readnone %1, i8* %2, i64 %3, %struct.group** nocapture writeonly %4) {
store %struct.group* null, %struct.group** %4
%6 = icmp ult i64 %3, 4
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = load i32, i32* @ENOMEM
br label %20
9: ; preds = %5
%10 = tail call %struct.group* @getgrnam(i8* %0)
%11 = icmp eq %struct.group* %10, null
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = getelementptr %struct.group, %struct.group* %10, i64 0, i32 0
%14 = bitcast i8* %2 to i32*
%15 = load i32, i32* %13
store i32 %15, i32* %14
%16 = bitcast %struct.group** %4 to i8**
store i8* %2, i8** %16
br label %17
17: ; preds = %12, %9
%18 = load i32, i32* @ENOENT
%19 = select i1 %11, i32 0, i32 %18
br label %20
20: ; preds = %17, %7
%21 = phi i32 [ %8, %7 ], [ %19, %17 ]
ret i32 %21
}