#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 */

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ setegid(unsigned int);
scalar_t__ seteuid(unsigned int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ svz_log(int, char *, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ umask(unsigned int);
int svz_pipe_set_state(unsigned int mask, unsigned int uid, unsigned int gid) {
  umask(mask);
  if (seteuid(uid) < 0) {
    svz_log(1,
            "pipe: "
            "seteuid"
            " (%d): %s\n",
            uid, strerror(errno));
    return -1;
  }
  if (setegid(gid) < 0) {
    svz_log(1,
            "pipe: "
            "setegid"
            " (%d): %s\n",
            gid, strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<svz_pipe_set_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <umask@plt>
mov    -0xc(%rbp),%edi
call   1070 <seteuid@plt>
cmp    $0x0,%rax
jge    11b1 <svz_pipe_set_state+0x61>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe60(%rip),%rsi
call   1050 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <svz_pipe_set_state+0xb0>
mov    -0x10(%rbp),%edi
call   1060 <setegid@plt>
cmp    $0x0,%rax
jge    11f9 <svz_pipe_set_state+0xa9>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xe30(%rip),%rsi
call   1050 <svz_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <svz_pipe_set_state+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svz_pipe_set_state>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
call   1030 <umask@plt>
mov    %ebp,%edi
call   1070 <seteuid@plt>
test   %rax,%rax
js     1197 <svz_pipe_set_state+0x47>
mov    %ebx,%edi
call   1060 <setegid@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11bf <svz_pipe_set_state+0x6f>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe8a(%rip),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
jmp    11b3 <svz_pipe_set_state+0x63>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe54(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
mov    %eax,%ecx
call   1050 <svz_log@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"pipe: seteuid (%d): %s\0A\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"pipe: setegid (%d): %s\0A\00"
define i32 @svz_pipe_set_state(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
%9 = call i32 @umask(i32 %8)
%10 = load i32, i32* %6
%11 = call i64 @seteuid(i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i32, i32* %6
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %16)
store i32 -1, i32* %4
br label %28
18: ; preds = %3
%19 = load i32, i32* %7
%20 = call i64 @setegid(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* %7
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @svz_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %25)
store i32 -1, i32* %4
br label %28
27: ; preds = %18
store i32 0, i32* %4
br label %28
28: ; preds = %27, %22, %13
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@.str = [24 x i8] c"pipe: seteuid (%d): %s\0A\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"pipe: setegid (%d): %s\0A\00"
define i32 @svz_pipe_set_state(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @umask(i32 %0)
%5 = tail call i64 @seteuid(i32 %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @svz_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %9)
br label %18
11: ; preds = %3
%12 = tail call i64 @setegid(i32 %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @svz_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %16)
br label %18
18: ; preds = %11, %14, %7
%19 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %11 ]
ret i32 %19
}