#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 snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int unbit_gres(char *, size_t, int, struct group *, char **, size_t *);
scalar_t__ unbit_magic_buf(char *, size_t, char **, size_t *);
enum nss_status _nss_unbit_getgrgid_r(gid_t gid, struct group *res,
                                      char *buffer, size_t buflen,
                                      int *errnop) {
  struct stat st;
  char filename[1024];
  if (gid < 30000)
    return NSS_STATUS_UNAVAIL;
  int ret = snprintf(filename, 1024,
                     "/containers/"
                     "%d",
                     (int)gid);
  if (ret <= 0 || ret > 1024)
    return NSS_STATUS_UNAVAIL;
  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;
  char *name = filename + (sizeof("/containers/") - 1);
  size_t name_len = ret - (sizeof("/containers/") - 1);
  res->gr_name = buffer;
  if (unbit_magic_buf(name, name_len, &buffer, &buflen))
    return NSS_STATUS_UNAVAIL;
  return unbit_gres(name, name_len, gid, res, &buffer, &buflen);
}


x86 assembly (clang, -O0):
<_nss_unbit_getgrgid_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
cmpl   $0x7530,-0x8(%rbp)
jge    117c <_nss_unbit_getgrgid_r+0x3c>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
lea    -0x430(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x400,%esi
lea    0xe6e(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jle    11ba <_nss_unbit_getgrgid_r+0x7a>
cmpl   $0x400,-0x434(%rbp)
jle    11cb <_nss_unbit_getgrgid_r+0x8b>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
lea    -0x430(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11f6 <_nss_unbit_getgrgid_r+0xb6>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1213 <_nss_unbit_getgrgid_r+0xd3>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1230 <_nss_unbit_getgrgid_r+0xf0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
lea    -0x430(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x440(%rbp)
movslq -0x434(%rbp),%rax
sub    $0xc,%rax
mov    %rax,-0x448(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x440(%rbp),%rdi
mov    -0x448(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1060 <unbit_magic_buf@plt>
cmp    $0x0,%rax
je     1295 <_nss_unbit_getgrgid_r+0x155>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <_nss_unbit_getgrgid_r+0x17a>
mov    -0x440(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%r8
lea    -0x20(%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_getgrgid_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %rdx,0x18(%rsp)
mov    %rcx,0x10(%rsp)
cmp    $0x752f,%edi
jle    11db <_nss_unbit_getgrgid_r+0x9b>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe90(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    %eax,%r15d
lea    -0x401(%r15),%eax
cmp    $0xfffffbff,%eax
jbe    11db <_nss_unbit_getgrgid_r+0x9b>
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11db <_nss_unbit_getgrgid_r+0x9b>
cmp    %ebp,0x8(%rsp)
jne    11db <_nss_unbit_getgrgid_r+0x9b>
cmp    %ebp,0xc(%rsp)
jne    11db <_nss_unbit_getgrgid_r+0x9b>
lea    0x2c(%rsp),%r12
mov    %r15d,%r15d
add    $0xfffffffffffffff4,%r15
mov    %rbx,(%r14)
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <unbit_magic_buf@plt>
test   %rax,%rax
je     11f4 <_nss_unbit_getgrgid_r+0xb4>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r14,%rcx
call   1040 <unbit_gres@plt>
jmp    11e4 <_nss_unbit_getgrgid_r+0xa4>

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

LLVM IR (-O2):
%struct.group = type { i8* }
%struct.stat = type { i32, i32 }
@NSS_STATUS_UNAVAIL = global i32 0
@.str = [15 x i8] c"/containers/%d\00"
define i32 @_nss_unbit_getgrgid_r(i32 %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 = icmp slt i32 %0, 30000
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
15: ; preds = %5
%16 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = add i32 %16, -1025
%18 = icmp ult i32 %17, -1024
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
21: ; preds = %15
%22 = call i64 @stat(i8* %11, %struct.stat* %8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 0
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, %0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
32: ; preds = %26
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 1
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, %0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
38: ; preds = %32
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 12
%40 = zext i32 %16 to i64
%41 = add nsw i64 %40, -12
%42 = getelementptr inbounds %struct.group, %struct.group* %1, i64 0, i32 0
store i8* %2, i8** %42
%43 = call i64 @unbit_magic_buf(i8* %39, i64 %41, i8** %6, i64* %7)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %38
%46 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %49
47: ; preds = %38
%48 = call i32 @unbit_gres(i8* %39, i64 %41, i32 %0, %struct.group* %1, i8** %6, i64* %7)
br label %49
49: ; preds = %19, %24, %30, %36, %47, %45, %13
%50 = phi i32 [ %14, %13 ], [ %20, %19 ], [ %25, %24 ], [ %31, %30 ], [ %37, %36 ], [ %46, %45 ], [ %48, %47 ]
ret i32 %50
}