#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 udev {
  int dummy;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ ESRCH;
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
scalar_t__ errno;
int /*<<< orphan*/ getgrnam_r(char const *, struct group *, char *, int,
                              struct group **);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strtoul(char const *, char **, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *);
gid_t util_lookup_group(struct udev *udev, const char *group) {
  char *endptr;
  int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
  char buf[buflen];
  struct group grbuf;
  struct group *gr;
  gid_t gid = 0;
  if (strcmp(group, "root") == 0)
    return 0;
  gid = strtoul(group, &endptr, 10);
  if (endptr[0] == '\0')
    return gid;
  errno = 0;
  getgrnam_r(group, &grbuf, buf, buflen, &gr);
  if (gr != NULL)
    return gr->gr_gid;
  if (errno == 0 || errno == ENOENT || errno == ESRCH)
    udev_log_null(udev, "specified group '%s' unknown\n", group);
  else
    udev_log_null(udev, "error resolving group '%s': %m\n", group);
  return 0;
}


x86 assembly (clang, -O0):
<util_lookup_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11cb <util_lookup_group+0x7b>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c8 <util_lookup_group+0x178>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1202 <util_lookup_group+0xb2>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c8 <util_lookup_group+0x178>
mov    -0x58(%rbp),%rdx
mov    0x2dcb(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%ecx
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%r8
call   1070 <getgrnam_r@plt>
cmpq   $0x0,-0x48(%rbp)
je     1248 <util_lookup_group+0xf8>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c8 <util_lookup_group+0x178>
mov    0x2d89(%rip),%rax
cmpq   $0x0,(%rax)
je     128d <util_lookup_group+0x13d>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
je     128d <util_lookup_group+0x13d>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <util_lookup_group+0x156>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd69(%rip),%rsi
call   1030 <udev_log_null@plt>
jmp    12ba <util_lookup_group+0x16a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd6e(%rip),%rsi
call   1030 <udev_log_null@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_lookup_group>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rdi,-0x30(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,%r13d
mov    %rsp,%r14
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6a(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     123e <util_lookup_group+0xee>
lea    -0x48(%rbp),%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1060 <strtoul@plt>
mov    -0x48(%rbp),%rcx
cmpb   $0x0,(%rcx)
je     11f6 <util_lookup_group+0xa6>
mov    0x2e0d(%rip),%rax
movq   $0x0,(%rax)
lea    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1070 <getgrnam_r@plt>
mov    -0x38(%rbp),%rax
test   %rax,%rax
je     11fb <util_lookup_group+0xab>
mov    (%rax),%r15d
jmp    123e <util_lookup_group+0xee>
mov    %eax,%r15d
jmp    123e <util_lookup_group+0xee>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
test   %rax,%rax
je     122b <util_lookup_group+0xdb>
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
je     122b <util_lookup_group+0xdb>
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
je     122b <util_lookup_group+0xdb>
lea    0xdfa(%rip),%rsi
jmp    1232 <util_lookup_group+0xe2>
lea    0xdd3(%rip),%rsi
mov    -0x30(%rbp),%rdi
mov    %r12,%rdx
call   1030 <udev_log_null@plt>
mov    %r14,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ENOENT = global i64 0
@ESRCH = global i64 0
@.str.1 = [30 x i8] c"specified group '%s' unknown\0A\00"
@.str.2 = [32 x i8] c"error resolving group '%s': %m\0A\00"
define i32 @util_lookup_group(%struct.udev* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.group
%11 = alloca %struct.group*
%12 = alloca i32
%13 = alloca i32
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%14 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%15 = call i32 @sysconf(i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %8
%19 = alloca i8, i64 %17
store i64 %17, i64* %9
store i32 0, i32* %12
%20 = load i8*, i8** %5
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %64
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = call i32 @strtoul(i8* %25, i8** %6, i32 10)
store i32 %26, i32* %12
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = load i32, i32* %12
store i32 %33, i32* %3
store i32 1, i32* %13
br label %64
34: ; preds = %24
store i64 0, i64* @errno
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i32 @getgrnam_r(i8* %35, %struct.group* %10, i8* %19, i32 %36, %struct.group** %11)
%38 = load %struct.group*, %struct.group** %11
%39 = icmp ne %struct.group* %38, null
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load %struct.group*, %struct.group** %11
%42 = getelementptr inbounds %struct.group, %struct.group* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %3
store i32 1, i32* %13
br label %64
44: ; preds = %34
%45 = load i64, i64* @errno
%46 = icmp eq i64 %45, 0
br i1 %46, label %55, label %47
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @ENOENT
%50 = icmp eq i64 %48, %49
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i64, i64* @errno
%53 = load i64, i64* @ESRCH
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %59
55: ; preds = %51, %47, %44
%56 = load %struct.udev*, %struct.udev** %4
%57 = load i8*, i8** %5
%58 = call i32 @udev_log_null(%struct.udev* %56, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %63
59: ; preds = %51
%60 = load %struct.udev*, %struct.udev** %4
%61 = load i8*, i8** %5
%62 = call i32 @udev_log_null(%struct.udev* %60, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %59, %55
store i32 0, i32* %3
store i32 1, i32* %13
br label %64
64: ; preds = %63, %40, %32, %23
%65 = load i8*, i8** %8
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ENOENT = global i64 0
@ESRCH = global i64 0
@.str.1 = [30 x i8] c"specified group '%s' unknown\0A\00"
@.str.2 = [32 x i8] c"error resolving group '%s': %m\0A\00"
define i32 @util_lookup_group(%struct.udev* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.group
%5 = alloca %struct.group*
%6 = bitcast i8** %3 to i8*
%7 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%8 = tail call i32 @sysconf(i32 %7)
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast %struct.group* %4 to i8*
%13 = bitcast %struct.group** %5 to i8*
%14 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %41, label %16
16: ; preds = %2
%17 = call i32 @strtoul(i8* %1, i8** %3, i32 10)
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %16
store i64 0, i64* @errno
%22 = call i32 @getgrnam_r(i8* %1, %struct.group* %4, i8* %11, i32 %8, %struct.group** %5)
%23 = load %struct.group*, %struct.group** %5
%24 = icmp eq %struct.group* %23, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.group, %struct.group* %23, i64 0, i32 0
%27 = load i32, i32* %26
br label %41
28: ; preds = %21
%29 = load i64, i64* @errno
%30 = icmp eq i64 %29, 0
%31 = load i64, i64* @ENOENT
%32 = icmp eq i64 %29, %31
%33 = select i1 %30, i1 true, i1 %32
%34 = load i64, i64* @ESRCH
%35 = icmp eq i64 %29, %34
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %41
39: ; preds = %28
%40 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %41
41: ; preds = %37, %39, %16, %2, %25
%42 = phi i32 [ %27, %25 ], [ 0, %2 ], [ %17, %16 ], [ 0, %39 ], [ 0, %37 ]
ret i32 %42
}