#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 /*<<< orphan*/ uid_t;
struct dirent {
  char const *d_name;
};
typedef int /*<<< orphan*/ gid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ NAME_MAX;
int /*<<< orphan*/ change_owner(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *stpncpy(char *, char const *, size_t);
size_t strlen(char const *);
void chown_dir_contents(const char *dir_path, uid_t uid, gid_t gid) {
  DIR *dp;
  struct dirent *ep;
  char *path_tmp = malloc(strlen(dir_path) + NAME_MAX + 2);
  if (path_tmp == NULL) {
    return;
  }
  char *buf = stpncpy(path_tmp, dir_path, strlen(dir_path));
  *buf++ = '/';
  dp = opendir(dir_path);
  if (dp != NULL) {
    while ((ep = readdir(dp)) != NULL) {
      stpncpy(buf, ep->d_name, strlen(ep->d_name));
      buf[strlen(ep->d_name)] = '\0';
      change_owner(path_tmp, uid, gid);
    }
    closedir(dp);
  }
  free(path_tmp);
}


x86 assembly (clang, -O0):
<chown_dir_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
mov    0x2e33(%rip),%rax
add    (%rax),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c5 <chown_dir_contents+0x45>
jmp    12ac <chown_dir_contents+0x12c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   10a0 <stpncpy@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x2f,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a3 <chown_dir_contents+0x123>
jmp    1221 <chown_dir_contents+0xa1>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     129a <chown_dir_contents+0x11a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   10a0 <stpncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <change_owner@plt>
jmp    1221 <chown_dir_contents+0xa1>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chown_dir_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1284 <chown_dir_contents+0x104>
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10a0 <stpncpy@plt>
mov    %rax,%rbx
movb   $0x2f,(%rax)
mov    %r14,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     126c <chown_dir_contents+0xec>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <readdir@plt>
mov    %rbx,0x8(%rsp)
test   %rax,%rax
je     1264 <chown_dir_contents+0xe4>
mov    %rax,%rbx
mov    %r15d,%r12d
addq   $0x1,0x8(%rsp)
mov    0x8(%rsp),%r15
xchg   %ax,%ax
mov    %r12d,%r13d
mov    %r14,%r12
mov    (%rbx),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%r14
mov    %r13d,%r12d
mov    0x10(%rsp),%r13
mov    %rax,%rdx
call   10a0 <stpncpy@plt>
mov    (%rbx),%rdi
call   1050 <strlen@plt>
movb   $0x0,(%r15,%rax,1)
mov    %r13,%rdi
mov    %r12d,%esi
mov    %ebp,%edx
call   1060 <change_owner@plt>
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1210 <chown_dir_contents+0x90>
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    0x10(%rsp),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@NAME_MAX = global i64 0
define void @chown_dir_contents(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
%13 = load i64, i64* @NAME_MAX
%14 = add i64 %12, %13
%15 = add i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
br label %63
20: ; preds = %3
%21 = load i8*, i8** %9
%22 = load i8*, i8** %4
%23 = load i8*, i8** %4
%24 = call i64 @strlen(i8* %23)
%25 = call i8* @stpncpy(i8* %21, i8* %22, i64 %24)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %10
store i8 47, i8* %26
%28 = load i8*, i8** %4
%29 = call i32* @opendir(i8* %28)
store i32* %29, i32** %7
%30 = load i32*, i32** %7
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %60
32: ; preds = %20
br label %33
33: ; preds = %37, %32
%34 = load i32*, i32** %7
%35 = call %struct.dirent* @readdir(i32* %34)
store %struct.dirent* %35, %struct.dirent** %8
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %57
37: ; preds = %33
%38 = load i8*, i8** %10
%39 = load %struct.dirent*, %struct.dirent** %8
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = load %struct.dirent*, %struct.dirent** %8
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strlen(i8* %44)
%46 = call i8* @stpncpy(i8* %38, i8* %41, i64 %45)
%47 = load i8*, i8** %10
%48 = load %struct.dirent*, %struct.dirent** %8
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @strlen(i8* %50)
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %9
%54 = load i32, i32* %5
%55 = load i32, i32* %6
%56 = call i32 @change_owner(i8* %53, i32 %54, i32 %55)
br label %33
57: ; preds = %33
%58 = load i32*, i32** %7
%59 = call i32 @closedir(i32* %58)
br label %60
60: ; preds = %57, %20
%61 = load i8*, i8** %9
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %60, %19
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@NAME_MAX = global i64 0
define void @chown_dir_contents(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = load i64, i64* @NAME_MAX
%6 = add i64 %4, 2
%7 = add i64 %6, %5
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %34, label %10
10: ; preds = %3
%11 = tail call i8* @stpncpy(i8* %8, i8* %0, i64 %4)
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8 47, i8* %11
%13 = tail call i32* @opendir(i8* %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %32, label %15
15: ; preds = %10
%16 = tail call %struct.dirent* @readdir(i32* %13)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %30, label %18
18: ; preds = %15, %18
%19 = phi %struct.dirent* [ %28, %18 ], [ %16, %15 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i64 @strlen(i8* dereferenceable(1) %21)
%23 = tail call i8* @stpncpy(i8* %12, i8* %21, i64 %22)
%24 = load i8*, i8** %20
%25 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%26 = getelementptr inbounds i8, i8* %12, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @change_owner(i8* %8, i32 %1, i32 %2)
%28 = tail call %struct.dirent* @readdir(i32* %13)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %30, label %18
30: ; preds = %18, %15
%31 = tail call i32 @closedir(i32* %13)
br label %32
32: ; preds = %30, %10
%33 = tail call i32 @free(i8* %8)
br label %34
34: ; preds = %3, %32
ret void
}