#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;

/* Variables and functions */
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ getgid();
scalar_t__ getuid();
int /*<<< orphan*/ server_log(int, char *, char *, int);
scalar_t__ setgid(scalar_t__);
int setuid(scalar_t__);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
void check_privs() {
  uid_t rid, eid;
  rid = getuid();
  if (getgid() == 0)
    rid = 0;
  eid = geteuid();
  if (getegid() == 0)
    eid = 0;
  if (rid != 0 && eid != 0)
    return;
  if (rid == 0 && eid == 0)
    server_log(
        8, "[check_privs] %s:%d: bad privs",
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/master/src/main.c",
        80);
  if (rid == 0) {
    if (setgid(eid) != 0)
      server_log(8, "[check_privs] %s:%d: failed to drop group privs",
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/"
                 "master/src/main.c",
                 85);
    if (setuid(eid) != 0)
      server_log(8, "[check_privs] %s:%d: failed to drop user privs",
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/"
                 "master/src/main.c",
                 87);
  } else {
    if (setgid(rid) != 0)
      server_log(8, "[check_privs] %s:%d: failed to drop group privs",
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/"
                 "master/src/main.c",
                 90);
    if (setuid(rid) != 0)
      server_log(8, "[check_privs] %s:%d: failed to drop user privs",
                 "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                 "outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/"
                 "master/src/main.c",
                 92);
  }
  umask(0);
  if (setuid(0) != -1)
    server_log(
        8, "[check_privs] %s:%d: got root somehow...",
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/master/src/main.c",
        98);
}


x86 assembly (clang, -O0):
<check_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1070 <getgid@plt>
cmp    $0x0,%rax
jne    11ac <check_privs+0x2c>
movq   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getegid@plt>
cmp    $0x0,%rax
jne    11d0 <check_privs+0x50>
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11eb <check_privs+0x6b>
cmpq   $0x0,-0x10(%rbp)
je     11eb <check_privs+0x6b>
jmp    1327 <check_privs+0x1a7>
cmpq   $0x0,-0x8(%rbp)
jne    121e <check_privs+0x9e>
cmpq   $0x0,-0x10(%rbp)
jne    121e <check_privs+0x9e>
mov    $0x8,%edi
lea    0xdf3(%rip),%rsi
lea    0xe0b(%rip),%rdx
mov    $0x50,%ecx
call   1050 <server_log@plt>
cmpq   $0x0,-0x8(%rbp)
jne    128d <check_privs+0x10d>
mov    -0x10(%rbp),%rdi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     1259 <check_privs+0xd9>
mov    $0x8,%edi
lea    0xe5c(%rip),%rsi
lea    0xdd0(%rip),%rdx
mov    $0x55,%ecx
call   1050 <server_log@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <setuid@plt>
cmp    $0x0,%eax
je     1288 <check_privs+0x108>
mov    $0x8,%edi
lea    0xe5d(%rip),%rsi
lea    0xda1(%rip),%rdx
mov    $0x57,%ecx
call   1050 <server_log@plt>
jmp    12f1 <check_privs+0x171>
mov    -0x8(%rbp),%rdi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     12bd <check_privs+0x13d>
mov    $0x8,%edi
lea    0xdf8(%rip),%rsi
lea    0xd6c(%rip),%rdx
mov    $0x5a,%ecx
call   1050 <server_log@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <setuid@plt>
cmp    $0x0,%eax
je     12ec <check_privs+0x16c>
mov    $0x8,%edi
lea    0xdf9(%rip),%rsi
lea    0xd3d(%rip),%rdx
mov    $0x5c,%ecx
call   1050 <server_log@plt>
jmp    12f1 <check_privs+0x171>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <setuid@plt>
cmp    $0xffffffff,%eax
je     1327 <check_privs+0x1a7>
mov    $0x8,%edi
lea    0xded(%rip),%rsi
lea    0xd02(%rip),%rdx
mov    $0x62,%ecx
call   1050 <server_log@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_privs>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getgid@plt>
test   %rax,%rax
cmove  %rax,%rbx
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1080 <getegid@plt>
test   %rax,%rax
cmove  %rax,%r14
test   %rbx,%rbx
je     11c2 <check_privs+0x42>
test   %r14,%r14
jne    127a <check_privs+0xfa>
mov    %rbx,%rax
or     %r14,%rax
jne    11e7 <check_privs+0x67>
lea    0xe2f(%rip),%rsi
lea    0xe47(%rip),%rdx
mov    $0x8,%edi
mov    $0x50,%ecx
call   1050 <server_log@plt>
test   %rbx,%rbx
je     1282 <check_privs+0x102>
mov    %rbx,%rdi
call   1090 <setgid@plt>
test   %rax,%rax
je     121a <check_privs+0x9a>
lea    0xea0(%rip),%rsi
lea    0xe14(%rip),%rdx
mov    $0x8,%edi
mov    $0x5a,%ecx
call   1050 <server_log@plt>
mov    %rbx,%rdi
call   10a0 <setuid@plt>
mov    $0x5c,%ecx
test   %eax,%eax
je     1243 <check_privs+0xc3>
lea    0xea2(%rip),%rsi
lea    0xde6(%rip),%rdx
mov    $0x8,%edi
call   1050 <server_log@plt>
xor    %edi,%edi
call   1060 <umask@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0xffffffff,%eax
je     127a <check_privs+0xfa>
lea    0xea6(%rip),%rsi
lea    0xdbb(%rip),%rdx
mov    $0x8,%edi
mov    $0x62,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <server_log@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
call   1090 <setgid@plt>
test   %rax,%rax
je     12ac <check_privs+0x12c>
lea    0xe0e(%rip),%rsi
lea    0xd82(%rip),%rdx
mov    $0x8,%edi
mov    $0x55,%ecx
call   1050 <server_log@plt>
mov    %r14,%rdi
call   10a0 <setuid@plt>
mov    $0x57,%ecx
test   %eax,%eax
jne    122b <check_privs+0xab>
jmp    1243 <check_privs+0xc3>

