#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 */

/* Variables and functions */
int PATH_MAX;
int S_IRUSR;
int S_IWUSR;
scalar_t__ chmod(char *, int);
scalar_t__ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
int snprintf(char *, int, char *, unsigned int, char const *);
void allow_access_by_user(unsigned int pin, const char *filename) {
  char path[PATH_MAX];
  int size = snprintf(path, PATH_MAX, "/sys/devices/virtual/gpio/gpio%u/%s",
                      pin, filename);
  if (size >= PATH_MAX) {
    error(7, 0, "path of GPIO pin is too long!");
  }
  if (chown(path, getuid(), getgid()) != 0) {
    error(5, errno, "failed to change group ownership of %s", path);
  }
  if (chmod(path, S_IRUSR | S_IWUSR) != 0) {
    error(6, errno, "failed to set permissions of %s", path);
  }
}


x86 assembly (clang, -O0):
<allow_access_by_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xe4e(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%eax
jl     11e1 <allow_access_by_user+0x81>
mov    $0x7,%edi
xor    %esi,%esi
lea    0xe4a(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1050 <getgid@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   1060 <chown@plt>
cmp    $0x0,%rax
je     122a <allow_access_by_user+0xca>
mov    -0x30(%rbp),%rcx
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    $0x5,%edi
lea    0xe1f(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
or     (%rax),%esi
call   1070 <chmod@plt>
cmp    $0x0,%rax
je     126f <allow_access_by_user+0x10f>
mov    -0x30(%rbp),%rcx
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    $0x6,%edi
lea    0xe01(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allow_access_by_user>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%r8
mov    %edi,%ecx
mov    0x2e7d(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6f(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
cmp    (%r14),%eax
jl     11b3 <allow_access_by_user+0x53>
lea    0xe7f(%rip),%rdx
mov    $0x7,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <error@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1050 <getgid@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%edx
call   1060 <chown@plt>
test   %rax,%rax
je     11f5 <allow_access_by_user+0x95>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdx
mov    $0x5,%edi
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <error@plt>
mov    0x2dcc(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <chmod@plt>
test   %rax,%rax
je     1233 <allow_access_by_user+0xd3>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdx
mov    $0x6,%edi
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <error@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [36 x i8] c"/sys/devices/virtual/gpio/gpio%u/%s\00"
@.str.1 = [30 x i8] c"path of GPIO pin is too long!\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"failed to change group ownership of %s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [32 x i8] c"failed to set permissions of %s\00"
define void @allow_access_by_user(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = load i32, i32* @PATH_MAX
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = call i32 @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i32, i32* @PATH_MAX
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i32, i32, i8*, ...) @error(i32 7, i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 (...) @getuid()
%23 = call i32 (...) @getgid()
%24 = call i64 @chown(i8* %11, i32 %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @errno
%28 = call i32 (i32, i32, i8*, ...) @error(i32 5, i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %29
29: ; preds = %26, %21
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = call i64 @chmod(i8* %11, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = call i32 (i32, i32, i8*, ...) @error(i32 6, i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %38
38: ; preds = %35, %29
%39 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [36 x i8] c"/sys/devices/virtual/gpio/gpio%u/%s\00"
@.str.1 = [30 x i8] c"path of GPIO pin is too long!\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"failed to change group ownership of %s\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [32 x i8] c"failed to set permissions of %s\00"
define void @allow_access_by_user(i32 %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @PATH_MAX
%8 = icmp slt i32 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 (i32, i32, i8*, ...) @error(i32 7, i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 (...) @getuid()
%13 = call i32 (...) @getgid()
%14 = call i64 @chown(i8* %5, i32 %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @errno
%18 = call i32 (i32, i32, i8*, ...) @error(i32 5, i32 %17, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %19
19: ; preds = %16, %11
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = call i64 @chmod(i8* %5, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i32 (i32, i32, i8*, ...) @error(i32 6, i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %28
28: ; preds = %25, %19
ret void
}