#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 int /*<<< orphan*/ uid_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fclose(scalar_t__);
int /*<<< orphan*/ fflush(scalar_t__);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ getegid();
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ os_close_files(int, int);
scalar_t__ os_uinfo(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ setegid(int /*<<< orphan*/);
int /*<<< orphan*/ seteuid(int /*<<< orphan*/);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
scalar_t__ stderr;
scalar_t__ stdin;
scalar_t__ stdout;
int os_server_start_2(const char *user, int close_stdfiles) {
  int istat;
  gid_t gid, old_egid, old_gid;
  uid_t uid, old_euid, old_uid;
  if (close_stdfiles) {
    if (stderr) {
      fflush(stderr);
      fclose(stderr);
    } else {
      fsync(2);
      close(2);
    }
    if (stdout) {
      fflush(stdout);
      fclose(stdout);
    } else {
      fsync(1);
      close(1);
    }
    if (stdin)
      fclose(stdin);
    else
      close(0);
    os_close_files(3, 20);
  }
  if (user && *user) {
    if (os_uinfo(&uid, &gid, user))
      return (-1);
    old_euid = geteuid();
    old_uid = getuid();
    old_egid = getegid();
    old_gid = getgid();
    seteuid(0);
    if (setgid(gid)) {
      int save_errno = errno;
      seteuid(old_euid);
      errno = save_errno;
      return (-1);
    }
    if (setuid(uid)) {
      int save_errno = errno;
      seteuid(old_euid);
      setgid(old_gid);
      setegid(old_egid);
      errno = save_errno;
      return (-1);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<os_server_start_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c4 <os_server_start_2+0xe4>
mov    0x2dd8(%rip),%rax
cmpq   $0x0,(%rax)
je     122d <os_server_start_2+0x4d>
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   1090 <fflush@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
jmp    1241 <os_server_start_2+0x61>
mov    $0x2,%edi
call   10b0 <fsync@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2d80(%rip),%rax
cmpq   $0x0,(%rax)
je     1275 <os_server_start_2+0x95>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rdi
call   1090 <fflush@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
jmp    1289 <os_server_start_2+0xa9>
mov    $0x1,%edi
call   10b0 <fsync@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2d58(%rip),%rax
cmpq   $0x0,(%rax)
je     12ae <os_server_start_2+0xce>
mov    0x2d47(%rip),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
jmp    12b5 <os_server_start_2+0xd5>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x3,%edi
mov    $0x14,%esi
call   10e0 <os_close_files@plt>
cmpq   $0x0,-0x10(%rbp)
je     13c6 <os_server_start_2+0x1e6>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     13c6 <os_server_start_2+0x1e6>
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <os_uinfo@plt>
cmp    $0x0,%rax
je     1306 <os_server_start_2+0x126>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <os_server_start_2+0x1ed>
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <getegid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,-0x24(%rbp)
xor    %edi,%edi
call   1100 <seteuid@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <setgid@plt>
cmp    $0x0,%rax
je     1373 <os_server_start_2+0x193>
mov    0x2c82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1100 <seteuid@plt>
mov    -0x34(%rbp),%ecx
mov    0x2c6b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <os_server_start_2+0x1ed>
mov    -0x28(%rbp),%edi
call   10d0 <setuid@plt>
cmp    $0x0,%rax
je     13c1 <os_server_start_2+0x1e1>
mov    0x2c44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1100 <seteuid@plt>
mov    -0x24(%rbp),%edi
call   10c0 <setgid@plt>
mov    -0x20(%rbp),%edi
call   10f0 <setegid@plt>
mov    -0x38(%rbp),%ecx
mov    0x2c1d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <os_server_start_2+0x1ed>
jmp    13c6 <os_server_start_2+0x1e6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_server_start_2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %esi,%esi
je     1286 <os_server_start_2+0xa6>
mov    0x2ddf(%rip),%rbp
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     1212 <os_server_start_2+0x32>
call   1090 <fflush@plt>
mov    0x0(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1226 <os_server_start_2+0x46>
mov    $0x2,%edi
call   10b0 <fsync@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2d9b(%rip),%rbp
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     1246 <os_server_start_2+0x66>
call   1090 <fflush@plt>
mov    0x0(%rbp),%rdi
call   1030 <fclose@plt>
jmp    125a <os_server_start_2+0x7a>
mov    $0x1,%edi
call   10b0 <fsync@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1270 <os_server_start_2+0x90>
call   1030 <fclose@plt>
jmp    1277 <os_server_start_2+0x97>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x3,%edi
mov    $0x14,%esi
call   10e0 <os_close_files@plt>
xor    %ebp,%ebp
test   %rbx,%rbx
je     1340 <os_server_start_2+0x160>
cmpb   $0x0,(%rbx)
je     1340 <os_server_start_2+0x160>
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <os_uinfo@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1340 <os_server_start_2+0x160>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
xor    %eax,%eax
call   10a0 <getegid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getgid@plt>
mov    %eax,%r15d
xor    %edi,%edi
call   1100 <seteuid@plt>
mov    0x4(%rsp),%edi
call   10c0 <setgid@plt>
test   %rax,%rax
je     1305 <os_server_start_2+0x125>
mov    0x2cd6(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1100 <seteuid@plt>
jmp    1332 <os_server_start_2+0x152>
mov    (%rsp),%edi
call   10d0 <setuid@plt>
test   %rax,%rax
je     1340 <os_server_start_2+0x160>
mov    0x2cb7(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1100 <seteuid@plt>
mov    %r15d,%edi
call   10c0 <setgid@plt>
mov    %r14d,%edi
call   10f0 <setegid@plt>
mov    0x2c97(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i64 0
@stdout = global i64 0
@stdin = global i64 0
@errno = global i32 0
define i32 @os_server_start_2(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %49
17: ; preds = %2
%18 = load i64, i64* @stderr
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i64, i64* @stderr
%22 = call i32 @fflush(i64 %21)
%23 = load i64, i64* @stderr
%24 = call i32 @fclose(i64 %23)
br label %28
25: ; preds = %17
%26 = call i32 @fsync(i32 2)
%27 = call i32 @close(i32 2)
br label %28
28: ; preds = %25, %20
%29 = load i64, i64* @stdout
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i64, i64* @stdout
%33 = call i32 @fflush(i64 %32)
%34 = load i64, i64* @stdout
%35 = call i32 @fclose(i64 %34)
br label %39
36: ; preds = %28
%37 = call i32 @fsync(i32 1)
%38 = call i32 @close(i32 1)
br label %39
39: ; preds = %36, %31
%40 = load i64, i64* @stdin
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i64, i64* @stdin
%44 = call i32 @fclose(i64 %43)
br label %47
45: ; preds = %39
%46 = call i32 @close(i32 0)
br label %47
47: ; preds = %45, %42
%48 = call i32 @os_close_files(i32 3, i32 20)
br label %49
49: ; preds = %47, %2
%50 = load i8*, i8** %4
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %90
52: ; preds = %49
%53 = load i8*, i8** %4
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %90
57: ; preds = %52
%58 = load i8*, i8** %4
%59 = call i64 @os_uinfo(i32* %10, i32* %7, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %57
store i32 -1, i32* %3
br label %91
62: ; preds = %57
%63 = call i32 (...) @geteuid()
store i32 %63, i32* %11
%64 = call i32 (...) @getuid()
store i32 %64, i32* %12
%65 = call i32 (...) @getegid()
store i32 %65, i32* %8
%66 = call i32 (...) @getgid()
store i32 %66, i32* %9
%67 = call i32 @seteuid(i32 0)
%68 = load i32, i32* %7
%69 = call i64 @setgid(i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %62
%72 = load i32, i32* @errno
store i32 %72, i32* %13
%73 = load i32, i32* %11
%74 = call i32 @seteuid(i32 %73)
%75 = load i32, i32* %13
store i32 %75, i32* @errno
store i32 -1, i32* %3
br label %91
76: ; preds = %62
%77 = load i32, i32* %10
%78 = call i64 @setuid(i32 %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %89
80: ; preds = %76
%81 = load i32, i32* @errno
store i32 %81, i32* %14
%82 = load i32, i32* %11
%83 = call i32 @seteuid(i32 %82)
%84 = load i32, i32* %9
%85 = call i64 @setgid(i32 %84)
%86 = load i32, i32* %8
%87 = call i32 @setegid(i32 %86)
%88 = load i32, i32* %14
store i32 %88, i32* @errno
store i32 -1, i32* %3
br label %91
89: ; preds = %76
br label %90
90: ; preds = %89, %52, %49
store i32 0, i32* %3
br label %91
91: ; preds = %90, %80, %71, %61
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@stderr = global i64 0
@stdout = global i64 0
@stdin = global i64 0
@errno = global i32 0
define i32 @os_server_start_2(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %1, 0
br i1 %7, label %37, label %8
8: ; preds = %2
%9 = load i64, i64* @stderr
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 @fflush(i64 %9)
%13 = load i64, i64* @stderr
%14 = tail call i32 @fclose(i64 %13)
br label %18
15: ; preds = %8
%16 = tail call i32 @fsync(i32 2)
%17 = tail call i32 @close(i32 2)
br label %18
18: ; preds = %15, %11
%19 = load i64, i64* @stdout
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = tail call i32 @fflush(i64 %19)
%23 = load i64, i64* @stdout
%24 = tail call i32 @fclose(i64 %23)
br label %28
25: ; preds = %18
%26 = tail call i32 @fsync(i32 1)
%27 = tail call i32 @close(i32 1)
br label %28
28: ; preds = %25, %21
%29 = load i64, i64* @stdin
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @fclose(i64 %29)
br label %35
33: ; preds = %28
%34 = tail call i32 @close(i32 0)
br label %35
35: ; preds = %33, %31
%36 = tail call i32 @os_close_files(i32 3, i32 20)
br label %37
37: ; preds = %35, %2
%38 = icmp eq i8* %0, null
br i1 %38, label %68, label %39
39: ; preds = %37
%40 = load i8, i8* %0
%41 = icmp eq i8 %40, 0
br i1 %41, label %68, label %42
42: ; preds = %39
%43 = call i64 @os_uinfo(i32* %4, i32* %3, i8* %0)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %68
45: ; preds = %42
%46 = call i32 (...) @geteuid()
%47 = call i32 (...) @getuid()
%48 = call i32 (...) @getegid()
%49 = call i32 (...) @getgid()
%50 = call i32 @seteuid(i32 0)
%51 = load i32, i32* %3
%52 = call i64 @setgid(i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %45
%55 = load i32, i32* @errno
%56 = call i32 @seteuid(i32 %46)
br label %66
57: ; preds = %45
%58 = load i32, i32* %4
%59 = call i64 @setuid(i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %68, label %61
61: ; preds = %57
%62 = load i32, i32* @errno
%63 = call i32 @seteuid(i32 %46)
%64 = call i64 @setgid(i32 %49)
%65 = call i32 @setegid(i32 %48)
br label %66
66: ; preds = %54, %61
%67 = phi i32 [ %62, %61 ], [ %55, %54 ]
store i32 %67, i32* @errno
br label %68
68: ; preds = %66, %37, %39, %57, %42
%69 = phi i32 [ -1, %42 ], [ 0, %57 ], [ 0, %39 ], [ 0, %37 ], [ -1, %66 ]
ret i32 %69
}