LLVM IR (-O0):
@.str = [31 x i8] c"[check_privs] %s:%d: bad privs\00"
@.str.1 = [133 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/master/src/main.c\00"
@.str.2 = [48 x i8] c"[check_privs] %s:%d: failed to drop group privs\00"
@.str.3 = [47 x i8] c"[check_privs] %s:%d: failed to drop user privs\00"
@.str.4 = [41 x i8] c"[check_privs] %s:%d: got root somehow...\00"
define void @check_privs() {
%1 = alloca i64
%2 = alloca i64
%3 = call i64 (...) @getuid()
store i64 %3, i64* %1
%4 = call i64 (...) @getgid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i64 0, i64* %1
br label %7
7: ; preds = %6, %0
%8 = call i64 (...) @geteuid()
store i64 %8, i64* %2
%9 = call i64 (...) @getegid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
store i64 0, i64* %2
br label %12
12: ; preds = %11, %7
%13 = load i64, i64* %1
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* %2
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
br label %62
19: ; preds = %15, %12
%20 = load i64, i64* %1
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i64, i64* %2
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 80)
br label %27
27: ; preds = %25, %22, %19
%28 = load i64, i64* %1
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = load i64, i64* %2
%32 = call i64 @setgid(i64 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 85)
br label %36
36: ; preds = %34, %30
%37 = load i64, i64* %2
%38 = call i32 @setuid(i64 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 87)
br label %42
42: ; preds = %40, %36
br label %56
43: ; preds = %27
%44 = load i64, i64* %1
%45 = call i64 @setgid(i64 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 90)
br label %49
49: ; preds = %47, %43
%50 = load i64, i64* %1
%51 = call i32 @setuid(i64 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 92)
br label %55
55: ; preds = %53, %49
br label %56
56: ; preds = %55, %42
%57 = call i32 @umask(i32 0)
%58 = call i32 @setuid(i64 0)
%59 = icmp ne i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @server_log(i32 8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 98)
br label %62
62: ; preds = %18, %60, %56
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"[check_privs] %s:%d: bad privs\00"
@.str.1 = [133 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/swoops/snakem/refs/heads/master/src/main.c\00"
@.str.2 = [48 x i8] c"[check_privs] %s:%d: failed to drop group privs\00"
@.str.3 = [47 x i8] c"[check_privs] %s:%d: failed to drop user privs\00"
@.str.4 = [41 x i8] c"[check_privs] %s:%d: got root somehow...\00"
define void @check_privs() {
%1 = tail call i64 (...) @getuid()
%2 = tail call i64 (...) @getgid()
%3 = icmp eq i64 %2, 0
%4 = select i1 %3, i64 0, i64 %1
%5 = tail call i64 (...) @geteuid()
%6 = tail call i64 (...) @getegid()
%7 = icmp eq i64 %6, 0
%8 = select i1 %7, i64 0, i64 %5
%9 = icmp ne i64 %4, 0
%10 = icmp ne i64 %8, 0
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %44, label %12
12: ; preds = %0
%13 = icmp eq i64 %4, 0
%14 = icmp eq i64 %8, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @server_log(i32 8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 80)
br label %18
18: ; preds = %16, %12
br i1 %13, label %19, label %27
19: ; preds = %18
%20 = tail call i64 @setgid(i64 %8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @server_log(i32 8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 85)
br label %24
24: ; preds = %22, %19
%25 = tail call i32 @setuid(i64 %8)
%26 = icmp eq i32 %25, 0
br i1 %26, label %38, label %35
27: ; preds = %18
%28 = tail call i64 @setgid(i64 %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @server_log(i32 8, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 90)
br label %32
32: ; preds = %30, %27
%33 = tail call i32 @setuid(i64 %4)
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32, %24
%36 = phi i32 [ 87, %24 ], [ 92, %32 ]
%37 = tail call i32 @server_log(i32 8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35, %32, %24
%39 = tail call i32 @umask(i32 0)
%40 = tail call i32 @setuid(i64 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = tail call i32 @server_log(i32 8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0), i32 98)
br label %44
44: ; preds = %38, %42, %0
ret void
}