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

/* Variables and functions */
int /*<<< orphan*/ fail(char *, char *);
struct group *getgrnam(char *);
scalar_t__ strtol(char *, char **, int /*<<< orphan*/);
gid_t togid(char *group) {
  struct group *gr;
  gid_t gid;
  char *cp;
  gr = getgrnam(group);
  if (gr)
    return gr->gr_gid;
  gid = strtol(group, &cp, 0);
  if (gid == 0 && cp == group)
    fail("cannot find gid for %s", group);
  return gid;
}


x86 assembly (clang, -O0):
<togid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1164 <togid+0x34>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <togid+0x78>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a0 <togid+0x70>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11a0 <togid+0x70>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1030 <fail@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<togid>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1050 <getgrnam@plt>
test   %rax,%rax
je     1147 <togid+0x17>
mov    (%rax),%rax
jmp    1173 <togid+0x43>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <strtol@plt>
test   %rax,%rax
jne    1173 <togid+0x43>
cmp    %rbx,0x8(%rsp)
jne    1173 <togid+0x43>
lea    0xe97(%rip),%rdi
mov    %rbx,%rsi
call   1030 <fail@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@.str = [23 x i8] c"cannot find gid for %s\00"
define i64 @togid(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.group*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call %struct.group* @getgrnam(i8* %7)
store %struct.group* %8, %struct.group** %4
%9 = load %struct.group*, %struct.group** %4
%10 = icmp ne %struct.group* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load %struct.group*, %struct.group** %4
%13 = getelementptr inbounds %struct.group, %struct.group* %12, i32 0, i32 0
%14 = load i64, i64* %13
store i64 %14, i64* %2
br label %29
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i64 @strtol(i8* %16, i8** %6, i32 0)
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i8*, i8** %6
%22 = load i8*, i8** %3
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** %3
%26 = call i32 @fail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %20, %15
%28 = load i64, i64* %5
store i64 %28, i64* %2
br label %29
29: ; preds = %27, %11
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
%struct.group = type { i64 }
@.str = [23 x i8] c"cannot find gid for %s\00"
define i64 @togid(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call %struct.group* @getgrnam(i8* %0)
%5 = icmp eq %struct.group* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.group, %struct.group* %4, i64 0, i32 0
%8 = load i64, i64* %7
br label %17
9: ; preds = %1
%10 = call i64 @strtol(i8* %0, i8** %2, i32 0)
%11 = icmp eq i64 %10, 0
%12 = load i8*, i8** %2
%13 = icmp eq i8* %12, %0
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @fail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %9, %15, %6
%18 = phi i64 [ %8, %6 ], [ 0, %15 ], [ %10, %9 ]
ret i64 %18
}