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

/* Variables and functions */
int /*<<< orphan*/ atol(char *);
int /*<<< orphan*/ exit(int);
struct group *getgrnam(char *);
scalar_t__ isdigit(char);
int /*<<< orphan*/ printf(char *, char *, int);
gid_t str2gid(char *str, int line_num) {
  struct group *gr;
  if (isdigit(str[0]))
    return (uid_t)atol(str);
  if (!(gr = getgrnam(str))) {
    printf("# ERROR # Invalid gid '%s' reading line %d\n", str, line_num);
    exit(255);
  }
  return gr->gr_gid;
}


x86 assembly (clang, -O0):
<str2gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1186 <str2gid+0x36>
mov    -0x10(%rbp),%rdi
call   1060 <atol@plt>
mov    %eax,-0x4(%rbp)
jmp    11c3 <str2gid+0x73>
mov    -0x10(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ba <str2gid+0x6a>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    $0xff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2gid>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movsbl (%rdi),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1173 <str2gid+0x23>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <atol@plt>
mov    %rbx,%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     1189 <str2gid+0x39>
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe70(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    $0xff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [44 x i8] c"# ERROR # Invalid gid '%s' reading line %d\0A\00"
define i32 @str2gid(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.group*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = call i64 @isdigit(i8 signext %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @atol(i8* %13)
store i32 %14, i32* %3
br label %28
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call %struct.group* @getgrnam(i8* %16)
store %struct.group* %17, %struct.group** %6
%18 = icmp ne %struct.group* %17, null
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
%23 = call i32 @exit(i32 255)
unreachable
24: ; preds = %15
%25 = load %struct.group*, %struct.group** %6
%26 = getelementptr inbounds %struct.group, %struct.group* %25, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %3
br label %28
28: ; preds = %24, %12
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [44 x i8] c"# ERROR # Invalid gid '%s' reading line %d\0A\00"
define i32 @str2gid(i8* %0, i32 %1) {
%3 = load i8, i8* %0
%4 = tail call i64 @isdigit(i8 signext %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @atol(i8* %0)
br label %17
8: ; preds = %2
%9 = tail call %struct.group* @getgrnam(i8* %0)
%10 = icmp eq %struct.group* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%13 = tail call i32 @exit(i32 255)
unreachable
14: ; preds = %8
%15 = getelementptr inbounds %struct.group, %struct.group* %9, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %14, %6
%18 = phi i32 [ %7, %6 ], [ %16, %14 ]
ret i32 %18
}