#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 pw_uid;
};
struct hotplug2_event_t {
  int dummy;
};
struct group {
  int gr_gid;
};

/* Variables and functions */
int chown(char *, int, int);
int /*<<< orphan*/ free(char *);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
char *replace_key_by_value(int /*<<< orphan*/, struct hotplug2_event_t *);
int /*<<< orphan*/ strdup(char *);
int chown_chgrp(struct hotplug2_event_t *event, int action, char *file,
                char *param) {
  struct group *grp;
  struct passwd *pwd;
  int rv;
  file = replace_key_by_value(strdup(file), event);
  param = replace_key_by_value(strdup(param), event);
  rv = -1;
  switch (action) {
  case 7:
    pwd = getpwnam(param);
    rv = chown(file, pwd->pw_uid, -1);
    break;
  case 6:
    grp = getgrnam(param);
    rv = chown(file, -1, grp->gr_gid);
    break;
  }
  free(file);
  free(param);
  return rv;
}


x86 assembly (clang, -O0):
<chown_chgrp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1060 <replace_key_by_value@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1060 <replace_key_by_value@plt>
mov    %rax,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x6,%eax
je     11fc <chown_chgrp+0x9c>
jmp    11c2 <chown_chgrp+0x62>
mov    -0x38(%rbp),%eax
sub    $0x7,%eax
jne    1220 <chown_chgrp+0xc0>
jmp    11d3 <chown_chgrp+0x73>
mov    -0x20(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    $0xffffffff,%edx
call   1070 <chown@plt>
mov    %eax,-0x34(%rbp)
jmp    1220 <chown_chgrp+0xc0>
mov    -0x20(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1070 <chown@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chown_chgrp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%rbx
mov    %esi,%r12d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1060 <replace_key_by_value@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1060 <replace_key_by_value@plt>
mov    %rax,%r15
cmp    $0x6,%r12d
je     11bd <chown_chgrp+0x5d>
mov    $0xffffffff,%ebx
cmp    $0x7,%r12d
jne    11d6 <chown_chgrp+0x76>
mov    %r15,%rdi
call   1040 <getpwnam@plt>
mov    (%rax),%esi
mov    $0xffffffff,%edx
jmp    11cc <chown_chgrp+0x6c>
mov    %r15,%rdi
call   1050 <getgrnam@plt>
mov    (%rax),%edx
mov    $0xffffffff,%esi
mov    %r14,%rdi
call   1070 <chown@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hotplug2_event_t = type { i32 }
%struct.group = type { i32 }
%struct.passwd = type { i32 }
define i32 @chown_chgrp(%struct.hotplug2_event_t* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca %struct.hotplug2_event_t*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.group*
%10 = alloca %struct.passwd*
%11 = alloca i32
store %struct.hotplug2_event_t* %0, %struct.hotplug2_event_t** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %7
%13 = call i32 @strdup(i8* %12)
%14 = load %struct.hotplug2_event_t*, %struct.hotplug2_event_t** %5
%15 = call i8* @replace_key_by_value(i32 %13, %struct.hotplug2_event_t* %14)
store i8* %15, i8** %7
%16 = load i8*, i8** %8
%17 = call i32 @strdup(i8* %16)
%18 = load %struct.hotplug2_event_t*, %struct.hotplug2_event_t** %5
%19 = call i8* @replace_key_by_value(i32 %17, %struct.hotplug2_event_t* %18)
store i8* %19, i8** %8
store i32 -1, i32* %11
%20 = load i32, i32* %6
switch i32 %20, label %37 [
i32 7, label %21
i32 6, label %29
]
21: ; preds = %4
%22 = load i8*, i8** %8
%23 = call %struct.passwd* @getpwnam(i8* %22)
store %struct.passwd* %23, %struct.passwd** %10
%24 = load i8*, i8** %7
%25 = load %struct.passwd*, %struct.passwd** %10
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @chown(i8* %24, i32 %27, i32 -1)
store i32 %28, i32* %11
br label %37
29: ; preds = %4
%30 = load i8*, i8** %8
%31 = call %struct.group* @getgrnam(i8* %30)
store %struct.group* %31, %struct.group** %9
%32 = load i8*, i8** %7
%33 = load %struct.group*, %struct.group** %9
%34 = getelementptr inbounds %struct.group, %struct.group* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @chown(i8* %32, i32 -1, i32 %35)
store i32 %36, i32* %11
br label %37
37: ; preds = %4, %29, %21
%38 = load i8*, i8** %7
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %11
ret i32 %42
}

LLVM IR (-O2):
%struct.hotplug2_event_t = type { i32 }
%struct.passwd = type { i32 }
%struct.group = type { i32 }
define i32 @chown_chgrp(%struct.hotplug2_event_t* %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call i32 @strdup(i8* %2)
%6 = tail call i8* @replace_key_by_value(i32 %5, %struct.hotplug2_event_t* %0)
%7 = tail call i32 @strdup(i8* %3)
%8 = tail call i8* @replace_key_by_value(i32 %7, %struct.hotplug2_event_t* %0)
switch i32 %1, label %21 [
i32 7, label %9
i32 6, label %13
]
9: ; preds = %4
%10 = tail call %struct.passwd* @getpwnam(i8* %8)
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%12 = load i32, i32* %11
br label %17
13: ; preds = %4
%14 = tail call %struct.group* @getgrnam(i8* %8)
%15 = getelementptr inbounds %struct.group, %struct.group* %14, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %9, %13
%18 = phi i32 [ %16, %13 ], [ -1, %9 ]
%19 = phi i32 [ -1, %13 ], [ %12, %9 ]
%20 = tail call i32 @chown(i8* %6, i32 %19, i32 %18)
br label %21
21: ; preds = %17, %4
%22 = phi i32 [ -1, %4 ], [ %20, %17 ]
%23 = tail call i32 @free(i8* %6)
%24 = tail call i32 @free(i8* %8)
ret i32 %22
}