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

/* Variables and functions */
int /*<<< orphan*/ ChangeDir(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ DeleteDir(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ MakeDir(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ SendAll(int, char *, int /*<<< orphan*/);
char *addStr(char *, char *);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
char *readString(char *, char **);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void DoOpDir(int fd, char *data) {
  char *dirName = NULL;
  data = readString(data, &dirName);
  if (dirName[strlen(dirName) - 1] != '/') {
    char *temp = dirName;
    dirName = addStr(dirName, "/");
    free(temp);
  }
  char *opCode = NULL;
  data = readString(data, &opCode);
  printf("OpDir, dirname:%s, opCode:%s \n", dirName, opCode);
  char *buf = NULL;
  uint32_t sendLen = 0;
  if (strcmp(opCode, "mkdir") == 0) {
    MakeDir(dirName, &buf, &sendLen);
  } else if (strcmp(opCode, "rmdir") == 0) {
    DeleteDir(dirName, &buf, &sendLen);
  } else if (strcmp(opCode, "chdir") == 0) {
    ChangeDir(dirName, &buf, &sendLen);
  } else {
    printf("invalid dir opcode, dirname:%s, opcode:%s \n", dirName, opCode);
    assert(0);
  }
  SendAll(fd, buf, sendLen);
  free(dirName);
  free(opCode);
  free(buf);
}


x86 assembly (clang, -O0):
<DoOpDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <readString@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1227 <DoOpDir+0x77>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1060 <addStr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <readString@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdb3(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdb3(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1293 <DoOpDir+0xe3>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <MakeDir@plt>
jmp    1318 <DoOpDir+0x168>
mov    -0x28(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12c3 <DoOpDir+0x113>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1090 <DeleteDir@plt>
jmp    1313 <DoOpDir+0x163>
mov    -0x28(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12f3 <DoOpDir+0x143>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10b0 <ChangeDir@plt>
jmp    130e <DoOpDir+0x15e>
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd31(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <assert@plt>
jmp    1313 <DoOpDir+0x163>
jmp    1318 <DoOpDir+0x168>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10d0 <SendAll@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DoOpDir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
movq   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %rax,%rdi
call   10a0 <readString@plt>
mov    %rax,%rbx
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbp,1)
je     1208 <DoOpDir+0x58>
mov    (%rsp),%rbp
lea    0xe0c(%rip),%rsi
mov    %rbp,%rdi
call   1060 <addStr@plt>
mov    %rax,(%rsp)
mov    %rbp,%rdi
call   1030 <free@plt>
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <readString@plt>
mov    (%rsp),%rsi
mov    0x10(%rsp),%rdx
lea    0xdd4(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,0x18(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x10(%rsp),%rdi
lea    0xdd1(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12a4 <DoOpDir+0xf4>
mov    0x10(%rsp),%rdi
lea    0xdc1(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12b9 <DoOpDir+0x109>
mov    0x10(%rsp),%rdi
lea    0xdb1(%rip),%rsi
call   1070 <strcmp@plt>
mov    (%rsp),%rsi
test   %rax,%rax
je     12ce <DoOpDir+0x11e>
mov    0x10(%rsp),%rdx
lea    0xd9d(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <assert@plt>
jmp    12e3 <DoOpDir+0x133>
mov    (%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10c0 <MakeDir@plt>
jmp    12e3 <DoOpDir+0x133>
mov    (%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1090 <DeleteDir@plt>
jmp    12e3 <DoOpDir+0x133>
lea    0x18(%rsp),%rax
lea    0xc(%rsp),%rdx
mov    %rsi,%rdi
mov    %rax,%rsi
call   10b0 <ChangeDir@plt>
mov    0x18(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %r14d,%edi
call   10d0 <SendAll@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [31 x i8] c"OpDir, dirname:%s, opCode:%s \0A\00"
@.str.2 = [6 x i8] c"mkdir\00"
@.str.3 = [6 x i8] c"rmdir\00"
@.str.4 = [6 x i8] c"chdir\00"
@.str.5 = [44 x i8] c"invalid dir opcode, dirname:%s, opcode:%s \0A\00"
define void @DoOpDir(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8* null, i8** %5
%10 = load i8*, i8** %4
%11 = call i8* @readString(i8* %10, i8** %5)
store i8* %11, i8** %4
%12 = load i8*, i8** %5
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 47
br i1 %20, label %21, label %27
21: ; preds = %2
%22 = load i8*, i8** %5
store i8* %22, i8** %6
%23 = load i8*, i8** %5
%24 = call i8* @addStr(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %24, i8** %5
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %21, %2
store i8* null, i8** %7
%28 = load i8*, i8** %4
%29 = call i8* @readString(i8* %28, i8** %7)
store i8* %29, i8** %4
%30 = load i8*, i8** %5
%31 = load i8*, i8** %7
%32 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
store i8* null, i8** %8
store i32 0, i32* %9
%33 = load i8*, i8** %7
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i8*, i8** %5
%38 = call i32 @MakeDir(i8* %37, i8** %8, i32* %9)
br label %60
39: ; preds = %27
%40 = load i8*, i8** %7
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8*, i8** %5
%45 = call i32 @DeleteDir(i8* %44, i8** %8, i32* %9)
br label %59
46: ; preds = %39
%47 = load i8*, i8** %7
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i8*, i8** %5
%52 = call i32 @ChangeDir(i8* %51, i8** %8, i32* %9)
br label %58
53: ; preds = %46
%54 = load i8*, i8** %5
%55 = load i8*, i8** %7
%56 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %54, i8* %55)
%57 = call i32 @assert(i32 0)
br label %58
58: ; preds = %53, %50
br label %59
59: ; preds = %58, %43
br label %60
60: ; preds = %59, %36
%61 = load i32, i32* %3
%62 = load i8*, i8** %8
%63 = load i32, i32* %9
%64 = call i32 @SendAll(i32 %61, i8* %62, i32 %63)
%65 = load i8*, i8** %5
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %7
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %8
%70 = call i32 @free(i8* %69)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [31 x i8] c"OpDir, dirname:%s, opCode:%s \0A\00"
@.str.2 = [6 x i8] c"mkdir\00"
@.str.3 = [6 x i8] c"rmdir\00"
@.str.4 = [6 x i8] c"chdir\00"
@.str.5 = [44 x i8] c"invalid dir opcode, dirname:%s, opcode:%s \0A\00"
define void @DoOpDir(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = call i8* @readString(i8* %1, i8** %3)
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i8* @addStr(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %3
%19 = call i32 @free(i8* %17)
br label %20
20: ; preds = %16, %2
%21 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%22 = call i8* @readString(i8* %8, i8** %4)
%23 = load i8*, i8** %3
%24 = load i8*, i8** %4
%25 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %24)
%26 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%27 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%28 = load i8*, i8** %4
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i8*, i8** %3
%33 = call i32 @MakeDir(i8* %32, i8** %5, i32* %6)
br label %52
34: ; preds = %20
%35 = load i8*, i8** %4
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i8*, i8** %3
%40 = call i32 @DeleteDir(i8* %39, i8** %5, i32* %6)
br label %52
41: ; preds = %34
%42 = load i8*, i8** %4
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
%45 = load i8*, i8** %3
br i1 %44, label %46, label %48
46: ; preds = %41
%47 = call i32 @ChangeDir(i8* %45, i8** %5, i32* %6)
br label %52
48: ; preds = %41
%49 = load i8*, i8** %4
%50 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i8* %45, i8* %49)
%51 = call i32 @assert(i32 0)
br label %52
52: ; preds = %38, %48, %46, %31
%53 = load i8*, i8** %5
%54 = load i32, i32* %6
%55 = call i32 @SendAll(i32 %0, i8* %53, i32 %54)
%56 = load i8*, i8** %3
%57 = call i32 @free(i8* %56)
%58 = load i8*, i8** %4
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %5
%61 = call i32 @free(i8* %60)
ret void
}