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

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ do_cmd(char *);
int /*<<< orphan*/ perror(char *);
size_t strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int cmd_handler(char *line) {
  size_t n = strlen(line);
  line[n - 1] = '\0';
  if (strncmp(line, "cd", 2) == 0) {
    if (chdir(line + 3) < 0) {
      perror("chdir");
      goto err;
    }
  } else {
    do_cmd(line);
  }
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<cmd_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11c5 <cmd_handler+0x75>
mov    -0x10(%rbp),%rdi
add    $0x3,%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    11c0 <cmd_handler+0x70>
lea    0xe4d(%rip),%rdi
call   1070 <perror@plt>
jmp    11da <cmd_handler+0x8a>
jmp    11ce <cmd_handler+0x7e>
mov    -0x10(%rbp),%rdi
call   1060 <do_cmd@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <cmd_handler+0x91>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_handler>:
push   %rbx
mov    %rdi,%rbx
call   1040 <strlen@plt>
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1183 <cmd_handler+0x33>
mov    %rbx,%rdi
call   1060 <do_cmd@plt>
xor    %eax,%eax
jmp    1199 <cmd_handler+0x49>
add    $0x3,%rbx
mov    %rbx,%rdi
call   1050 <chdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     119b <cmd_handler+0x4b>
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"cd\00"
@.str.1 = [6 x i8] c"chdir\00"
define i32 @cmd_handler(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %4
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = sub i64 %8, 1
%10 = getelementptr inbounds i8, i8* %7, i64 %9
store i8 0, i8* %10
%11 = load i8*, i8** %3
%12 = call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 3
%17 = call i64 @chdir(i8* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %26
21: ; preds = %14
br label %25
22: ; preds = %1
%23 = load i8*, i8** %3
%24 = call i32 @do_cmd(i8* %23)
br label %25
25: ; preds = %22, %21
store i32 0, i32* %2
br label %27
26: ; preds = %19
store i32 -1, i32* %2
br label %27
27: ; preds = %26, %25
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [3 x i8] c"cd\00"
@.str.1 = [6 x i8] c"chdir\00"
define i32 @cmd_handler(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, -1
%4 = getelementptr inbounds i8, i8* %0, i64 %3
store i8 0, i8* %4
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %0, i64 3
%9 = tail call i64 @chdir(i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %1
%14 = tail call i32 @do_cmd(i8* %0)
br label %15
15: ; preds = %13, %7, %11
%16 = phi i32 [ -1, %11 ], [ 0, %7 ], [ 0, %13 ]
ret i32 %16
}