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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int geteuid();
int /*<<< orphan*/ printf(char *, ...);
int seteuid(int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  uid_t start = geteuid();
  uid_t target = start != 65534 ? 65534 : 65533;
  int result;
  fprintf(stderr, "current EUID=%u, attempting EUID=%u\n", start, target);
  result = seteuid(target);
  if (result == 0) {
    printf("RISK\tsetuid(2) succeeded for EUID=%u\n", target);
    return 1;
  } else if (errno == EINVAL) {
    printf("SAFE\tsetuid(2) failed as expected with EINVAL\n");
    return 0;
  }
  printf("ERROR\tsetuid(2) failed unexpectedly with errno=%d\n", errno);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    $0xfffd,%eax
mov    $0xfffe,%ecx
cmp    $0xfffe,%edx
cmovne %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1060 <seteuid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11c6 <main+0x86>
mov    -0x18(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121e <main+0xde>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <main+0xba>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121e <main+0xde>
jmp    11ff <main+0xbf>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1050 <geteuid@plt>
xor    %ecx,%ecx
cmp    $0xfffe,%eax
sete   %cl
mov    $0xfffe,%ebx
sub    %ecx,%ebx
mov    0x2e70(%rip),%rcx
mov    (%rcx),%edi
lea    0xe87(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1070 <seteuid@plt>
test   %eax,%eax
je     11b3 <main+0x63>
mov    0x2e44(%rip),%rax
mov    (%rax),%rsi
mov    0x2e32(%rip),%rax
cmp    (%rax),%rsi
jne    11c5 <main+0x75>
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d3 <main+0x83>
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"current EUID=%u, attempting EUID=%u\0A\00"
@.str.1 = [38 x i8] c"RISK\09setuid(2) succeeded for EUID=%u\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [47 x i8] c"SAFE\09setuid(2) failed as expected with EINVAL\0A\00"
@.str.3 = [51 x i8] c"ERROR\09setuid(2) failed unexpectedly with errno=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @geteuid()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 65534
%12 = zext i1 %11 to i64
%13 = select i1 %11, i32 65534, i32 65533
store i32 %13, i32* %7
%14 = load i32, i32* @stderr
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32, i32* %7
%19 = call i32 @seteuid(i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 1, i32* %3
br label %35
25: ; preds = %2
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINVAL
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %35
31: ; preds = %25
br label %32
32: ; preds = %31
%33 = load i64, i64* @errno
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i64 %33)
store i32 1, i32* %3
br label %35
35: ; preds = %32, %29, %22
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"current EUID=%u, attempting EUID=%u\0A\00"
@.str.1 = [38 x i8] c"RISK\09setuid(2) succeeded for EUID=%u\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.3 = [51 x i8] c"ERROR\09setuid(2) failed unexpectedly with errno=%d\0A\00"
@str = [46 x i8] c"SAFE\09setuid(2) failed as expected with EINVAL\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @geteuid()
%4 = icmp eq i32 %3, 65534
%5 = select i1 %4, i32 65533, i32 65534
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %5)
%8 = tail call i32 @seteuid(i32 %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %20
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINVAL
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %20
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i64 %13)
br label %20
20: ; preds = %18, %16, %10
%21 = phi i32 [ 1, %10 ], [ 0, %16 ], [ 1, %18 ]
ret i32 %21
}