#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 stat {
  int st_uid;
  int st_gid;
};
struct group {
  char *gr_name;
};
typedef int gid_t;
typedef enum nss_status { ____Placeholder_nss_status } nss_status;

/* Variables and functions */
int NSS_STATUS_UNAVAIL;
int atoi(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ stat(char *, struct stat *);
size_t strlen(char *);
int unbit_gres(char *, size_t, int, struct group *, char **, size_t *);
enum nss_status _nss_unbit_getgrnam_r(char *name, struct group *res,
                                      char *buffer, size_t buflen,
                                      int *errnop) {
  struct stat st;
  char filename[1024];
  size_t name_len = strlen(name);
  gid_t gid = atoi(name);
  if (gid < 30000)
    return NSS_STATUS_UNAVAIL;
  if (name_len + sizeof("/containers/") >= 1024)
    return NSS_STATUS_UNAVAIL;
  memcpy(filename, "/containers/", sizeof("/containers/"));
  memcpy(filename + (sizeof("/containers/") - 1), name, name_len);
  filename[(sizeof("/containers/") - 1) + name_len] = 0;
  if (stat(filename, &st))
    return NSS_STATUS_UNAVAIL;
  if (st.st_uid != gid)
    return NSS_STATUS_UNAVAIL;
  if (st.st_gid != gid)
    return NSS_STATUS_UNAVAIL;
  res->gr_name = name;
  return unbit_gres(name, name_len, gid, res, &buffer, &buflen);
}


x86 assembly (clang, -O0):
<_nss_unbit_getgrnam_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x44c(%rbp)
cmpl   $0x7530,-0x44c(%rbp)
jge    11af <_nss_unbit_getgrnam_r+0x5f>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <_nss_unbit_getgrnam_r+0x168>
mov    -0x448(%rbp),%rax
add    $0xd,%rax
cmp    $0x400,%rax
jb     11d7 <_nss_unbit_getgrnam_r+0x87>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <_nss_unbit_getgrnam_r+0x168>
lea    -0x440(%rbp),%rdi
lea    0xe1b(%rip),%rsi
mov    $0xd,%edx
call   1060 <memcpy@plt>
lea    -0x440(%rbp),%rdi
add    $0xc,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x448(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x448(%rbp),%rax
add    $0xc,%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1248 <_nss_unbit_getgrnam_r+0xf8>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <_nss_unbit_getgrnam_r+0x168>
mov    -0x38(%rbp),%eax
cmp    -0x44c(%rbp),%eax
je     1268 <_nss_unbit_getgrnam_r+0x118>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <_nss_unbit_getgrnam_r+0x168>
mov    -0x34(%rbp),%eax
cmp    -0x44c(%rbp),%eax
je     1288 <_nss_unbit_getgrnam_r+0x138>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b8 <_nss_unbit_getgrnam_r+0x168>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    -0x44c(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%r8
lea    -0x28(%rbp),%r9
call   1040 <unbit_gres@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_unbit_getgrnam_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,0x18(%rsp)
mov    %rcx,0x10(%rsp)
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <atoi@plt>
cmp    $0x752f,%eax
jle    1203 <_nss_unbit_getgrnam_r+0xb3>
mov    %eax,%ebp
lea    -0x3f3(%r15),%rax
cmp    $0xfffffffffffffbff,%rax
jbe    1203 <_nss_unbit_getgrnam_r+0xb3>
lea    0xe62(%rip),%rsi
lea    0x20(%rsp),%r12
mov    $0xd,%edx
mov    %r12,%rdi
call   1060 <memcpy@plt>
lea    0x2c(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <memcpy@plt>
movb   $0x0,0xc(%r15,%r12,1)
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    1203 <_nss_unbit_getgrnam_r+0xb3>
cmp    %ebp,0x8(%rsp)
jne    1203 <_nss_unbit_getgrnam_r+0xb3>
cmp    %ebp,0xc(%rsp)
jne    1203 <_nss_unbit_getgrnam_r+0xb3>
mov    %rbx,(%r14)
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <unbit_gres@plt>
jmp    120c <_nss_unbit_getgrnam_r+0xbc>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i8* }
%struct.stat = type { i32, i32 }
@NSS_STATUS_UNAVAIL = global i32 0
@.str = [13 x i8] c"/containers/\00"
define i32 @_nss_unbit_getgrnam_r(i8* %0, %struct.group* %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.group*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca %struct.stat
%13 = alloca [1024 x i8]
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %7
store %struct.group* %1, %struct.group** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %14
%18 = load i8*, i8** %7
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %15
%20 = load i32, i32* %15
%21 = icmp slt i32 %20, 30000
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %23, i32* %6
br label %69
24: ; preds = %5
%25 = load i64, i64* %14
%26 = add i64 %25, 13
%27 = icmp uge i64 %26, 1024
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %29, i32* %6
br label %69
30: ; preds = %24
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%32 = call i32 @memcpy(i8* %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%34 = getelementptr inbounds i8, i8* %33, i64 12
%35 = load i8*, i8** %7
%36 = load i64, i64* %14
%37 = call i32 @memcpy(i8* %34, i8* %35, i64 %36)
%38 = load i64, i64* %14
%39 = add i64 12, %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%42 = call i64 @stat(i8* %41, %struct.stat* %12)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %30
%45 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %45, i32* %6
br label %69
46: ; preds = %30
%47 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* %15
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %52, i32* %6
br label %69
53: ; preds = %46
%54 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load i32, i32* %15
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %59, i32* %6
br label %69
60: ; preds = %53
%61 = load i8*, i8** %7
%62 = load %struct.group*, %struct.group** %8
%63 = getelementptr inbounds %struct.group, %struct.group* %62, i32 0, i32 0
store i8* %61, i8** %63
%64 = load i8*, i8** %7
%65 = load i64, i64* %14
%66 = load i32, i32* %15
%67 = load %struct.group*, %struct.group** %8
%68 = call i32 @unbit_gres(i8* %64, i64 %65, i32 %66, %struct.group* %67, i8** %9, i64* %10)
store i32 %68, i32* %6
br label %69
69: ; preds = %60, %58, %51, %44, %28, %22
%70 = load i32, i32* %6
ret i32 %70
}

LLVM IR (-O2):
%struct.group = type { i8* }
%struct.stat = type { i32, i32 }
@NSS_STATUS_UNAVAIL = global i32 0
@.str = [13 x i8] c"/containers/\00"
define i32 @_nss_unbit_getgrnam_r(i8* %0, %struct.group* %1, i8* %2, i64 %3, i32* nocapture readnone %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca [1024 x i8]
store i8* %2, i8** %6
store i64 %3, i64* %7
%10 = bitcast %struct.stat* %8 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%12 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%13 = tail call i32 @atoi(i8* %0)
%14 = icmp slt i32 %13, 30000
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %47
17: ; preds = %5
%18 = add i64 %12, -1011
%19 = icmp ult i64 %18, -1024
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %47
22: ; preds = %17
%23 = call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 12
%25 = call i32 @memcpy(i8* %24, i8* %0, i64 %12)
%26 = add nsw i64 %12, 12
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i64 @stat(i8* %11, %struct.stat* %8)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %47
32: ; preds = %22
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 0
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, %13
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %47
38: ; preds = %32
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 1
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, %13
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %47
44: ; preds = %38
%45 = getelementptr inbounds %struct.group, %struct.group* %1, i64 0, i32 0
store i8* %0, i8** %45
%46 = call i32 @unbit_gres(i8* %0, i64 %12, i32 %13, %struct.group* %1, i8** %6, i64* %7)
br label %47
47: ; preds = %44, %42, %36, %30, %20, %15
%48 = phi i32 [ %16, %15 ], [ %21, %20 ], [ %31, %30 ], [ %37, %36 ], [ %43, %42 ], [ %46, %44 ]
ret i32 %48
}