#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 *, size_t,
                      struct group **);
char *realloc(char *, size_t);
scalar_t__ strcmp(char const *, char *);
scalar_t__ strtoul(char const *, char **, int);
size_t 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;
  size_t 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    %rax,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11c1 <util_lookup_group+0x51>
movq   $0x0,-0x8(%rbp)
jmp    132b <util_lookup_group+0x1bb>
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    11f4 <util_lookup_group+0x84>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132b <util_lookup_group+0x1bb>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1225 <util_lookup_group+0xb5>
jmp    131a <util_lookup_group+0x1aa>
mov    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%r8
call   1090 <getgrnam_r@plt>
mov    %rax,%rcx
mov    0x2d80(%rip),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x40(%rbp)
je     126e <util_lookup_group+0xfe>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1315 <util_lookup_group+0x1a5>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%rax
jne    1299 <util_lookup_group+0x129>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1204 <util_lookup_group+0x94>
mov    0x2d30(%rip),%rax
cmpq   $0x0,(%rax)
je     12de <util_lookup_group+0x16e>
mov    0x2d1f(%rip),%rax
mov    (%rax),%rax
mov    0x2d05(%rip),%rcx
cmp    (%rcx),%rax
je     12de <util_lookup_group+0x16e>
mov    0x2d05(%rip),%rax
mov    (%rax),%rax
mov    0x2ce3(%rip),%rcx
cmp    (%rcx),%rax
jne    12f7 <util_lookup_group+0x187>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd18(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    130b <util_lookup_group+0x19b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd1d(%rip),%rsi
call   1040 <udev_log_null@plt>
jmp    1310 <util_lookup_group+0x1a0>
jmp    1315 <util_lookup_group+0x1a5>
jmp    131a <util_lookup_group+0x1aa>
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,%r15
mov    %rdi,%rbp
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,%rbx
lea    0xe54(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1258 <util_lookup_group+0xd8>
lea    0x18(%rsp),%rsi
mov    %r15,%rdi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %rax,%r12
mov    0x18(%rsp),%rax
cmpb   $0x0,(%rax)
je     12ac <util_lookup_group+0x12c>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     125d <util_lookup_group+0xdd>
mov    %rbp,0x8(%rsp)
lea    0x10(%rsp),%r14
mov    0x2dd2(%rip),%rbp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rax,%r13
mov    %r15,%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %r14,%r8
call   10a0 <getgrnam_r@plt>
mov    %rax,0x0(%rbp)
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
jne    1262 <util_lookup_group+0xe2>
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <util_lookup_group+0xe7>
add    %rbx,%rbx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
jne    1210 <util_lookup_group+0x90>
jmp    12a4 <util_lookup_group+0x124>
xor    %r12d,%r12d
jmp    12ac <util_lookup_group+0x12c>
xor    %r13d,%r13d
jmp    12a1 <util_lookup_group+0x121>
mov    (%rcx),%r12
jmp    12a4 <util_lookup_group+0x124>
test   %rax,%rax
je     128d <util_lookup_group+0x10d>
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     128d <util_lookup_group+0x10d>
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
je     128d <util_lookup_group+0x10d>
lea    0xd98(%rip),%rsi
jmp    1294 <util_lookup_group+0x114>
lea    0xd71(%rip),%rsi
mov    0x8(%rsp),%rdi
mov    %r15,%rdx
call   1040 <udev_log_null@plt>
xor    %r12d,%r12d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%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 i64
%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 i64 @sysconf(i32 %13)
store i64 %14, i64* %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 i64, i64* %7
%33 = call i8* @realloc(i8* %31, i64 %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 i64, i64* %7
%42 = call i64 @getgrnam_r(i8* %39, %struct.group* %9, i8* %40, i64 %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 i64, i64* %7
%55 = mul i64 %54, 2
store i64 %55, i64* %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 i64 @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* @malloc(i64 %8)
%20 = icmp eq i8* %19, null
br i1 %20, label %49, label %24
21: ; preds = %33
%22 = call i8* @realloc(i8* %25, i64 %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 i64 [ %36, %21 ], [ %8, %18 ]
%27 = call i64 @getgrnam_r(i8* %1, %struct.group* %4, i8* %25, i64 %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 i64 %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
}