#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 passwd {
  int /*<<< orphan*/ pw_uid;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void change_owner(char *file, char *user, char *group) {
  struct passwd *pwd;
  struct group *grp;
  int fildes;
  fildes = open(file, O_RDWR);
  if (fildes == -1) {
    perror("Error openning file");
    return;
  }
  pwd = getpwnam(user);
  grp = getgrnam(group);
  fchown(fildes, pwd->pw_uid, grp->gr_gid);
  close(fildes);
}


x86 assembly (clang, -O0):
<change_owner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a4 <change_owner+0x44>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
jmp    11da <change_owner+0x7a>
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <fchown@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_owner>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ab <change_owner+0x4b>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <getpwnam@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <getgrnam@plt>
mov    (%r15),%esi
mov    (%rax),%edx
mov    %ebx,%edi
call   1070 <fchown@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <close@plt>
lea    0xe4e(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <perror@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@O_RDWR = global i32 0
@.str = [20 x i8] c"Error openning file\00"
define void @change_owner(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca %struct.group*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %32
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call %struct.passwd* @getpwnam(i8* %18)
store %struct.passwd* %19, %struct.passwd** %7
%20 = load i8*, i8** %6
%21 = call %struct.group* @getgrnam(i8* %20)
store %struct.group* %21, %struct.group** %8
%22 = load i32, i32* %9
%23 = load %struct.passwd*, %struct.passwd** %7
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load %struct.group*, %struct.group** %8
%27 = getelementptr inbounds %struct.group, %struct.group* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @fchown(i32 %22, i32 %25, i32 %28)
%30 = load i32, i32* %9
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %17, %15
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@O_RDWR = global i32 0
@.str = [20 x i8] c"Error openning file\00"
define void @change_owner(i8* nocapture %0, i8* nocapture %1, i8* %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %3
%10 = tail call %struct.passwd* @getpwnam(i8* %1)
%11 = tail call %struct.group* @getgrnam(i8* %2)
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @fchown(i32 %5, i32 %13, i32 %15)
%17 = tail call i32 @close(i32 %5)
br label %18
18: ; preds = %9, %7
ret void
}