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

/* Variables and functions */
scalar_t__ getlogin_r(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
char *yffs_owner(struct yffs *, char const *);
char yffs_permission(struct yffs *, char const *);
int have_read(struct yffs *fs, const char *filename) {
  char permbit = yffs_permission(fs, filename);
  char *user = (char *)malloc(sizeof(char) * 10);
  if (getlogin_r(user, 10) != 0)
    printf("problem getting user login...\n");
  char *owner = yffs_owner(fs, filename);
  if (strcmp(owner, user) == 0) {
    permbit = permbit & 8;
    if (permbit != 0)
      return 1;
    else
      return 0;
  } else {
    permbit = permbit & 2;
    if (permbit != 0)
      return 1;
    else
      return 0;
  }
}


x86 assembly (clang, -O0):
<have_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <yffs_permission@plt>
mov    %al,-0x19(%rbp)
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1080 <getlogin_r@plt>
cmp    $0x0,%rax
je     11b2 <have_read+0x52>
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <yffs_owner@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1209 <have_read+0xa9>
movsbl -0x19(%rbp),%eax
and    $0x8,%eax
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     11fd <have_read+0x9d>
movl   $0x1,-0x4(%rbp)
jmp    1233 <have_read+0xd3>
movl   $0x0,-0x4(%rbp)
jmp    1233 <have_read+0xd3>
movsbl -0x19(%rbp),%eax
and    $0x2,%eax
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     122c <have_read+0xcc>
movl   $0x1,-0x4(%rbp)
jmp    1233 <have_read+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<have_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1070 <yffs_permission@plt>
mov    %eax,%ebx
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0xa,%esi
call   1080 <getlogin_r@plt>
test   %rax,%rax
je     11a0 <have_read+0x40>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <yffs_owner@plt>
mov    %rax,%rdi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
sete   %cl
add    %cl,%cl
or     $0x1,%cl
shr    %cl,%bl
and    $0x1,%bl
movzbl %bl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.yffs = type { i32 }
@.str = [31 x i8] c"problem getting user login...\0A\00"
define i32 @have_read(%struct.yffs* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.yffs*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
store %struct.yffs* %0, %struct.yffs** %4
store i8* %1, i8** %5
%9 = load %struct.yffs*, %struct.yffs** %4
%10 = load i8*, i8** %5
%11 = call signext i8 @yffs_permission(%struct.yffs* %9, i8* %10)
store i8 %11, i8* %6
%12 = call i64 @malloc(i32 10)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = call i64 @getlogin_r(i8* %14, i32 10)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load %struct.yffs*, %struct.yffs** %4
%21 = load i8*, i8** %5
%22 = call i8* @yffs_owner(%struct.yffs* %20, i8* %21)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = load i8*, i8** %7
%25 = call i64 @strcmp(i8* %23, i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %19
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
%30 = and i32 %29, 8
%31 = trunc i32 %30 to i8
store i8 %31, i8* %6
%32 = load i8, i8* %6
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 1, i32* %3
br label %47
36: ; preds = %27
store i32 0, i32* %3
br label %47
37: ; preds = %19
%38 = load i8, i8* %6
%39 = sext i8 %38 to i32
%40 = and i32 %39, 2
%41 = trunc i32 %40 to i8
store i8 %41, i8* %6
%42 = load i8, i8* %6
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %37
store i32 1, i32* %3
br label %47
46: ; preds = %37
store i32 0, i32* %3
br label %47
47: ; preds = %46, %45, %36, %35
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.yffs = type { i32 }
@str = [30 x i8] c"problem getting user login...\00"
define i32 @have_read(%struct.yffs* %0, i8* %1) {
%3 = tail call signext i8 @yffs_permission(%struct.yffs* %0, i8* %1)
%4 = tail call i64 @malloc(i32 10)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i64 @getlogin_r(i8* %5, i32 10)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i8* @yffs_owner(%struct.yffs* %0, i8* %1)
%12 = tail call i64 @strcmp(i8* %11, i8* %5)
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i8 3, i8 1
%15 = lshr i8 %3, %14
%16 = and i8 %15, 1
%17 = zext i8 %16 to i32
ret i32 %17
}