#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;
struct passwd {
  scalar_t__ pw_name;
};
struct group {
  scalar_t__ gr_mem;
};

/* Variables and functions */
struct group *getgrnam(char *);
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
scalar_t__ strcmp(char const *, char const *);
int userIsPrivileged(void) {
  int st = 0;
  const char **entry;
  const char *name;
  uid_t uid = getuid();
  struct group *grp;
  struct passwd *pass;
  if (uid == 0) {
    return 1;
  }
  pass = getpwuid(uid);
  if (pass == NULL) {
    return 0;
  }
  grp = getgrnam("openvpn-cli");
  if (grp == NULL) {
    return 0;
  }
  name = (const char *)pass->pw_name;
  entry = (const char **)grp->gr_mem;
  while (*entry != NULL) {
    if (strcmp(*entry, name) == 0) {
      st = 1;
      break;
    } else {
      entry++;
    }
  }
  return st;
}


x86 assembly (clang, -O0):
<userIsPrivileged>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1171 <userIsPrivileged+0x31>
movl   $0x1,-0x4(%rbp)
jmp    121d <userIsPrivileged+0xdd>
mov    -0x20(%rbp),%rdi
call   1030 <getpwuid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1195 <userIsPrivileged+0x55>
movl   $0x0,-0x4(%rbp)
jmp    121d <userIsPrivileged+0xdd>
lea    0xe64(%rip),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bc <userIsPrivileged+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    121d <userIsPrivileged+0xdd>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1217 <userIsPrivileged+0xd7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1206 <userIsPrivileged+0xc6>
movl   $0x1,-0x8(%rbp)
jmp    1217 <userIsPrivileged+0xd7>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    11d2 <userIsPrivileged+0x92>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<userIsPrivileged>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11ad <userIsPrivileged+0x6d>
mov    %rax,%rdi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     11aa <userIsPrivileged+0x6a>
mov    %rax,%rbx
lea    0xe93(%rip),%rdi
call   1060 <getgrnam@plt>
test   %rax,%rax
je     11aa <userIsPrivileged+0x6a>
mov    (%rax),%rbp
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     11aa <userIsPrivileged+0x6a>
mov    (%rbx),%rbx
add    $0x8,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11ad <userIsPrivileged+0x6d>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1190 <userIsPrivileged+0x50>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.passwd = type { i64 }
%struct.group = type { i64 }
@.str = [12 x i8] c"openvpn-cli\00"
define i32 @userIsPrivileged() {
%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.passwd* @getpwuid(i64 %1)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call %struct.group* @getgrnam(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq %struct.group* %7, null
br i1 %8, label %27, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%11 = load i64, i64* %10
%12 = inttoptr i64 %11 to i8*
%13 = getelementptr inbounds %struct.group, %struct.group* %7, i64 0, i32 0
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to i8**
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %27, label %21
18: ; preds = %21
%19 = load i8*, i8** %26
%20 = icmp eq i8* %19, null
br i1 %20, label %27, label %21
21: ; preds = %9, %18
%22 = phi i8* [ %19, %18 ], [ %16, %9 ]
%23 = phi i8** [ %26, %18 ], [ %15, %9 ]
%24 = tail call i64 @strcmp(i8* %22, i8* %12)
%25 = icmp eq i64 %24, 0
%26 = getelementptr inbounds i8*, i8** %23, i64 1
br i1 %25, label %27, label %18
27: ; preds = %21, %18, %9, %6, %3, %0
%28 = phi i32 [ 1, %0 ], [ 0, %3 ], [ 0, %6 ], [ 0, %9 ], [ 1, %21 ], [ 0, %18 ]
ret i32 %28
}