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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
struct group *getgrnam(char const *);
gid_t strtogid(const char *str) {
  struct group *gr = NULL;
  errno = 0;
  gr = getgrnam(str);
  if (errno) {
    return (gid_t)-1;
  } else if (!gr) {
    errno = EINVAL;
    return (gid_t)-1;
  }
  return gr->gr_gid;
}


x86 assembly (clang, -O0):
<strtogid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2eb5(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e9a(%rip),%rax
cmpq   $0x0,(%rax)
je     115c <strtogid+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <strtogid+0x85>
cmpq   $0x0,-0x18(%rbp)
jne    1187 <strtogid+0x77>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rcx
mov    0x2e68(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <strtogid+0x85>
jmp    118c <strtogid+0x7c>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtogid>:
push   %rbx
mov    0x2ec8(%rip),%rbx
movq   $0x0,(%rbx)
call   1030 <getgrnam@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpq   $0x0,(%rbx)
je     1134 <strtogid+0x24>
pop    %rbx
ret
test   %rcx,%rcx
je     113d <strtogid+0x2d>
mov    (%rcx),%eax
pop    %rbx
ret
mov    0x2e94(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@errno = global i64 0
@EINVAL = global i64 0
define i32 @strtogid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.group*
store i8* %0, i8** %3
store %struct.group* null, %struct.group** %4
store i64 0, i64* @errno
%5 = load i8*, i8** %3
%6 = call %struct.group* @getgrnam(i8* %5)
store %struct.group* %6, %struct.group** %4
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %20
10: ; preds = %1
%11 = load %struct.group*, %struct.group** %4
%12 = icmp ne %struct.group* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = load i64, i64* @EINVAL
store i64 %14, i64* @errno
store i32 -1, i32* %2
br label %20
15: ; preds = %10
br label %16
16: ; preds = %15
%17 = load %struct.group*, %struct.group** %4
%18 = getelementptr inbounds %struct.group, %struct.group* %17, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %2
br label %20
20: ; preds = %16, %13, %9
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.group = type { i32 }
@errno = global i64 0
@EINVAL = global i64 0
define i32 @strtogid(i8* %0) {
store i64 0, i64* @errno
%2 = tail call %struct.group* @getgrnam(i8* %0)
%3 = load i64, i64* @errno
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = icmp eq %struct.group* %2, null
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = load i64, i64* @EINVAL
store i64 %8, i64* @errno
br label %12
9: ; preds = %5
%10 = getelementptr inbounds %struct.group, %struct.group* %2, i64 0, i32 0
%11 = load i32, i32* %10
br label %12
12: ; preds = %1, %9, %7
%13 = phi i32 [ %11, %9 ], [ -1, %7 ], [ -1, %1 ]
ret i32 %13
}