#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;
struct stat {
  int st_uid;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int geteuid();
int getuid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int main(void) {
  int fd;
  struct stat statbuf;
  pid_t pid;
  uid_t ruid, euid;
  if (stat("test.txt", &statbuf) == -1) {
    perror("fail to stat");
    exit(1);
  }
  if ((ruid = getuid()) == -1) {
    perror("fail to get ruid");
    exit(1);
  }
  if ((euid = geteuid()) == -1) {
    perror("fail to get euid");
    exit(1);
  }
  printf("real id is : %u, effective id is : %u \n", (unsigned int)ruid,
         (unsigned int)euid);
  printf("file owner is : %u\n", statbuf.st_uid);
  if (access("test.txt", R_OK) == -1) {
    perror("fail to access");
    exit(1);
  }
  printf("access successfully\n");
  if ((fd = open("test.txt", O_RDONLY)) == -1) {
    perror("fail to open");
    exit(1);
  }
  printf("ready to read\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x3e>
lea    0xe4a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11f7 <main+0x67>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1220 <main+0x90>
lea    0xe16(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xdab(%rip),%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
jne    1279 <main+0xe9>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    0xd69(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    12be <main+0x12e>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe54(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1262 <main+0xc2>
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    $0xffffffff,%eax
je     126b <main+0xcb>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <geteuid@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xd4>
lea    0xe50(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%esi
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0xdeb(%rip),%rdi
call   10a0 <access@plt>
cmp    $0xffffffff,%eax
je     127d <main+0xdd>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0xe6>
mov    %eax,%ebx
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xda0(%rip),%rdi
jmp    128d <main+0xed>
lea    0xda4(%rip),%rdi
jmp    128d <main+0xed>
lea    0xdac(%rip),%rdi
jmp    128d <main+0xed>
lea    0xdf0(%rip),%rdi
jmp    128d <main+0xed>
lea    0xdf6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [13 x i8] c"fail to stat\00"
@.str.2 = [17 x i8] c"fail to get ruid\00"
@.str.3 = [17 x i8] c"fail to get euid\00"
@.str.4 = [40 x i8] c"real id is : %u, effective id is : %u \0A\00"
@.str.5 = [20 x i8] c"file owner is : %u\0A\00"
@R_OK = global i32 0
@.str.6 = [15 x i8] c"fail to access\00"
@.str.7 = [21 x i8] c"access successfully\0A\00"
@O_RDONLY = global i32 0
@.str.8 = [13 x i8] c"fail to open\00"
@.str.9 = [15 x i8] c"ready to read\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 (...) @getuid()
store i32 %13, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = call i32 (...) @geteuid()
store i32 %19, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %25, i32 %26)
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @R_OK
%32 = call i32 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %24
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %39)
store i32 %40, i32* %2
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
%47 = load i32, i32* %2
%48 = call i32 @close(i32 %47)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [13 x i8] c"fail to stat\00"
@.str.2 = [17 x i8] c"fail to get ruid\00"
@.str.3 = [17 x i8] c"fail to get euid\00"
@.str.4 = [40 x i8] c"real id is : %u, effective id is : %u \0A\00"
@.str.5 = [20 x i8] c"file owner is : %u\0A\00"
@R_OK = global i32 0
@.str.6 = [15 x i8] c"fail to access\00"
@O_RDONLY = global i32 0
@.str.8 = [13 x i8] c"fail to open\00"
@str = [20 x i8] c"access successfully\00"
@str.10 = [14 x i8] c"ready to read\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 (...) @getuid()
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = tail call i32 (...) @geteuid()
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %9, i32 %15)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%23 = load i32, i32* %22
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @R_OK
%26 = tail call i32 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%33 = load i32, i32* @O_RDONLY
%34 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
%41 = tail call i32 @close(i32 %34)
ret i32 0
}