#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 {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ ERANGE;
scalar_t__ ESRCH;
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getgrnam_r(char const *, struct group *, char *, int,
                      struct group **);
char *realloc(char *, int);
scalar_t__ strcmp(char const *, char *);
scalar_t__ 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;
  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;
  buf = NULL;
  gid = 0;
  for (;;) {
    char *newbuf;
    newbuf = realloc(buf, buflen);
    if (!newbuf)
      break;
    buf = newbuf;
    errno = getgrnam_r(group, &grbuf, buf, buflen, &gr);
    if (gr != NULL) {
      gid = gr->gr_gid;
    } else if (errno == ERANGE) {
      buflen *= 2;
      continue;
    } else 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);
    }
    break;
  }
  free(buf);
  return gid;
}


x86 assembly (clang, -O0):
<util_lookup_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11c0 <util_lookup_group+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1325 <util_lookup_group+0x1b5>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11f3 <util_lookup_group+0x83>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1325 <util_lookup_group+0x1b5>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1223 <util_lookup_group+0xb3>
jmp    1314 <util_lookup_group+0x1a4>
mov    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%r8
call   1090 <getgrnam_r@plt>
mov    %rax,%rcx
mov    0x2d83(%rip),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x40(%rbp)
je     126b <util_lookup_group+0xfb>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    130f <util_lookup_group+0x19f>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
jne    1293 <util_lookup_group+0x123>
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1203 <util_lookup_group+0x93>
mov    0x2d36(%rip),%rax
cmpq   $0x0,(%rax)
je     12d8 <util_lookup_group+0x168>
mov    0x2d25(%rip),%rax
mov    (%rax),%rax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%rax
je     12d8 <util_lookup_group+0x168>
mov    0x2d0b(%rip),%rax
mov    (%rax),%rax
mov    0x2ce9(%rip),%rcx
cmp    (%rcx),%rax
jne    12f1 <util_lookup_group+0x181>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd1e(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    1305 <util_lookup_group+0x195>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd23(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    130a <util_lookup_group+0x19a>
jmp    130f <util_lookup_group+0x19f>
jmp    1314 <util_lookup_group+0x1a4>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_lookup_group>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,%ebp
lea    0xe65(%rip),%rsi
mov    %r12,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1245 <util_lookup_group+0xd5>
lea    0x18(%rsp),%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %rax,%r15
mov    0x18(%rsp),%rax
cmpb   $0x0,(%rax)
je     1298 <util_lookup_group+0x128>
xor    %r15d,%r15d
xor    %edi,%edi
mov    %ebp,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     124a <util_lookup_group+0xda>
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%r14
mov    0x2ddf(%rip),%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rbx
mov    %r12,%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
mov    %ebp,%ecx
mov    %r14,%r8
call   1090 <getgrnam_r@plt>
mov    %rax,0x0(%r13)
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
jne    124e <util_lookup_group+0xde>
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    1253 <util_lookup_group+0xe3>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
test   %rax,%rax
jne    1200 <util_lookup_group+0x90>
jmp    1290 <util_lookup_group+0x120>
xor    %r15d,%r15d
jmp    1298 <util_lookup_group+0x128>
xor    %ebx,%ebx
jmp    1290 <util_lookup_group+0x120>
mov    (%rcx),%r15
jmp    1290 <util_lookup_group+0x120>
test   %rax,%rax
je     1279 <util_lookup_group+0x109>
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%rax
je     1279 <util_lookup_group+0x109>
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     1279 <util_lookup_group+0x109>
lea    0xdac(%rip),%rsi
jmp    1280 <util_lookup_group+0x110>
lea    0xd85(%rip),%rsi
mov    0x8(%rsp),%rdi
mov    %r12,%rdx
call   1040 <udev_log_null@plt>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.group = type { i64 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ERANGE = 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 i64 @util_lookup_group(%struct.udev* %0, i8* %1) {
%3 = alloca i64
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.group
%10 = alloca %struct.group*
%11 = alloca i64
%12 = alloca i8*
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%13 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%14 = call i32 @sysconf(i32 %13)
store i32 %14, i32* %7
store i64 0, i64* %11
%15 = load i8*, i8** %5
%16 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i64 0, i64* %3
br label %82
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i64 @strtoul(i8* %20, i8** %6, i32 10)
store i64 %21, i64* %11
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = load i64, i64* %11
store i64 %28, i64* %3
br label %82
29: ; preds = %19
store i8* null, i8** %8
store i64 0, i64* %11
br label %30
30: ; preds = %53, %29
%31 = load i8*, i8** %8
%32 = load i32, i32* %7
%33 = call i8* @realloc(i8* %31, i32 %32)
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = icmp ne i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %30
br label %78
37: ; preds = %30
%38 = load i8*, i8** %12
store i8* %38, i8** %8
%39 = load i8*, i8** %5
%40 = load i8*, i8** %8
%41 = load i32, i32* %7
%42 = call i64 @getgrnam_r(i8* %39, %struct.group* %9, i8* %40, i32 %41, %struct.group** %10)
store i64 %42, i64* @errno
%43 = load %struct.group*, %struct.group** %10
%44 = icmp ne %struct.group* %43, null
br i1 %44, label %45, label %49
45: ; preds = %37
%46 = load %struct.group*, %struct.group** %10
%47 = getelementptr inbounds %struct.group, %struct.group* %46, i32 0, i32 0
%48 = load i64, i64* %47
store i64 %48, i64* %11
br label %77
49: ; preds = %37
%50 = load i64, i64* @errno
%51 = load i64, i64* @ERANGE
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %7
%55 = mul nsw i32 %54, 2
store i32 %55, i32* %7
br label %30
56: ; preds = %49
%57 = load i64, i64* @errno
%58 = icmp eq i64 %57, 0
br i1 %58, label %67, label %59
59: ; preds = %56
%60 = load i64, i64* @errno
%61 = load i64, i64* @ENOENT
%62 = icmp eq i64 %60, %61
br i1 %62, label %67, label %63
63: ; preds = %59
%64 = load i64, i64* @errno
%65 = load i64, i64* @ESRCH
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %63, %59, %56
%68 = load %struct.udev*, %struct.udev** %4
%69 = load i8*, i8** %5
%70 = call i32 @udev_log_null(%struct.udev* %68, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %69)
br label %75
71: ; preds = %63
%72 = load %struct.udev*, %struct.udev** %4
%73 = load i8*, i8** %5
%74 = call i32 @udev_log_null(%struct.udev* %72, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %71, %67
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76, %45
br label %78
78: ; preds = %77, %36
%79 = load i8*, i8** %8
%80 = call i32 @free(i8* %79)
%81 = load i64, i64* %11
store i64 %81, i64* %3
br label %82
82: ; preds = %78, %27, %18
%83 = load i64, i64* %3
ret i64 %83
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.group = type { i64 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ERANGE = 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 i64 @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 = bitcast %struct.group* %4 to i8*
%10 = bitcast %struct.group** %5 to i8*
%11 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %53, label %13
13: ; preds = %2
%14 = call i64 @strtoul(i8* %1, i8** %3, i32 10)
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %53, label %18
18: ; preds = %13
%19 = call i8* @realloc(i8* null, i32 %8)
%20 = icmp eq i8* %19, null
br i1 %20, label %49, label %24
21: ; preds = %33
%22 = call i8* @realloc(i8* %25, i32 %36)
%23 = icmp eq i8* %22, null
br i1 %23, label %49, label %24
24: ; preds = %18, %21
%25 = phi i8* [ %22, %21 ], [ %19, %18 ]
%26 = phi i32 [ %36, %21 ], [ %8, %18 ]
%27 = call i64 @getgrnam_r(i8* %1, %struct.group* %4, i8* %25, i32 %26, %struct.group** %5)
store i64 %27, i64* @errno
%28 = load %struct.group*, %struct.group** %5
%29 = icmp eq %struct.group* %28, null
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = getelementptr inbounds %struct.group, %struct.group* %28, i64 0, i32 0
%32 = load i64, i64* %31
br label %49
33: ; preds = %24
%34 = load i64, i64* @ERANGE
%35 = icmp eq i64 %27, %34
%36 = shl nsw i32 %26, 1
br i1 %35, label %21, label %37
37: ; preds = %33
%38 = icmp eq i64 %27, 0
%39 = load i64, i64* @ENOENT
%40 = icmp eq i64 %27, %39
%41 = select i1 %38, i1 true, i1 %40
%42 = load i64, i64* @ESRCH
%43 = icmp eq i64 %27, %42
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = 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 %49
47: ; preds = %37
%48 = 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 %49
49: ; preds = %21, %18, %47, %45, %30
%50 = phi i8* [ %25, %30 ], [ %25, %45 ], [ %25, %47 ], [ null, %18 ], [ %25, %21 ]
%51 = phi i64 [ %32, %30 ], [ 0, %45 ], [ 0, %47 ], [ 0, %18 ], [ 0, %21 ]
%52 = call i32 @free(i8* %50)
br label %53
53: ; preds = %13, %2, %49
%54 = phi i64 [ %51, %49 ], [ 0, %2 ], [ %14, %13 ]
ret i64 %54
}