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

/* Variables and functions */
int /*<<< orphan*/ _chgrp_do_recursive(int, int /*<<< orphan*/, char *);
int _chgrp_error(char *, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int _chgrp_do_recursive_do(int opts, gid_t gid, char *file) {
  DIR *dir;
  struct dirent *de;
  int len;
  char *s;
  char *p;
  if ((dir = opendir(file)) == NULL)
    return _chgrp_error(file, 1);
  readdir(dir);
  readdir(dir);
  len = strlen(file);
  len += (len && file[len - 1] == '/') ? 1 : 2;
  if ((s = malloc(len)) == NULL) {
    closedir(dir);
    return _chgrp_error(file, 1);
  }
  strcpy(s, file);
  s[len - 2] = '/';
  s[len - 1] = '\0';
  while ((de = readdir(dir)) != NULL) {
    if ((p = realloc(s, len + strlen(de->d_name))) == NULL) {
      _chgrp_error("malloc", 0);
      continue;
    }
    s = p;
    strcat(s, de->d_name);
    _chgrp_do_recursive(opts, gid, s);
    s[len - 1] = '\0';
  }
  free(s);
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<_chgrp_do_recursive_do>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ef <_chgrp_do_recursive_do+0x3f>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <_chgrp_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1369 <_chgrp_do_recursive_do+0x1b9>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x41(%rbp)
je     1236 <_chgrp_do_recursive_do+0x86>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%dl
mov    $0x2,%eax
mov    $0x1,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1284 <_chgrp_do_recursive_do+0xd4>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <_chgrp_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1369 <_chgrp_do_recursive_do+0x1b9>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1350 <_chgrp_do_recursive_do+0x1a0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%rdi
add    %eax,%esi
call   10b0 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1313 <_chgrp_do_recursive_do+0x163>
lea    0xcf9(%rip),%rdi
xor    %esi,%esi
call   1080 <_chgrp_error@plt>
jmp    12b3 <_chgrp_do_recursive_do+0x103>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
call   10c0 <strcat@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rdx
call   10d0 <_chgrp_do_recursive@plt>
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12b3 <_chgrp_do_recursive_do+0x103>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_chgrp_do_recursive_do>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %esi,%r14d
mov    %edi,%ebx
mov    %rdx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     130d <_chgrp_do_recursive_do+0x15d>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <readdir@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %r13,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     120d <_chgrp_do_recursive_do+0x5d>
movslq %eax,%rcx
xor    %edx,%edx
cmpb   $0x2f,-0x1(%rcx,%r13,1)
sete   %dl
mov    $0x2,%ebp
sub    %edx,%ebp
jmp    1212 <_chgrp_do_recursive_do+0x62>
mov    $0x2,%ebp
add    %eax,%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1305 <_chgrp_do_recursive_do+0x155>
mov    %rax,%r15
mov    %r14d,0xc(%rsp)
mov    %ebx,0x8(%rsp)
mov    %rax,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
movslq %ebp,%rcx
mov    %rcx,0x10(%rsp)
movw   $0x2f,-0x2(%rcx,%r15,1)
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12e1 <_chgrp_do_recursive_do+0x131>
mov    %rax,%r14
lea    0xd9a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%esi
mov    %r15,%rdi
call   10b0 <realloc@plt>
test   %rax,%rax
jne    12a4 <_chgrp_do_recursive_do+0xf4>
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <_chgrp_error@plt>
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1270 <_chgrp_do_recursive_do+0xc0>
jmp    12e1 <_chgrp_do_recursive_do+0x131>
mov    %rax,%rbx
mov    (%r14),%rsi
mov    %rax,%rdi
call   10c0 <strcat@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
mov    %rbx,%rdx
call   10d0 <_chgrp_do_recursive@plt>
mov    0x10(%rsp),%rax
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %r12,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
mov    %rbx,%r15
test   %rax,%rax
jne    1270 <_chgrp_do_recursive_do+0xc0>
jmp    12e4 <_chgrp_do_recursive_do+0x134>
mov    %r15,%rbx
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    %r13,%rdi
mov    $0x1,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <_chgrp_error@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"malloc\00"
define i32 @_chgrp_do_recursive_do(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%13 = load i8*, i8** %7
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %8
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i8*, i8** %7
%18 = call i32 @_chgrp_error(i8* %17, i32 1)
store i32 %18, i32* %4
br label %102
19: ; preds = %3
%20 = load i32*, i32** %8
%21 = call %struct.dirent* @readdir(i32* %20)
%22 = load i32*, i32** %8
%23 = call %struct.dirent* @readdir(i32* %22)
%24 = load i8*, i8** %7
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %19
%29 = load i8*, i8** %7
%30 = load i32, i32* %10
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 47
br label %37
37: ; preds = %28, %19
%38 = phi i1 [ false, %19 ], [ %36, %28 ]
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 1, i32 2
%41 = load i32, i32* %10
%42 = add nsw i32 %41, %40
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = call i8* @malloc(i32 %43)
store i8* %44, i8** %11
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %37
%47 = load i32*, i32** %8
%48 = call i32 @closedir(i32* %47)
%49 = load i8*, i8** %7
%50 = call i32 @_chgrp_error(i8* %49, i32 1)
store i32 %50, i32* %4
br label %102
51: ; preds = %37
%52 = load i8*, i8** %11
%53 = load i8*, i8** %7
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = load i8*, i8** %11
%56 = load i32, i32* %10
%57 = sub nsw i32 %56, 2
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 47, i8* %59
%60 = load i8*, i8** %11
%61 = load i32, i32* %10
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
br label %65
65: ; preds = %81, %79, %51
%66 = load i32*, i32** %8
%67 = call %struct.dirent* @readdir(i32* %66)
store %struct.dirent* %67, %struct.dirent** %9
%68 = icmp ne %struct.dirent* %67, null
br i1 %68, label %69, label %97
69: ; preds = %65
%70 = load i8*, i8** %11
%71 = load i32, i32* %10
%72 = load %struct.dirent*, %struct.dirent** %9
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = call i32 @strlen(i8* %74)
%76 = add nsw i32 %71, %75
%77 = call i8* @realloc(i8* %70, i32 %76)
store i8* %77, i8** %12
%78 = icmp eq i8* %77, null
br i1 %78, label %79, label %81
79: ; preds = %69
%80 = call i32 @_chgrp_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
br label %65
81: ; preds = %69
%82 = load i8*, i8** %12
store i8* %82, i8** %11
%83 = load i8*, i8** %11
%84 = load %struct.dirent*, %struct.dirent** %9
%85 = getelementptr inbounds %struct.dirent, %struct.dirent* %84, i32 0, i32 0
%86 = load i8*, i8** %85
%87 = call i32 @strcat(i8* %83, i8* %86)
%88 = load i32, i32* %5
%89 = load i32, i32* %6
%90 = load i8*, i8** %11
%91 = call i32 @_chgrp_do_recursive(i32 %88, i32 %89, i8* %90)
%92 = load i8*, i8** %11
%93 = load i32, i32* %10
%94 = sub nsw i32 %93, 1
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %92, i64 %95
store i8 0, i8* %96
br label %65
97: ; preds = %65
%98 = load i8*, i8** %11
%99 = call i32 @free(i8* %98)
%100 = load i32*, i32** %8
%101 = call i32 @closedir(i32* %100)
store i32 0, i32* %4
br label %102
102: ; preds = %97, %46, %16
%103 = load i32, i32* %4
ret i32 %103
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"malloc\00"
define i32 @_chgrp_do_recursive_do(i32 %0, i32 %1, i8* %2) {
%4 = tail call i32* @opendir(i8* %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @_chgrp_error(i8* %2, i32 1)
br label %65
8: ; preds = %3
%9 = tail call %struct.dirent* @readdir(i32* %4)
%10 = tail call %struct.dirent* @readdir(i32* %4)
%11 = tail call i32 @strlen(i8* %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = add nsw i32 %11, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 47
%19 = select i1 %18, i32 1, i32 2
br label %20
20: ; preds = %13, %8
%21 = phi i32 [ 2, %8 ], [ %19, %13 ]
%22 = add nsw i32 %21, %11
%23 = tail call i8* @malloc(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = tail call i32 @closedir(i32* %4)
%27 = tail call i32 @_chgrp_error(i8* %2, i32 1)
br label %65
28: ; preds = %20
%29 = tail call i32 @strcpy(i8* %23, i8* %2)
%30 = add nsw i32 %22, -2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %23, i64 %31
store i8 47, i8* %32
%33 = add nsw i32 %22, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %23, i64 %34
store i8 0, i8* %35
%36 = tail call %struct.dirent* @readdir(i32* %4)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %61, label %38
38: ; preds = %28, %53
%39 = phi %struct.dirent* [ %59, %53 ], [ %36, %28 ]
%40 = phi i8* [ %47, %53 ], [ %23, %28 ]
br label %41
41: ; preds = %38, %49
%42 = phi %struct.dirent* [ %39, %38 ], [ %51, %49 ]
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i64 0, i32 0
%44 = load i8*, i8** %43
%45 = tail call i32 @strlen(i8* %44)
%46 = add nsw i32 %45, %22
%47 = tail call i8* @realloc(i8* %40, i32 %46)
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = tail call i32 @_chgrp_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
%51 = tail call %struct.dirent* @readdir(i32* %4)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %61, label %41
53: ; preds = %41
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i64 0, i32 0
%55 = load i8*, i8** %54
%56 = tail call i32 @strcat(i8* %47, i8* %55)
%57 = tail call i32 @_chgrp_do_recursive(i32 %0, i32 %1, i8* %47)
%58 = getelementptr inbounds i8, i8* %47, i64 %34
store i8 0, i8* %58
%59 = tail call %struct.dirent* @readdir(i32* %4)
%60 = icmp eq %struct.dirent* %59, null
br i1 %60, label %61, label %38
61: ; preds = %53, %49, %28
%62 = phi i8* [ %23, %28 ], [ %40, %49 ], [ %47, %53 ]
%63 = tail call i32 @free(i8* %62)
%64 = tail call i32 @closedir(i32* %4)
br label %65
65: ; preds = %61, %25, %6
%66 = phi i32 [ %7, %6 ], [ %27, %25 ], [ 0, %61 ]
ret i32 %66
}