#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, 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;
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");
  ret = read(fd, &gid_buffer, sizeof(gid_buffer));
  if (ret < 0)
    fprintf(stderr, "ERR: "
                    "Unable to read /proc/sys/vm/hugetlb_shm_group");
  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    11ee <check_hugetlb_shm_group+0x3e>
jmp    129f <check_hugetlb_shm_group+0xef>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1225 <check_hugetlb_shm_group+0x75>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%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    125a <check_hugetlb_shm_group+0xaa>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%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     129f <check_hugetlb_shm_group+0xef>
jmp    1282 <check_hugetlb_shm_group+0xd2>
mov    $0x0,%al
call   10a0 <cleanup@plt>
lea    0xdd6(%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   %r14
push   %rbx
sub    $0x40,%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,%r14
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     1244 <check_hugetlb_shm_group+0xa4>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1204 <check_hugetlb_shm_group+0x64>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %rax,%rax
jns    122d <check_hugetlb_shm_group+0x8d>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rsp,%rdi
call   10b0 <atoi@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
cmp    %r14,%rbx
jne    124d <check_hugetlb_shm_group+0xad>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1090 <cleanup@plt>
lea    0xe0b(%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 = [51 x i8] c"ERR: Unable to open /proc/sys/vm/hugetlb_shm_group\00"
@.str.2 = [51 x i8] c"ERR: Unable to read /proc/sys/vm/hugetlb_shm_group\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 %44
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 %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %13
%22 = load i32, i32* %1
%23 = bitcast [64 x i8]* %3 to i8**
%24 = call i64 @read(i32 %22, i8** %23, i32 64)
store i64 %24, i64* %2
%25 = load i64, i64* %2
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %27, %21
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%32 = call i64 @atoi(i8* %31)
store i64 %32, i64* %4
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
%35 = load i64, i64* %4
%36 = load i64, i64* %5
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %30
br label %39
39: ; preds = %38
%40 = call i32 (...) @cleanup()
%41 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %12, %43, %30
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 = [51 x i8] c"ERR: Unable to open /proc/sys/vm/hugetlb_shm_group\00"
@.str.2 = [51 x i8] c"ERR: Unable to read /proc/sys/vm/hugetlb_shm_group\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 %28, 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 %13
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %10, %6
%14 = bitcast [64 x i8]* %1 to i8**
%15 = call i64 @read(i32 %8, i8** %14, i32 64)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %13
%21 = call i64 @atoi(i8* %2)
%22 = tail call i32 @close(i32 %8)
%23 = icmp eq i64 %21, %3
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = tail call i32 (...) @cleanup()
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20, %0
ret void
}