#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 */
struct test {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int /*<<< orphan*/ expect(char *, int, int, int /*<<< orphan*/);
int setuid(int);
void priv_cred_setuid(int asroot, int injail, struct test *test) {
  int error;
  error = setuid(200);
  if (asroot && injail)
    expect("priv_setuid(asroot, injail)", error, 0, 0);
  if (asroot && !injail)
    expect("priv_setuid(asroot, !injail)", error, 0, 0);
  if (!asroot && injail)
    expect("priv_setuid(!asroot, injail)", error, -1, EPERM);
  if (!asroot && !injail)
    expect("priv_setuid(!asroot, !injail)", error, -1, EPERM);
}


x86 assembly (clang, -O0):
<priv_cred_setuid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    $0xc8,%edi
call   1040 <setuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1166 <priv_cred_setuid+0x46>
cmpl   $0x0,-0x8(%rbp)
je     1166 <priv_cred_setuid+0x46>
mov    -0x14(%rbp),%esi
lea    0xea3(%rip),%rdi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
je     118d <priv_cred_setuid+0x6d>
cmpl   $0x0,-0x8(%rbp)
jne    118d <priv_cred_setuid+0x6d>
mov    -0x14(%rbp),%esi
lea    0xe98(%rip),%rdi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11be <priv_cred_setuid+0x9e>
cmpl   $0x0,-0x8(%rbp)
je     11be <priv_cred_setuid+0x9e>
mov    -0x14(%rbp),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
lea    0xe85(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11ef <priv_cred_setuid+0xcf>
cmpl   $0x0,-0x8(%rbp)
jne    11ef <priv_cred_setuid+0xcf>
mov    -0x14(%rbp),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
lea    0xe71(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<priv_cred_setuid>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
mov    $0xc8,%edi
call   1040 <setuid@plt>
mov    %eax,%r14d
test   %ebx,%ebx
je     1150 <priv_cred_setuid+0x30>
test   %ebp,%ebp
je     1150 <priv_cred_setuid+0x30>
lea    0xebc(%rip),%rdi
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <expect@plt>
test   %ebx,%ebx
je     116b <priv_cred_setuid+0x4b>
test   %ebp,%ebp
jne    116b <priv_cred_setuid+0x4b>
lea    0xebd(%rip),%rdi
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <expect@plt>
test   %ebx,%ebx
jne    1190 <priv_cred_setuid+0x70>
test   %ebp,%ebp
je     1190 <priv_cred_setuid+0x70>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
lea    0xeb6(%rip),%rdi
mov    %r14d,%esi
mov    $0xffffffff,%edx
call   1030 <expect@plt>
or     %ebx,%ebp
je     1199 <priv_cred_setuid+0x79>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
lea    0xead(%rip),%rdi
mov    %r14d,%esi
mov    $0xffffffff,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <expect@plt>

LLVM IR (-O0):
%struct.test = type { i32 }
@.str = [28 x i8] c"priv_setuid(asroot, injail)\00"
@.str.1 = [29 x i8] c"priv_setuid(asroot, !injail)\00"
@.str.2 = [29 x i8] c"priv_setuid(!asroot, injail)\00"
@EPERM = global i32 0
@.str.3 = [30 x i8] c"priv_setuid(!asroot, !injail)\00"
define void @priv_cred_setuid(i32 %0, i32 %1, %struct.test* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.test*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.test* %2, %struct.test** %6
%8 = call i32 @setuid(i32 200)
store i32 %8, i32* %7
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %7
%16 = call i32 @expect(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %15, i32 0, i32 0)
br label %17
17: ; preds = %14, %11, %3
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* %7
%25 = call i32 @expect(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 0, i32 0)
br label %26
26: ; preds = %23, %20, %17
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %26
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* %7
%34 = load i32, i32* @EPERM
%35 = call i32 @expect(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 -1, i32 %34)
br label %36
36: ; preds = %32, %29, %26
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %39
%43 = load i32, i32* %7
%44 = load i32, i32* @EPERM
%45 = call i32 @expect(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %43, i32 -1, i32 %44)
br label %46
46: ; preds = %42, %39, %36
ret void
}

LLVM IR (-O2):
%struct.test = type { i32 }
@.str = [28 x i8] c"priv_setuid(asroot, injail)\00"
@.str.1 = [29 x i8] c"priv_setuid(asroot, !injail)\00"
@.str.2 = [29 x i8] c"priv_setuid(!asroot, injail)\00"
@EPERM = global i32 0
@.str.3 = [30 x i8] c"priv_setuid(!asroot, !injail)\00"
define void @priv_cred_setuid(i32 %0, i32 %1, %struct.test* nocapture readnone %2) {
%4 = tail call i32 @setuid(i32 200)
%5 = icmp ne i32 %0, 0
%6 = icmp ne i32 %1, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @expect(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0, i32 0)
br label %10
10: ; preds = %8, %3
%11 = icmp eq i32 %0, 0
%12 = or i1 %11, %6
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @expect(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 0, i32 0)
br label %15
15: ; preds = %13, %10
%16 = and i1 %11, %6
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @EPERM
%19 = tail call i32 @expect(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 -1, i32 %18)
br label %20
20: ; preds = %17, %15
%21 = or i32 %1, %0
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @EPERM
%25 = tail call i32 @expect(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 -1, i32 %24)
br label %26
26: ; preds = %23, %20
ret void
}