#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 stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EUID_ROOT();
int /*<<< orphan*/ EUID_USER();
int /*<<< orphan*/ assert(char const *);
scalar_t__ geteuid();
int lstat(char const *, struct stat *);
int lstat_as_user(const char *fname, struct stat *s) {
  assert(fname);
  int called_as_root = 0;
  if (geteuid() == 0)
    called_as_root = 1;
  if (called_as_root)
    EUID_USER();
  int rv = lstat(fname, s);
  if (called_as_root)
    EUID_ROOT();
  return rv;
}


x86 assembly (clang, -O0):
<lstat_as_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <assert@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%rax
jne    1188 <lstat_as_user+0x38>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1199 <lstat_as_user+0x49>
mov    $0x0,%al
call   1030 <EUID_USER@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <lstat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ba <lstat_as_user+0x6a>
mov    $0x0,%al
call   1070 <EUID_ROOT@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lstat_as_user>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <assert@plt>
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %rax,%rax
je     117d <lstat_as_user+0x2d>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <lstat@plt>
xor    %eax,%eax
call   1030 <EUID_USER@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <lstat@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <EUID_ROOT@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @lstat_as_user(i8* %0, %struct.stat* %1) {
%3 = alloca i8*
%4 = alloca %struct.stat*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store %struct.stat* %1, %struct.stat** %4
%7 = load i8*, i8** %3
%8 = call i32 @assert(i8* %7)
store i32 0, i32* %5
%9 = call i64 (...) @geteuid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %5
br label %12
12: ; preds = %11, %2
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (...) @EUID_USER()
br label %17
17: ; preds = %15, %12
%18 = load i8*, i8** %3
%19 = load %struct.stat*, %struct.stat** %4
%20 = call i32 @lstat(i8* %18, %struct.stat* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (...) @EUID_ROOT()
br label %25
25: ; preds = %23, %17
%26 = load i32, i32* %6
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @lstat_as_user(i8* %0, %struct.stat* nocapture %1) {
%3 = tail call i32 @assert(i8* %0)
%4 = tail call i64 (...) @geteuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @lstat(i8* %0, %struct.stat* %1)
br label %12
8: ; preds = %2
%9 = tail call i32 (...) @EUID_USER()
%10 = tail call i32 @lstat(i8* %0, %struct.stat* %1)
%11 = tail call i32 (...) @EUID_ROOT()
br label %12
12: ; preds = %6, %8
%13 = phi i32 [ %10, %8 ], [ %7, %6 ]
ret i32 %13
}