#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_mode;
  scalar_t__ st_uid;
  scalar_t__ st_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int NGROUPS_MAX;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ S_ISCHR(int);
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
scalar_t__ geteuid();
int getgroups(int, scalar_t__ *);
int stat(char const *, struct stat *);
int checkcam(const char *path) {
  struct stat st;
  gid_t grps[NGROUPS_MAX];
  int grps_count = -1, i;
  if (stat(path, &st) == -1 || !S_ISCHR(st.st_mode) ||
      (grps_count = getgroups(NGROUPS_MAX, grps)) == -1)
    return 0;
  if (((S_IROTH | S_IWOTH) & st.st_mode) == (S_IROTH | S_IWOTH) ||
      (geteuid() == st.st_uid &&
       ((S_IRUSR | S_IWUSR) & st.st_mode) == (S_IRUSR | S_IWUSR)))
    return 1;
  if (((S_IRGRP | S_IWGRP) & st.st_mode) == (S_IRGRP | S_IWGRP))
    for (i = 0; i < grps_count; i++)
      if (grps[i] == st.st_gid)
        return 1;
  return 0;
}


x86 assembly (clang, -O0):
<checkcam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11c5 <checkcam+0x85>
mov    -0x28(%rbp),%edi
call   1060 <S_ISCHR@plt>
cmp    $0x0,%eax
je     11c5 <checkcam+0x85>
mov    -0x50(%rbp),%rsi
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1050 <getgroups@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11d8 <checkcam+0x98>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12eb <checkcam+0x1ab>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
and    -0x28(%rbp),%eax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dab(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
je     1247 <checkcam+0x107>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    -0x20(%rbp),%rax
jne    125a <checkcam+0x11a>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    0x2db8(%rip),%rcx
or     (%rcx),%eax
and    -0x28(%rbp),%eax
mov    0x2d84(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2da3(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    125a <checkcam+0x11a>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12eb <checkcam+0x1ab>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
and    -0x28(%rbp),%eax
mov    0x2d52(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d69(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    12dd <checkcam+0x19d>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    12d8 <checkcam+0x198>
mov    -0x50(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
cmp    -0x18(%rbp),%rax
jne    12c5 <checkcam+0x185>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12eb <checkcam+0x1ab>
jmp    12ca <checkcam+0x18a>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1290 <checkcam+0x150>
jmp    12dd <checkcam+0x19d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkcam>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r13
mov    0x2e7d(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%r14
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x40(%rbp),%rsi
call   1040 <stat@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11c7 <checkcam+0x87>
mov    -0x40(%rbp),%r15d
mov    %r15d,%edi
call   1060 <S_ISCHR@plt>
test   %eax,%eax
je     11c7 <checkcam+0x87>
mov    (%r12),%edi
mov    %r14,%rsi
call   1050 <getgroups@plt>
cmp    $0xffffffff,%eax
je     11c7 <checkcam+0x87>
mov    %eax,%r12d
mov    0x2e11(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %r15d
test   %r15d,%ecx
jne    11db <checkcam+0x9b>
mov    $0x1,%ebx
mov    %r13,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    -0x38(%rbp),%rax
jne    1201 <checkcam+0xc1>
mov    0x2dc7(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %r15d,%ecx
je     11c2 <checkcam+0x82>
mov    0x2dc0(%rip),%rax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %r15d,%ecx
jne    11c7 <checkcam+0x87>
test   %r12d,%r12d
jle    11c7 <checkcam+0x87>
mov    -0x30(%rbp),%rax
mov    %r12d,%ecx
xor    %edx,%edx
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
cmp    %rax,(%r14,%rdx,8)
je     11c2 <checkcam+0x82>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    1230 <checkcam+0xf0>
jmp    11c7 <checkcam+0x87>

LLVM IR (-O0):
%struct.stat = type { i32, i64, i64 }
@NGROUPS_MAX = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @checkcam(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @NGROUPS_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i64, i64 %11
store i64 %11, i64* %6
store i32 -1, i32* %7
%14 = load i8*, i8** %3
%15 = call i32 @stat(i8* %14, %struct.stat* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %26, label %17
17: ; preds = %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISCHR(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @NGROUPS_MAX
%24 = call i32 @getgroups(i32 %23, i64* %13)
store i32 %24, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22, %17, %1
store i32 0, i32* %2
store i32 1, i32* %9
br label %86
27: ; preds = %22
%28 = load i32, i32* @S_IROTH
%29 = load i32, i32* @S_IWOTH
%30 = or i32 %28, %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = and i32 %30, %32
%34 = load i32, i32* @S_IROTH
%35 = load i32, i32* @S_IWOTH
%36 = or i32 %34, %35
%37 = icmp eq i32 %33, %36
br i1 %37, label %54, label %38
38: ; preds = %27
%39 = call i64 (...) @geteuid()
%40 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%41 = load i64, i64* %40
%42 = icmp eq i64 %39, %41
br i1 %42, label %43, label %55
43: ; preds = %38
%44 = load i32, i32* @S_IRUSR
%45 = load i32, i32* @S_IWUSR
%46 = or i32 %44, %45
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = and i32 %46, %48
%50 = load i32, i32* @S_IRUSR
%51 = load i32, i32* @S_IWUSR
%52 = or i32 %50, %51
%53 = icmp eq i32 %49, %52
br i1 %53, label %54, label %55
54: ; preds = %43, %27
store i32 1, i32* %2
store i32 1, i32* %9
br label %86
55: ; preds = %43, %38
%56 = load i32, i32* @S_IRGRP
%57 = load i32, i32* @S_IWGRP
%58 = or i32 %56, %57
%59 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%60 = load i32, i32* %59
%61 = and i32 %58, %60
%62 = load i32, i32* @S_IRGRP
%63 = load i32, i32* @S_IWGRP
%64 = or i32 %62, %63
%65 = icmp eq i32 %61, %64
br i1 %65, label %66, label %85
66: ; preds = %55
store i32 0, i32* %8
br label %67
67: ; preds = %81, %66
%68 = load i32, i32* %8
%69 = load i32, i32* %7
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %84
71: ; preds = %67
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i64, i64* %13, i64 %73
%75 = load i64, i64* %74
%76 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%77 = load i64, i64* %76
%78 = icmp eq i64 %75, %77
br i1 %78, label %79, label %80
79: ; preds = %71
store i32 1, i32* %2
store i32 1, i32* %9
br label %86
80: ; preds = %71
br label %81
81: ; preds = %80
%82 = load i32, i32* %8
%83 = add nsw i32 %82, 1
store i32 %83, i32* %8
br label %67
84: ; preds = %67
br label %85
85: ; preds = %84, %55
store i32 0, i32* %2
store i32 1, i32* %9
br label %86
86: ; preds = %85, %79, %54, %26
%87 = load i8*, i8** %5
%88 = load i32, i32* %2
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32, i64, i64 }
@NGROUPS_MAX = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
define i32 @checkcam(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @NGROUPS_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i64, i64 %5
%8 = call i32 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %56, label %10
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @S_ISCHR(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %56, label %15
15: ; preds = %10
%16 = load i32, i32* @NGROUPS_MAX
%17 = call i32 @getgroups(i32 %16, i64* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %56, label %19
19: ; preds = %15
%20 = load i32, i32* @S_IROTH
%21 = load i32, i32* @S_IWOTH
%22 = or i32 %21, %20
%23 = and i32 %22, %12
%24 = icmp eq i32 %23, %22
br i1 %24, label %56, label %25
25: ; preds = %19
%26 = call i64 (...) @geteuid()
%27 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%28 = load i64, i64* %27
%29 = icmp eq i64 %26, %28
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %32, %31
%34 = and i32 %33, %12
%35 = icmp eq i32 %34, %33
br i1 %35, label %56, label %36
36: ; preds = %30, %25
%37 = load i32, i32* @S_IRGRP
%38 = load i32, i32* @S_IWGRP
%39 = or i32 %38, %37
%40 = and i32 %39, %12
%41 = icmp eq i32 %40, %39
%42 = icmp sgt i32 %17, 0
%43 = select i1 %41, i1 %42, i1 false
br i1 %43, label %44, label %56
44: ; preds = %36
%45 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%46 = load i64, i64* %45
%47 = zext i32 %17 to i64
br label %50
48: ; preds = %50
%49 = icmp eq i64 %55, %47
br i1 %49, label %56, label %50
50: ; preds = %44, %48
%51 = phi i64 [ 0, %44 ], [ %55, %48 ]
%52 = getelementptr inbounds i64, i64* %7, i64 %51
%53 = load i64, i64* %52
%54 = icmp eq i64 %53, %46
%55 = add nuw nsw i64 %51, 1
br i1 %54, label %56, label %48
56: ; preds = %50, %48, %36, %19, %30, %1, %10, %15
%57 = phi i32 [ 0, %15 ], [ 0, %10 ], [ 0, %1 ], [ 1, %30 ], [ 1, %19 ], [ 0, %36 ], [ 1, %50 ], [ 0, %48 ]
ret i32 %57
}