#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 {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int /*<<< orphan*/ free(scalar_t__ *);
struct group *getgrnam(char *);
scalar_t__ getuid();
scalar_t__ *read_groups();
bool mtdiag_launch_allowed() {
  struct group *gr;
  gid_t *groups;
  int i;
  if (getuid() == 0)
    return true;
  gr = getgrnam("mtdiag-launch");
  if (gr) {
    groups = read_groups();
    if (groups) {
      for (i = 0; groups[i]; ++i) {
        if (groups[i] == gr->gr_gid) {
          free(groups);
          return true;
        }
      }
      free(groups);
    }
  }
  return false;
}


x86 assembly (clang, -O0):
<mtdiag_launch_allowed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
jne    1165 <mtdiag_launch_allowed+0x25>
movl   $0x1,-0x4(%rbp)
jmp    1206 <mtdiag_launch_allowed+0xc6>
lea    0xe94(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ff <mtdiag_launch_allowed+0xbf>
mov    $0x0,%al
call   1050 <read_groups@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fa <mtdiag_launch_allowed+0xba>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11f1 <mtdiag_launch_allowed+0xb1>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jne    11de <mtdiag_launch_allowed+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1206 <mtdiag_launch_allowed+0xc6>
jmp    11e3 <mtdiag_launch_allowed+0xa3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119d <mtdiag_launch_allowed+0x5d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    11ff <mtdiag_launch_allowed+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtdiag_launch_allowed>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
je     11a3 <mtdiag_launch_allowed+0x63>
lea    0xea7(%rip),%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     11b7 <mtdiag_launch_allowed+0x77>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <read_groups@plt>
test   %rax,%rax
je     11b7 <mtdiag_launch_allowed+0x77>
mov    (%rax),%rcx
xor    %ebx,%ebx
test   %rcx,%rcx
je     11af <mtdiag_launch_allowed+0x6f>
mov    (%r14),%rdx
mov    %rax,%rsi
add    $0x8,%rsi
nopl   0x0(%rax,%rax,1)
cmp    %rdx,%rcx
je     11aa <mtdiag_launch_allowed+0x6a>
mov    (%rsi),%rcx
add    $0x8,%rsi
test   %rcx,%rcx
jne    1190 <mtdiag_launch_allowed+0x50>
jmp    11af <mtdiag_launch_allowed+0x6f>
mov    $0x1,%ebx
jmp    11b7 <mtdiag_launch_allowed+0x77>
mov    $0x1,%ebx
mov    %rax,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@.str = [14 x i8] c"mtdiag-launch\00"
define i32 @mtdiag_launch_allowed() {
%1 = alloca i32
%2 = alloca %struct.group*
%3 = alloca i64*
%4 = alloca i32
%5 = call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 1, i32* %1
br label %46
8: ; preds = %0
%9 = call %struct.group* @getgrnam(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store %struct.group* %9, %struct.group** %2
%10 = load %struct.group*, %struct.group** %2
%11 = icmp ne %struct.group* %10, null
br i1 %11, label %12, label %45
12: ; preds = %8
%13 = call i64* (...) @read_groups()
store i64* %13, i64** %3
%14 = load i64*, i64** %3
%15 = icmp ne i64* %14, null
br i1 %15, label %16, label %44
16: ; preds = %12
store i32 0, i32* %4
br label %17
17: ; preds = %38, %16
%18 = load i64*, i64** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i64, i64* %18, i64 %20
%22 = load i64, i64* %21
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %17
%25 = load i64*, i64** %3
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i64, i64* %25, i64 %27
%29 = load i64, i64* %28
%30 = load %struct.group*, %struct.group** %2
%31 = getelementptr inbounds %struct.group, %struct.group* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp eq i64 %29, %32
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i64*, i64** %3
%36 = call i32 @free(i64* %35)
store i32 1, i32* %1
br label %46
37: ; preds = %24
br label %38
38: ; preds = %37
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %17
41: ; preds = %17
%42 = load i64*, i64** %3
%43 = call i32 @free(i64* %42)
br label %44
44: ; preds = %41, %12
br label %45
45: ; preds = %44, %8
store i32 0, i32* %1
br label %46
46: ; preds = %45, %34, %7
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
%struct.group = type { i64 }
@.str = [14 x i8] c"mtdiag-launch\00"
define i32 @mtdiag_launch_allowed() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %27, label %3
3: ; preds = %0
%4 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq %struct.group* %4, null
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i64* (...) @read_groups()
%8 = icmp eq i64* %7, null
br i1 %8, label %27, label %9
9: ; preds = %6
%10 = load i64, i64* %7
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.group, %struct.group* %4, i64 0, i32 0
%14 = load i64, i64* %13
br label %19
15: ; preds = %19
%16 = getelementptr inbounds i64, i64* %7, i64 %23
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %12, %15
%20 = phi i64 [ 0, %12 ], [ %23, %15 ]
%21 = phi i64 [ %10, %12 ], [ %17, %15 ]
%22 = icmp eq i64 %21, %14
%23 = add nuw i64 %20, 1
br i1 %22, label %24, label %15
24: ; preds = %15, %19, %9
%25 = phi i32 [ 0, %9 ], [ 1, %19 ], [ 0, %15 ]
%26 = tail call i32 @free(i64* %7)
br label %27
27: ; preds = %24, %3, %6, %0
%28 = phi i32 [ 1, %0 ], [ 0, %6 ], [ 0, %3 ], [ %25, %24 ]
ret i32 %28
}