#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*/ EPERM;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, int, ...);
int setuid(int /*<<< orphan*/);
void setuid_failure(int phase) {
  int ret = setuid(0);
  switch (ret) {
  case 0:
    do {
      printf("FAIL: "
             "setuid succeeded unexpectedly in phase %d"
             "\n",
             phase);
      _exit(1);
    } while (0);
  case -1:
    if (errno != EPERM)
      do {
        printf("FAIL: "
               "setuid phase %d"
               ": %m\n",
               phase);
        _exit(1);
      } while (0);
    break;
  default:
    do {
      printf("FAIL: "
             "invalid setuid return value in phase %d: %d"
             "\n",
             phase, ret);
      _exit(1);
    } while (0);
  }
}


x86 assembly (clang, -O0):
<setuid_failure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
xor    %edi,%edi
call   1050 <setuid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0xffffffff,%eax
je     1189 <setuid_failure+0x59>
jmp    1159 <setuid_failure+0x29>
mov    -0xc(%rbp),%eax
test   %eax,%eax
jne    11c6 <setuid_failure+0x96>
jmp    1169 <setuid_failure+0x39>
jmp    116e <setuid_failure+0x3e>
mov    -0x4(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
je     11c1 <setuid_failure+0x91>
jmp    11a6 <setuid_failure+0x76>
mov    -0x4(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    11e9 <setuid_failure+0xb9>
jmp    11cb <setuid_failure+0x9b>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setuid_failure>:
push   %rbx
mov    %edi,%ebx
xor    %edi,%edi
call   1050 <setuid@plt>
cmp    $0xffffffff,%eax
jne    1155 <setuid_failure+0x25>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%eax
jne    1162 <setuid_failure+0x32>
pop    %rbx
ret
test   %eax,%eax
jne    117c <setuid_failure+0x4c>
lea    0xea0(%rip),%rdi
jmp    1169 <setuid_failure+0x39>
lea    0xec8(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    0xec9(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"FAIL: setuid succeeded unexpectedly in phase %d\0A\00"
@errno = global i32 0
@EPERM = global i32 0
@.str.1 = [27 x i8] c"FAIL: setuid phase %d: %m\0A\00"
@.str.2 = [51 x i8] c"FAIL: invalid setuid return value in phase %d: %d\0A\00"
define void @setuid_failure(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = call i32 @setuid(i32 0)
store i32 %4, i32* %3
%5 = load i32, i32* %3
switch i32 %5, label %23 [
i32 0, label %6
i32 -1, label %12
]
6: ; preds = %1
br label %7
7: ; preds = %6
%8 = load i32, i32* %2
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @_exit(i32 1)
unreachable
11: ; No predecessors!
br label %12
12: ; preds = %1, %11
%13 = load i32, i32* @errno
%14 = load i32, i32* @EPERM
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
br label %17
17: ; preds = %16
%18 = load i32, i32* %2
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @_exit(i32 1)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %12
br label %30
23: ; preds = %1
br label %24
24: ; preds = %23
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26)
%28 = call i32 @_exit(i32 1)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %22
ret void
}

LLVM IR (-O2):
@.str = [49 x i8] c"FAIL: setuid succeeded unexpectedly in phase %d\0A\00"
@errno = global i32 0
@EPERM = global i32 0
@.str.1 = [27 x i8] c"FAIL: setuid phase %d: %m\0A\00"
@.str.2 = [51 x i8] c"FAIL: invalid setuid return value in phase %d: %d\0A\00"
define void @setuid_failure(i32 %0) {
%2 = tail call i32 @setuid(i32 0)
switch i32 %2, label %13 [
i32 0, label %3
i32 -1, label %6
]
3: ; preds = %1
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 @_exit(i32 1)
unreachable
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = load i32, i32* @EPERM
%9 = icmp eq i32 %7, %8
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%12 = tail call i32 @_exit(i32 1)
unreachable
13: ; preds = %1
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %2)
%15 = tail call i32 @_exit(i32 1)
unreachable
16: ; preds = %6
ret void
}