#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 scalar_t__ uid_t;
typedef scalar_t__ ssize_t;
typedef scalar_t__ gid_t;
typedef int /*<<< orphan*/ gid_buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ atoi(char *);
int /*<<< orphan*/ cleanup();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getgid();
scalar_t__ getuid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char **, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void check_hugetlb_shm_group(void) {
  int fd;
  ssize_t ret;
  char gid_buffer[64] = {0};
  gid_t hugetlb_shm_group;
  gid_t gid = getgid();
  uid_t uid = getuid();
  if (uid == 0)
    return;
  fd = open("/proc/sys/vm/hugetlb_shm_group", O_RDONLY);
  if (fd < 0)
    fprintf(stderr,
            "ERR: "
            "Unable to open /proc/sys/vm/hugetlb_shm_group: %s",
            strerror(errno));
  ret = read(fd, &gid_buffer, sizeof(gid_buffer));
  if (ret < 0)
    fprintf(stderr,
            "ERR: "
            "Unable to read /proc/sys/vm/hugetlb_shm_group: %s",
            strerror(errno));
  hugetlb_shm_group = atoi(gid_buffer);
  close(fd);
  if (hugetlb_shm_group != gid)
    do {
      cleanup();
      printf("Bad configuration: "
             "Do not have permission to use SHM_HUGETLB"
             "\n");
      exit(1);
    } while (0);
}


x86 assembly (clang, -O0):
<check_hugetlb_shm_group>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   1090 <getgid@plt>
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11fe <check_hugetlb_shm_group+0x3e>
jmp    12dd <check_hugetlb_shm_group+0x11d>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    0xdf2(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    124c <check_hugetlb_shm_group+0x8c>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1298 <check_hugetlb_shm_group+0xd8>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xdc3(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x50(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %rax,-0x58(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x58(%rbp),%rax
cmp    -0x60(%rbp),%rax
je     12dd <check_hugetlb_shm_group+0x11d>
jmp    12c0 <check_hugetlb_shm_group+0x100>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xdbf(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_hugetlb_shm_group>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <getgid@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     1284 <check_hugetlb_shm_group+0xd4>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0xe06(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    122f <check_hugetlb_shm_group+0x7f>
mov    0x2dcc(%rip),%rax
mov    (%rax),%r14d
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdfb(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %rax,%rax
jns    126d <check_hugetlb_shm_group+0xbd>
mov    0x2d8e(%rip),%rax
mov    (%rax),%r14d
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdf4(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    %rsp,%rdi
call   10b0 <atoi@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
cmp    %r15,%rbx
jne    128f <check_hugetlb_shm_group+0xdf>
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1090 <cleanup@plt>
lea    0xdf0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"/proc/sys/vm/hugetlb_shm_group\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [55 x i8] c"ERR: Unable to open /proc/sys/vm/hugetlb_shm_group: %s\00"
@errno = global i32 0
@.str.2 = [55 x i8] c"ERR: Unable to read /proc/sys/vm/hugetlb_shm_group: %s\00"
@.str.3 = [62 x i8] c"Bad configuration: Do not have permission to use SHM_HUGETLB\0A\00"
define void @check_hugetlb_shm_group() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [64 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = bitcast [64 x i8]* %3 to i8*
%8 = call i64 (...) @getgid()
store i64 %8, i64* %5
%9 = call i64 (...) @getuid()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
br label %48
13: ; preds = %0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %18, %13
%24 = load i32, i32* %1
%25 = bitcast [64 x i8]* %3 to i8**
%26 = call i64 @read(i32 %24, i8** %25, i32 64)
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %29, %23
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%36 = call i64 @atoi(i8* %35)
store i64 %36, i64* %4
%37 = load i32, i32* %1
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* %4
%40 = load i64, i64* %5
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %34
br label %43
43: ; preds = %42
%44 = call i32 (...) @cleanup()
%45 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; No predecessors!
br label %48
48: ; preds = %12, %47, %34
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"/proc/sys/vm/hugetlb_shm_group\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [55 x i8] c"ERR: Unable to open /proc/sys/vm/hugetlb_shm_group: %s\00"
@errno = global i32 0
@.str.2 = [55 x i8] c"ERR: Unable to read /proc/sys/vm/hugetlb_shm_group: %s\00"
@str = [61 x i8] c"Bad configuration: Do not have permission to use SHM_HUGETLB\00"
define void @check_hugetlb_shm_group() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @getgid()
%4 = tail call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %32, label %6
6: ; preds = %0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %10, %6
%16 = bitcast [64 x i8]* %1 to i8**
%17 = call i64 @read(i32 %8, i8** %16, i32 64)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %15
%25 = call i64 @atoi(i8* %2)
%26 = tail call i32 @close(i32 %8)
%27 = icmp eq i64 %25, %3
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = tail call i32 (...) @cleanup()
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %24, %0
ret void
}