#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 {
  int gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ EX_NOUSER;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
struct group *getgrnam(char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int /*<<< orphan*/ strtol(char const *, char **, int);
int /*<<< orphan*/ usage();
void parse_gid(const char *s, char *buf, size_t bufsize) {
  struct group *gr;
  char *inval;
  if ((gr = getgrnam(s)) != NULL)
    snprintf(buf, bufsize, "%d", gr->gr_gid);
  else {
    strtol(s, &inval, 10);
    if (*inval != 0) {
      errx(EX_NOUSER, "unknown group id: %s", s);
      usage();
    } else {
      strncpy(buf, s, bufsize);
    }
  }
}


x86 assembly (clang, -O0):
<parse_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11aa <parse_gid+0x4a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
lea    0xe60(%rip),%rdx
call   1050 <snprintf@plt>
jmp    1207 <parse_gid+0xa7>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f1 <parse_gid+0x91>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1040 <errx@plt>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    1202 <parse_gid+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
jmp    1207 <parse_gid+0xa7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_gid>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1070 <getgrnam@plt>
test   %rax,%rax
je     1199 <parse_gid+0x39>
mov    (%rax),%ecx
lea    0xe7b(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11dd <parse_gid+0x7d>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdx
call   1040 <errx@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <usage@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <strncpy@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
@.str = [3 x i8] c"%d\00"
@EX_NOUSER = global i32 0
@.str.1 = [21 x i8] c"unknown group id: %s\00"
define void @parse_gid(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.group*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = call %struct.group* @getgrnam(i8* %9)
store %struct.group* %10, %struct.group** %7
%11 = icmp ne %struct.group* %10, null
br i1 %11, label %12, label %19
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = load %struct.group*, %struct.group** %7
%16 = getelementptr inbounds %struct.group, %struct.group* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @snprintf(i8* %13, i64 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %37
19: ; preds = %3
%20 = load i8*, i8** %4
%21 = call i32 @strtol(i8* %20, i8** %8, i32 10)
%22 = load i8*, i8** %8
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i32, i32* @EX_NOUSER
%28 = load i8*, i8** %4
%29 = call i32 @errx(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 (...) @usage()
br label %36
31: ; preds = %19
%32 = load i8*, i8** %5
%33 = load i8*, i8** %4
%34 = load i64, i64* %6
%35 = call i32 @strncpy(i8* %32, i8* %33, i64 %34)
br label %36
36: ; preds = %31, %26
br label %37
37: ; preds = %36, %12
ret void
}

LLVM IR (-O2):
%struct.group = type { i32 }
@.str = [3 x i8] c"%d\00"
@EX_NOUSER = global i32 0
@.str.1 = [21 x i8] c"unknown group id: %s\00"
define void @parse_gid(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call %struct.group* @getgrnam(i8* %0)
%7 = icmp eq %struct.group* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = getelementptr inbounds %struct.group, %struct.group* %6, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %23
12: ; preds = %3
%13 = call i32 @strtol(i8* %0, i8** %4, i32 10)
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = load i32, i32* @EX_NOUSER
%19 = tail call i32 @errx(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = tail call i32 (...) @usage()
br label %23
21: ; preds = %12
%22 = tail call i32 @strncpy(i8* %1, i8* %0, i64 %2)
br label %23
23: ; preds = %17, %21, %8
ret void
}