#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int cleanup();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ geteuid();
int /*<<< orphan*/ perror(char *);
int restoreprivilege();
int setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
int setresgid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setresuid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup();
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int error, errorseen;
  if (geteuid() != 0) {
    fprintf(stderr, "testaccess must run as root.\n");
    exit(EXIT_FAILURE);
  }
  error = setup();
  if (error) {
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = setresuid((uid_t)0, (uid_t)0, (uid_t)0);
  if (error) {
    perror("setresuid");
    cleanup();
  }
  error = setgroups(0, NULL);
  if (error) {
    perror("setgroups");
    cleanup();
  }
  error = setresgid((gid_t)0, (gid_t)0, (gid_t)0);
  if (error) {
    perror("setresgid");
    cleanup();
  }
  error = setresuid((uid_t)500, (uid_t)500, (uid_t)0);
  if (error) {
    perror("setresuid.1");
    cleanup();
    exit(EXIT_FAILURE);
  }
  errorseen = 0;
  error = access("test1", R_OK);
  if (!error) {
    fprintf(stderr, "saved uid used instead of real uid\n");
    errorseen++;
  }
  error = restoreprivilege();
  if (error) {
    perror("restoreprivilege");
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = setresuid((uid_t)500, (uid_t)501, (uid_t)0);
  if (error) {
    perror("setresid.2");
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = access("test2", R_OK);
  if (error) {
    fprintf(stderr,
            "Effective uid was used instead of real uid in access().\n");
    errorseen++;
  }
  error = access("test3", R_OK);
  if (!error) {
    fprintf(stderr,
            "Effective uid was used instead of real uid in access().\n");
    errorseen++;
  }
  error = restoreprivilege();
  if (error) {
    perror("restoreprivilege");
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = setresgid((gid_t)500, (gid_t)501, (gid_t)0);
  if (error) {
    perror("setresgid.1");
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = setresuid((uid_t)500, (uid_t)500, (uid_t)0);
  if (error) {
    perror("setresuid.3");
    cleanup();
    exit(EXIT_FAILURE);
  }
  error = access("test4", R_OK);
  if (!error) {
    fprintf(stderr, "saved gid used instead of real gid\n");
  }
  error = access("test5", R_OK);
  if (error) {
    fprintf(stderr,
            "Effective gid was used instead of real gid in access().\n");
    errorseen++;
  }
  error = access("test6", R_OK);
  if (!error) {
    fprintf(stderr,
            "Effective gid was used instead of real gid in access().\n");
    errorseen++;
  }
  fprintf(stderr, "%d errors seen.\n", errorseen);
  error = cleanup();
  if (error) {
    perror("cleanup");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


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   1060 <geteuid@plt>
cmp    $0x0,%rax
je     11fc <main+0x4c>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <setup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1225 <main+0x75>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <setresuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1250 <main+0xa0>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <setgroups@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     127b <main+0xcb>
lea    0xdb9(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1050 <setresgid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12a6 <main+0xf6>
lea    0xd98(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    $0x1f4,%esi
xor    %edx,%edx
mov    %esi,%edi
call   1030 <setresuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12e2 <main+0x132>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    0xd4f(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    132b <main+0x17b>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <restoreprivilege@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1360 <main+0x1b0>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x1f4,%edi
mov    $0x1f5,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     139f <main+0x1ef>
lea    0xcfe(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c2a(%rip),%rax
mov    (%rax),%esi
lea    0xcdf(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     13e1 <main+0x231>
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
lea    0xcc3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    0x2be8(%rip),%rax
mov    (%rax),%esi
lea    0xcdc(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1423 <main+0x273>
mov    0x2bd6(%rip),%rax
mov    (%rax),%edi
lea    0xc81(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <restoreprivilege@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1458 <main+0x2a8>
lea    0xc34(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2b87(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x1f4,%edi
mov    $0x1f5,%esi
xor    %edx,%edx
call   1050 <setresgid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1497 <main+0x2e7>
lea    0xc56(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2b48(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x1f4,%esi
xor    %edx,%edx
mov    %esi,%edi
call   1030 <setresuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     14d3 <main+0x323>
lea    0xc26(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    0x2b0c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2af6(%rip),%rax
mov    (%rax),%esi
lea    0xc08(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    150c <main+0x35c>
mov    0x2ae4(%rip),%rax
mov    (%rax),%edi
lea    0xbec(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2abd(%rip),%rax
mov    (%rax),%esi
lea    0xbf9(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     154e <main+0x39e>
mov    0x2aab(%rip),%rax
mov    (%rax),%edi
lea    0xbdd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    0x2a7b(%rip),%rax
mov    (%rax),%esi
lea    0xbf6(%rip),%rdi
call   10a0 <access@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1590 <main+0x3e0>
mov    0x2a69(%rip),%rax
mov    (%rax),%edi
lea    0xb9b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    0x2a49(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xbb7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1090 <cleanup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     15d8 <main+0x428>
lea    0xba6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2a07(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x29e9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
jne    1401 <main+0x251>
xor    %eax,%eax
call   10d0 <setup@plt>
test   %eax,%eax
jne    1438 <main+0x288>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
test   %eax,%eax
je     11f5 <main+0x45>
lea    0xe35(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1090 <cleanup@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1070 <setgroups@plt>
test   %eax,%eax
je     1215 <main+0x65>
lea    0xe1f(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1090 <cleanup@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <setresgid@plt>
test   %eax,%eax
je     1237 <main+0x87>
lea    0xe07(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1090 <cleanup@plt>
xor    %ebx,%ebx
mov    $0x1f4,%edi
mov    $0x1f4,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
test   %eax,%eax
jne    141a <main+0x26a>
mov    0x2d77(%rip),%r14
mov    (%r14),%esi
lea    0xde5(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
jne    1288 <main+0xd8>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
xor    %eax,%eax
call   1040 <restoreprivilege@plt>
test   %eax,%eax
jne    1423 <main+0x273>
mov    $0x1f4,%edi
mov    $0x1f5,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
test   %eax,%eax
jne    142c <main+0x27c>
mov    (%r14),%esi
lea    0xdd4(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
je     12dd <main+0x12d>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%ebx
mov    (%r14),%esi
lea    0xde6(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
jne    130a <main+0x15a>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%ebx
xor    %eax,%eax
call   1040 <restoreprivilege@plt>
test   %eax,%eax
jne    1423 <main+0x273>
mov    $0x1f4,%edi
mov    $0x1f5,%esi
xor    %edx,%edx
call   1050 <setresgid@plt>
test   %eax,%eax
jne    144d <main+0x29d>
mov    $0x1f4,%edi
mov    $0x1f4,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
test   %eax,%eax
jne    1456 <main+0x2a6>
mov    (%r14),%esi
lea    0xd96(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
jne    1375 <main+0x1c5>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r14),%esi
lea    0xd96(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
je     13a2 <main+0x1f2>
mov    0x2c51(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%ebx
mov    (%r14),%esi
lea    0xda8(%rip),%rdi
call   10a0 <access@plt>
test   %eax,%eax
jne    13cf <main+0x21f>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%ebx
mov    0x2c0a(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1090 <cleanup@plt>
test   %eax,%eax
jne    145f <main+0x2af>
mov    0x2bce(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2bd8(%rip),%rax
mov    (%rax),%edi
lea    0xbef(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    143f <main+0x28f>
lea    0xc1b(%rip),%rdi
jmp    1433 <main+0x283>
lea    0xc48(%rip),%rdi
jmp    1433 <main+0x283>
lea    0xc50(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
call   1090 <cleanup@plt>
mov    0x2b92(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xc7f(%rip),%rdi
jmp    1433 <main+0x283>
lea    0xc82(%rip),%rdi
jmp    1433 <main+0x283>
lea    0xd05(%rip),%rdi
call   10b0 <perror@plt>
jmp    143f <main+0x28f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"testaccess must run as root.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"setresuid\00"
@.str.2 = [10 x i8] c"setgroups\00"
@.str.3 = [10 x i8] c"setresgid\00"
@.str.4 = [12 x i8] c"setresuid.1\00"
@.str.5 = [6 x i8] c"test1\00"
@R_OK = global i32 0
@.str.6 = [36 x i8] c"saved uid used instead of real uid\0A\00"
@.str.7 = [17 x i8] c"restoreprivilege\00"
@.str.8 = [11 x i8] c"setresid.2\00"
@.str.9 = [6 x i8] c"test2\00"
@.str.10 = [57 x i8] c"Effective uid was used instead of real uid in access().\0A\00"
@.str.11 = [6 x i8] c"test3\00"
@.str.12 = [12 x i8] c"setresgid.1\00"
@.str.13 = [12 x i8] c"setresuid.3\00"
@.str.14 = [6 x i8] c"test4\00"
@.str.15 = [36 x i8] c"saved gid used instead of real gid\0A\00"
@.str.16 = [6 x i8] c"test5\00"
@.str.17 = [57 x i8] c"Effective gid was used instead of real gid in access().\0A\00"
@.str.18 = [6 x i8] c"test6\00"
@.str.19 = [17 x i8] c"%d errors seen.\0A\00"
@.str.20 = [8 x i8] c"cleanup\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @geteuid()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = call i32 (...) @setup()
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = call i32 (...) @cleanup()
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = call i32 @setresuid(i32 0, i32 0, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 (...) @cleanup()
br label %30
30: ; preds = %27, %23
%31 = call i32 @setgroups(i32 0, i32* null)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 (...) @cleanup()
br label %37
37: ; preds = %34, %30
%38 = call i32 @setresgid(i32 0, i32 0, i32 0)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 (...) @cleanup()
br label %44
44: ; preds = %41, %37
%45 = call i32 @setresuid(i32 500, i32 500, i32 0)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 (...) @cleanup()
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %44
store i32 0, i32* %7
%54 = load i32, i32* @R_OK
%55 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %54)
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = icmp ne i32 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
br label %63
63: ; preds = %58, %53
%64 = call i32 (...) @restoreprivilege()
store i32 %64, i32* %6
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %63
%68 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%69 = call i32 (...) @cleanup()
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %63
%73 = call i32 @setresuid(i32 500, i32 501, i32 0)
store i32 %73, i32* %6
%74 = load i32, i32* %6
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %72
%77 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%78 = call i32 (...) @cleanup()
%79 = load i32, i32* @EXIT_FAILURE
%80 = call i32 @exit(i32 %79)
unreachable
81: ; preds = %72
%82 = load i32, i32* @R_OK
%83 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 %82)
store i32 %83, i32* %6
%84 = load i32, i32* %6
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %81
%87 = load i32, i32* @stderr
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0))
%89 = load i32, i32* %7
%90 = add nsw i32 %89, 1
store i32 %90, i32* %7
br label %91
91: ; preds = %86, %81
%92 = load i32, i32* @R_OK
%93 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0), i32 %92)
store i32 %93, i32* %6
%94 = load i32, i32* %6
%95 = icmp ne i32 %94, 0
br i1 %95, label %101, label %96
96: ; preds = %91
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0))
%99 = load i32, i32* %7
%100 = add nsw i32 %99, 1
store i32 %100, i32* %7
br label %101
101: ; preds = %96, %91
%102 = call i32 (...) @restoreprivilege()
store i32 %102, i32* %6
%103 = load i32, i32* %6
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %110
105: ; preds = %101
%106 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%107 = call i32 (...) @cleanup()
%108 = load i32, i32* @EXIT_FAILURE
%109 = call i32 @exit(i32 %108)
unreachable
110: ; preds = %101
%111 = call i32 @setresgid(i32 500, i32 501, i32 0)
store i32 %111, i32* %6
%112 = load i32, i32* %6
%113 = icmp ne i32 %112, 0
br i1 %113, label %114, label %119
114: ; preds = %110
%115 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0))
%116 = call i32 (...) @cleanup()
%117 = load i32, i32* @EXIT_FAILURE
%118 = call i32 @exit(i32 %117)
unreachable
119: ; preds = %110
%120 = call i32 @setresuid(i32 500, i32 500, i32 0)
store i32 %120, i32* %6
%121 = load i32, i32* %6
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %128
123: ; preds = %119
%124 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%125 = call i32 (...) @cleanup()
%126 = load i32, i32* @EXIT_FAILURE
%127 = call i32 @exit(i32 %126)
unreachable
128: ; preds = %119
%129 = load i32, i32* @R_OK
%130 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.14, i64 0, i64 0), i32 %129)
store i32 %130, i32* %6
%131 = load i32, i32* %6
%132 = icmp ne i32 %131, 0
br i1 %132, label %136, label %133
133: ; preds = %128
%134 = load i32, i32* @stderr
%135 = call i32 (i32, i8*, ...) @fprintf(i32 %134, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.15, i64 0, i64 0))
br label %136
136: ; preds = %133, %128
%137 = load i32, i32* @R_OK
%138 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.16, i64 0, i64 0), i32 %137)
store i32 %138, i32* %6
%139 = load i32, i32* %6
%140 = icmp ne i32 %139, 0
br i1 %140, label %141, label %146
141: ; preds = %136
%142 = load i32, i32* @stderr
%143 = call i32 (i32, i8*, ...) @fprintf(i32 %142, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.17, i64 0, i64 0))
%144 = load i32, i32* %7
%145 = add nsw i32 %144, 1
store i32 %145, i32* %7
br label %146
146: ; preds = %141, %136
%147 = load i32, i32* @R_OK
%148 = call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.18, i64 0, i64 0), i32 %147)
store i32 %148, i32* %6
%149 = load i32, i32* %6
%150 = icmp ne i32 %149, 0
br i1 %150, label %156, label %151
151: ; preds = %146
%152 = load i32, i32* @stderr
%153 = call i32 (i32, i8*, ...) @fprintf(i32 %152, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.17, i64 0, i64 0))
%154 = load i32, i32* %7
%155 = add nsw i32 %154, 1
store i32 %155, i32* %7
br label %156
156: ; preds = %151, %146
%157 = load i32, i32* @stderr
%158 = load i32, i32* %7
%159 = call i32 (i32, i8*, ...) @fprintf(i32 %157, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.19, i64 0, i64 0), i32 %158)
%160 = call i32 (...) @cleanup()
store i32 %160, i32* %6
%161 = load i32, i32* %6
%162 = icmp ne i32 %161, 0
br i1 %162, label %163, label %167
163: ; preds = %156
%164 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.20, i64 0, i64 0))
%165 = load i32, i32* @EXIT_FAILURE
%166 = call i32 @exit(i32 %165)
unreachable
167: ; preds = %156
%168 = load i32, i32* @EXIT_SUCCESS
%169 = call i32 @exit(i32 %168)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"testaccess must run as root.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"setresuid\00"
@.str.2 = [10 x i8] c"setgroups\00"
@.str.3 = [10 x i8] c"setresgid\00"
@.str.4 = [12 x i8] c"setresuid.1\00"
@.str.5 = [6 x i8] c"test1\00"
@R_OK = global i32 0
@.str.6 = [36 x i8] c"saved uid used instead of real uid\0A\00"
@.str.7 = [17 x i8] c"restoreprivilege\00"
@.str.8 = [11 x i8] c"setresid.2\00"
@.str.9 = [6 x i8] c"test2\00"
@.str.10 = [57 x i8] c"Effective uid was used instead of real uid in access().\0A\00"
@.str.11 = [6 x i8] c"test3\00"
@.str.12 = [12 x i8] c"setresgid.1\00"
@.str.13 = [12 x i8] c"setresuid.3\00"
@.str.14 = [6 x i8] c"test4\00"
@.str.15 = [36 x i8] c"saved gid used instead of real gid\0A\00"
@.str.16 = [6 x i8] c"test5\00"
@.str.17 = [57 x i8] c"Effective gid was used instead of real gid in access().\0A\00"
@.str.18 = [6 x i8] c"test6\00"
@.str.19 = [17 x i8] c"%d errors seen.\0A\00"
@.str.20 = [8 x i8] c"cleanup\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i32 (...) @setup()
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = tail call i32 (...) @cleanup()
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10
%18 = tail call i32 @setresuid(i32 0, i32 0, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 (...) @cleanup()
br label %23
23: ; preds = %20, %17
%24 = tail call i32 @setgroups(i32 0, i32* null)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 (...) @cleanup()
br label %29
29: ; preds = %26, %23
%30 = tail call i32 @setresgid(i32 0, i32 0, i32 0)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 (...) @cleanup()
br label %35
35: ; preds = %32, %29
%36 = tail call i32 @setresuid(i32 500, i32 500, i32 0)
%37 = icmp eq i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 (...) @cleanup()
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %35
%44 = load i32, i32* @R_OK
%45 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %50
50: ; preds = %47, %43
%51 = phi i32 [ 0, %43 ], [ 1, %47 ]
%52 = tail call i32 (...) @restoreprivilege()
%53 = icmp eq i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %50
%55 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%56 = tail call i32 (...) @cleanup()
%57 = load i32, i32* @EXIT_FAILURE
%58 = tail call i32 @exit(i32 %57)
unreachable
59: ; preds = %50
%60 = tail call i32 @setresuid(i32 500, i32 501, i32 0)
%61 = icmp eq i32 %60, 0
br i1 %61, label %67, label %62
62: ; preds = %59
%63 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
%64 = tail call i32 (...) @cleanup()
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %59
%68 = load i32, i32* @R_OK
%69 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 %68)
%70 = icmp eq i32 %69, 0
br i1 %70, label %75, label %71
71: ; preds = %67
%72 = load i32, i32* @stderr
%73 = tail call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0))
%74 = add nuw nsw i32 %51, 1
br label %75
75: ; preds = %71, %67
%76 = phi i32 [ %74, %71 ], [ %51, %67 ]
%77 = load i32, i32* @R_OK
%78 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0), i32 %77)
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %84
80: ; preds = %75
%81 = load i32, i32* @stderr
%82 = tail call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.10, i64 0, i64 0))
%83 = add nuw nsw i32 %76, 1
br label %84
84: ; preds = %80, %75
%85 = phi i32 [ %76, %75 ], [ %83, %80 ]
%86 = tail call i32 (...) @restoreprivilege()
%87 = icmp eq i32 %86, 0
br i1 %87, label %93, label %88
88: ; preds = %84
%89 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%90 = tail call i32 (...) @cleanup()
%91 = load i32, i32* @EXIT_FAILURE
%92 = tail call i32 @exit(i32 %91)
unreachable
93: ; preds = %84
%94 = tail call i32 @setresgid(i32 500, i32 501, i32 0)
%95 = icmp eq i32 %94, 0
br i1 %95, label %101, label %96
96: ; preds = %93
%97 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0))
%98 = tail call i32 (...) @cleanup()
%99 = load i32, i32* @EXIT_FAILURE
%100 = tail call i32 @exit(i32 %99)
unreachable
101: ; preds = %93
%102 = tail call i32 @setresuid(i32 500, i32 500, i32 0)
%103 = icmp eq i32 %102, 0
br i1 %103, label %109, label %104
104: ; preds = %101
%105 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%106 = tail call i32 (...) @cleanup()
%107 = load i32, i32* @EXIT_FAILURE
%108 = tail call i32 @exit(i32 %107)
unreachable
109: ; preds = %101
%110 = load i32, i32* @R_OK
%111 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.14, i64 0, i64 0), i32 %110)
%112 = icmp eq i32 %111, 0
br i1 %112, label %113, label %116
113: ; preds = %109
%114 = load i32, i32* @stderr
%115 = tail call i32 (i32, i8*, ...) @fprintf(i32 %114, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.15, i64 0, i64 0))
br label %116
116: ; preds = %113, %109
%117 = load i32, i32* @R_OK
%118 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.16, i64 0, i64 0), i32 %117)
%119 = icmp eq i32 %118, 0
br i1 %119, label %124, label %120
120: ; preds = %116
%121 = load i32, i32* @stderr
%122 = tail call i32 (i32, i8*, ...) @fprintf(i32 %121, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.17, i64 0, i64 0))
%123 = add nuw nsw i32 %85, 1
br label %124
124: ; preds = %120, %116
%125 = phi i32 [ %123, %120 ], [ %85, %116 ]
%126 = load i32, i32* @R_OK
%127 = tail call i32 @access(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.18, i64 0, i64 0), i32 %126)
%128 = icmp eq i32 %127, 0
br i1 %128, label %129, label %133
129: ; preds = %124
%130 = load i32, i32* @stderr
%131 = tail call i32 (i32, i8*, ...) @fprintf(i32 %130, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.17, i64 0, i64 0))
%132 = add nuw nsw i32 %125, 1
br label %133
133: ; preds = %129, %124
%134 = phi i32 [ %125, %124 ], [ %132, %129 ]
%135 = load i32, i32* @stderr
%136 = tail call i32 (i32, i8*, ...) @fprintf(i32 %135, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.19, i64 0, i64 0), i32 %134)
%137 = tail call i32 (...) @cleanup()
%138 = icmp eq i32 %137, 0
br i1 %138, label %143, label %139
139: ; preds = %133
%140 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.20, i64 0, i64 0))
%141 = load i32, i32* @EXIT_FAILURE
%142 = tail call i32 @exit(i32 %141)
unreachable
143: ; preds = %133
%144 = load i32, i32* @EXIT_SUCCESS
%145 = tail call i32 @exit(i32 %144)
unreachable
}