#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
int O_NOFOLLOW;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char *, int);
int read(int, char *, int);
int strtol(char *, int /*<<< orphan*/, int);
uid_t audit_getloginuid(void) {
  uid_t uid;
  int len, in;
  char buf[16];
  errno = 0;
  in = open("/proc/self/loginuid", O_NOFOLLOW | O_RDONLY);
  if (in < 0)
    return -1;
  do {
    len = read(in, buf, sizeof(buf));
  } while (len < 0 && errno == EINTR);
  close(in);
  if (len < 0 || len >= sizeof(buf))
    return -1;
  buf[len] = 0;
  errno = 0;
  uid = strtol(buf, 0, 10);
  if (errno)
    return -1;
  else
    return uid;
}


x86 assembly (clang, -O0):
<audit_getloginuid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e81(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    0x2e7a(%rip),%rax
or     (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    118d <audit_getloginuid+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <audit_getloginuid+0x113>
jmp    1192 <audit_getloginuid+0x52>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x21(%rbp)
jge    11cf <audit_getloginuid+0x8f>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1192 <audit_getloginuid+0x52>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0xc(%rbp)
jl     11fa <audit_getloginuid+0xba>
movslq -0xc(%rbp),%rax
cmp    $0x10,%rax
jb     1206 <audit_getloginuid+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <audit_getloginuid+0x113>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x20(%rbp,%rax,1)
mov    0x2dba(%rip),%rax
movq   $0x0,(%rax)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d99(%rip),%rax
cmpq   $0x0,(%rax)
je     124d <audit_getloginuid+0x10d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <audit_getloginuid+0x113>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<audit_getloginuid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e7b(%rip),%r12
movq   $0x0,(%r12)
mov    0x2e94(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11f4 <audit_getloginuid+0xb4>
mov    %eax,%ebp
mov    %rsp,%r15
mov    0x2e37(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bf <audit_getloginuid+0x7f>
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
je     11a0 <audit_getloginuid+0x60>
mov    %ebp,%edi
call   1030 <close@plt>
cmp    $0xf,%ebx
ja     11f4 <audit_getloginuid+0xb4>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
movq   $0x0,(%r12)
mov    %rsp,%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    (%r12),%rbx
sbb    %r14d,%r14d
or     %eax,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [20 x i8] c"/proc/self/loginuid\00"
@O_NOFOLLOW = global i32 0
@O_RDONLY = global i32 0
@EINTR = global i64 0
define i32 @audit_getloginuid() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
store i64 0, i64* @errno
%6 = load i32, i32* @O_NOFOLLOW
%7 = load i32, i32* @O_RDONLY
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %48
13: ; preds = %0
br label %14
14: ; preds = %25, %13
%15 = load i32, i32* %4
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 16)
store i32 %17, i32* %3
br label %18
18: ; preds = %14
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %14, label %27
27: ; preds = %25
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = icmp uge i64 %34, 16
br i1 %35, label %36, label %37
36: ; preds = %32, %27
store i32 -1, i32* %1
br label %48
37: ; preds = %32
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %39
store i8 0, i8* %40
store i64 0, i64* @errno
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%42 = call i32 @strtol(i8* %41, i32 0, i32 10)
store i32 %42, i32* %2
%43 = load i64, i64* @errno
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
store i32 -1, i32* %1
br label %48
46: ; preds = %37
%47 = load i32, i32* %2
store i32 %47, i32* %1
br label %48
48: ; preds = %46, %45, %36, %12
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [20 x i8] c"/proc/self/loginuid\00"
@O_NOFOLLOW = global i32 0
@O_RDONLY = global i32 0
@EINTR = global i64 0
define i32 @audit_getloginuid() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
store i64 0, i64* @errno
%3 = load i32, i32* @O_NOFOLLOW
%4 = load i32, i32* @O_RDONLY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %0, %8
%9 = call i32 @read(i32 %6, i8* %2, i32 16)
%10 = icmp slt i32 %9, 0
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %8, label %15
15: ; preds = %8
%16 = tail call i32 @close(i32 %6)
%17 = icmp ugt i32 %9, 15
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = zext i32 %9 to i64
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
store i64 0, i64* @errno
%21 = call i32 @strtol(i8* %2, i32 0, i32 10)
%22 = load i64, i64* @errno
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i32 %21, i32 -1
br label %25
25: ; preds = %18, %15, %0
%26 = phi i32 [ -1, %0 ], [ -1, %15 ], [ %24, %18 ]
ret i32 %26
}