#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 */
int chdir(char *);
int readlink(char *, char *, int);
int k_chbindir() {
  char dirbuf[256] = {0};
  int c = readlink("/proc/self/exe", dirbuf, 256);
  if (0 >= c) {
    return 1;
  }
  int index = c - 1;
  while (dirbuf[index] != '/' && 0 != index) {
    dirbuf[index--] = '\0';
  }
  if (dirbuf[index] != '/') {
    return 1;
  }
  if (0 != index) {
    dirbuf[index] = '\0';
  }
  return chdir(dirbuf);
}


x86 assembly (clang, -O0):
<k_chbindir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rsi
lea    0xea4(%rip),%rdi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmp    -0x114(%rbp),%eax
jl     1186 <k_chbindir+0x56>
movl   $0x1,-0x4(%rbp)
jmp    1249 <k_chbindir+0x119>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x118(%rbp)
movslq -0x118(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x119(%rbp)
je     11c6 <k_chbindir+0x96>
xor    %eax,%eax
cmp    -0x118(%rbp),%eax
setne  %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    11d9 <k_chbindir+0xa9>
jmp    11f9 <k_chbindir+0xc9>
mov    -0x118(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x118(%rbp)
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
jmp    1195 <k_chbindir+0x65>
movslq -0x118(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
je     121d <k_chbindir+0xed>
movl   $0x1,-0x4(%rbp)
jmp    1249 <k_chbindir+0x119>
xor    %eax,%eax
cmp    -0x118(%rbp),%eax
je     123a <k_chbindir+0x10a>
movslq -0x118(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<k_chbindir>:
sub    $0x108,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe68(%rip),%rdi
mov    %rsp,%rsi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jle    120a <k_chbindir+0xea>
add    $0xffffffff,%ecx
lea    (%rsp,%rcx,1),%rsi
setne  %dil
cmpb   $0x2f,(%rsp,%rcx,1)
setne  %dl
je     11f6 <k_chbindir+0xd6>
test   %ecx,%ecx
je     11f6 <k_chbindir+0xd6>
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rsp,%rcx,1)
lea    -0x1(%rcx),%rsi
cmp    $0x1,%rcx
setne  %dil
cmpb   $0x2f,-0x1(%rsp,%rcx,1)
setne  %dl
je     11f3 <k_chbindir+0xd3>
cmp    $0x1,%rcx
mov    %rsi,%rcx
jne    11d0 <k_chbindir+0xb0>
add    %rsp,%rsi
test   %dl,%dl
jne    120a <k_chbindir+0xea>
test   %dil,%dil
je     1202 <k_chbindir+0xe2>
movb   $0x0,(%rsi)
mov    %rsp,%rdi
call   1040 <chdir@plt>
add    $0x108,%rsp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
define i32 @k_chbindir() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = bitcast [256 x i8]* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %6, i32 256)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sge i32 0, %8
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 1, i32* %1
br label %49
11: ; preds = %0
%12 = load i32, i32* %3
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %4
br label %14
14: ; preds = %26, %11
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, 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 %24
21: ; preds = %14
%22 = load i32, i32* %4
%23 = icmp ne i32 0, %22
br label %24
24: ; preds = %21, %14
%25 = phi i1 [ false, %14 ], [ %23, %21 ]
br i1 %25, label %26, label %31
26: ; preds = %24
%27 = load i32, i32* %4
%28 = add nsw i32 %27, -1
store i32 %28, i32* %4
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %29
store i8 0, i8* %30
br label %14
31: ; preds = %24
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 47
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 1, i32* %1
br label %49
39: ; preds = %31
%40 = load i32, i32* %4
%41 = icmp ne i32 0, %40
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %44
store i8 0, i8* %45
br label %46
46: ; preds = %42, %39
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%48 = call i32 @chdir(i8* %47)
store i32 %48, i32* %1
br label %49
49: ; preds = %46, %38, %10
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
define i32 @k_chbindir() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %2, i32 256)
%4 = icmp slt i32 %3, 1
br i1 %4, label %30, label %5
5: ; preds = %0
%6 = add nsw i32 %3, -1
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 47
%11 = icmp ne i32 %6, 0
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %22
13: ; preds = %5, %13
%14 = phi i64 [ %16, %13 ], [ %7, %5 ]
%15 = phi i8* [ %17, %13 ], [ %8, %5 ]
store i8 0, i8* %15
%16 = add nsw i64 %14, -1
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 47
%20 = icmp ne i64 %16, 0
%21 = select i1 %19, i1 %20, i1 false
br i1 %21, label %13, label %22
22: ; preds = %13, %5
%23 = phi i8* [ %8, %5 ], [ %17, %13 ]
%24 = phi i1 [ %10, %5 ], [ %19, %13 ]
%25 = phi i1 [ %11, %5 ], [ %20, %13 ]
br i1 %24, label %30, label %26
26: ; preds = %22
br i1 %25, label %27, label %28
27: ; preds = %26
store i8 0, i8* %23
br label %28
28: ; preds = %27, %26
%29 = call i32 @chdir(i8* %2)
br label %30
30: ; preds = %28, %22, %0
%31 = phi i32 [ 1, %0 ], [ %29, %28 ], [ 1, %22 ]
ret i32 %31
}