#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__ pid_t;
typedef scalar_t__ gid_t;

/* Variables and functions */
scalar_t__ fork();
scalar_t__ getegid();
scalar_t__ geteuid();
scalar_t__ getgid();
scalar_t__ getuid();
int /*<<< orphan*/ sanitize_files();
int /*<<< orphan*/ setgroups(int, scalar_t__ *);
int /*<<< orphan*/ setregid(scalar_t__, scalar_t__);
pid_t sfork() {
  pid_t childpid;
  if ((childpid == fork()) == -1)
    return -1;
  if (childpid != 0)
    return childpid;
  sanitize_files();
  gid_t newgid = getgid(), oldgid = getegid();
  uid_t newuid = getuid(), olduid = geteuid();
  if (!olduid)
    setgroups(1, &newgid);
  if (newgid != oldgid)
    setregid(newgid, newgid);
  if (newuid != olduid)
    setregid(newuid, newuid);
  return 0;
}


x86 assembly (clang, -O0):
<sfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0xffffffff,%eax
jne    11bf <sfork+0x3f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1261 <sfork+0xe1>
cmpq   $0x0,-0x10(%rbp)
je     11d7 <sfork+0x57>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1261 <sfork+0xe1>
mov    $0x0,%al
call   1090 <sanitize_files@plt>
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <getegid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1223 <sfork+0xa3>
mov    $0x1,%edi
lea    -0x18(%rbp),%rsi
call   1050 <setgroups@plt>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     123e <sfork+0xbe>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <setregid@plt>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1259 <sfork+0xd9>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <setregid@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sfork>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <fork@plt>
xor    %eax,%eax
call   1090 <sanitize_files@plt>
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
xor    %eax,%eax
call   1070 <getegid@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %rax,%r12
test   %rax,%rax
je     11ea <sfork+0x6a>
cmp    %r15,%rbx
jne    1200 <sfork+0x80>
cmp    %r12,%r14
je     11dc <sfork+0x5c>
mov    %r14,%rdi
mov    %r14,%rsi
call   1080 <setregid@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsp,%rsi
mov    $0x1,%edi
call   1050 <setgroups@plt>
mov    (%rsp),%rbx
cmp    %r15,%rbx
je     11cc <sfork+0x4c>
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1080 <setregid@plt>
cmp    %r12,%r14
jne    11d1 <sfork+0x51>
jmp    11dc <sfork+0x5c>

LLVM IR (-O0):
define i64 @sfork() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = load i64, i64* %2
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %7, %8
%10 = zext i1 %9 to i32
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %0
store i64 -1, i64* %1
br label %45
13: ; preds = %0
%14 = load i64, i64* %2
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i64, i64* %2
store i64 %17, i64* %1
br label %45
18: ; preds = %13
%19 = call i32 (...) @sanitize_files()
%20 = call i64 (...) @getgid()
store i64 %20, i64* %3
%21 = call i64 (...) @getegid()
store i64 %21, i64* %4
%22 = call i64 (...) @getuid()
store i64 %22, i64* %5
%23 = call i64 (...) @geteuid()
store i64 %23, i64* %6
%24 = load i64, i64* %6
%25 = icmp ne i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %18
%27 = call i32 @setgroups(i32 1, i64* %3)
br label %28
28: ; preds = %26, %18
%29 = load i64, i64* %3
%30 = load i64, i64* %4
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i64, i64* %3
%34 = load i64, i64* %3
%35 = call i32 @setregid(i64 %33, i64 %34)
br label %36
36: ; preds = %32, %28
%37 = load i64, i64* %5
%38 = load i64, i64* %6
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i64, i64* %5
%42 = load i64, i64* %5
%43 = call i32 @setregid(i64 %41, i64 %42)
br label %44
44: ; preds = %40, %36
store i64 0, i64* %1
br label %45
45: ; preds = %44, %16, %12
%46 = load i64, i64* %1
ret i64 %46
}

LLVM IR (-O2):
define i64 @sfork() {
%1 = alloca i64
%2 = tail call i64 (...) @fork()
%3 = tail call i32 (...) @sanitize_files()
%4 = bitcast i64* %1 to i8*
%5 = tail call i64 (...) @getgid()
store i64 %5, i64* %1
%6 = tail call i64 (...) @getegid()
%7 = tail call i64 (...) @getuid()
%8 = tail call i64 (...) @geteuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @setgroups(i32 1, i64* %1)
%12 = load i64, i64* %1
br label %13
13: ; preds = %10, %0
%14 = phi i64 [ %12, %10 ], [ %5, %0 ]
%15 = icmp eq i64 %14, %6
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @setregid(i64 %14, i64 %14)
br label %18
18: ; preds = %16, %13
%19 = icmp eq i64 %7, %8
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @setregid(i64 %7, i64 %7)
br label %22
22: ; preds = %20, %18
ret i64 0
}