#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 passwd {
  char *pw_dir;
};

/* Variables and functions */
#define EACCES 133
#define EFAULT 132
#define EIO 131
#define ENAMETOOLONG 130
#define ENOENT 129
#define ENOTDIR 128
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int chdir(char *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *get_current_dir();
struct passwd *getpwnam(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int internal_cd(int argc, char **argv) {
  uid_t uid = getuid();
  struct passwd *user = getpwuid(uid);
  char *name;
  char *p;
  char *chemin;
  char *oldpwd;
  int err;
  int ret;
  if (argc == 1) {
    chemin = malloc(sizeof(char) * (1 + strlen(user->pw_dir)));
    assert(chemin != NULL);
    strcpy(chemin, user->pw_dir);
  } else if ((argc == 2) && (argv[1][0] == '~')) {
    if ((argv[1][1] == '\0') || (argv[1][1] == '/')) {
      chemin = malloc(sizeof(char) * (strlen(user->pw_dir) + strlen(argv[1])));
      assert(chemin != NULL);
      sprintf(chemin, "%s%s", user->pw_dir, argv[1] + 1);
    } else {
      p = strchr(argv[1], '/');
      if (p == NULL) {
        name = malloc(sizeof(char) * (1 + strlen(argv[1] + 1)));
        assert(name != NULL);
        strcpy(name, argv[1] + 1);
      } else {
        name = malloc(sizeof(char) * (p - argv[1]));
        memcpy(name, argv[1] + 1, p - argv[1] - 1);
        name[p - argv[1] - 1] = '\0';
      }
      if (NULL == (user = getpwnam(name))) {
        fprintf(stderr, "Philsh: aucun utilisateur de ce nom : %s\n", name);
        free(name);
        return 2;
      }
      free(name);
      if (p == NULL) {
        chemin = malloc(sizeof(char) * (1 + strlen(user->pw_dir)));
        assert(chemin != NULL);
        strcpy(chemin, user->pw_dir);
      } else {
        chemin = malloc(sizeof(char) * (1 + strlen(user->pw_dir) + strlen(p)));
        assert(chemin != NULL);
        sprintf(chemin, "%s%s", user->pw_dir, p);
      }
    }
  } else {
    chemin = malloc(sizeof(char) * (1 + strlen(argv[1])));
    assert(chemin != NULL);
    strcpy(chemin, argv[1]);
  }
  oldpwd = get_current_dir();
  setenv("OLDPWD", oldpwd, 1);
  free(oldpwd);
  if (0 != (ret = chdir(chemin))) {
    err = errno;
    switch (err) {
    case EACCES:
      fprintf(stderr, "Philsh: %s permission non accordée\n", chemin);
      break;
    case EIO:
      fprintf(stderr, "Philsh: %s erreur d'entrée/sortie\n", chemin);
      break;
    case ENAMETOOLONG:
      fprintf(stderr, "Philsh: %s path trop long\n", chemin);
      break;
    case ENOENT:
      fprintf(stderr, "Philsh: aucun fichier ou dossier de ce type : %s\n",
              chemin);
      break;
    case EFAULT:
      fprintf(
          stderr,
          "Philsh: %s pointe en dehors de l'espace d'adressage accessible\n",
          chemin);
    case ENOTDIR:
      fprintf(stderr, "Philsh : %s n'est pas un dossier\n", chemin);
      break;
    default:
      fprintf(stderr, "Philsh : %s permission non accordée\n", chemin);
      break;
    }
  }
  free(chemin);
  return ret;
}


x86 assembly (clang, -O0):
<internal_cd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1266 <internal_cd+0x76>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
jmp    1548 <internal_cd+0x358>
cmpl   $0x2,-0x8(%rbp)
jne    14ff <internal_cd+0x30f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    14ff <internal_cd+0x30f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     12ae <internal_cd+0xbe>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    131c <internal_cd+0x12c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x1,%rcx
lea    0xd06(%rip),%rsi
call   1110 <sprintf@plt>
jmp    14fa <internal_cd+0x30a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2f,%esi
call   10b0 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    138e <internal_cd+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
add    $0x1,%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
add    $0x1,%rsi
call   1040 <strcpy@plt>
jmp    13f1 <internal_cd+0x201>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
sub    %rcx,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
add    $0x1,%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   10f0 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
mov    0x8(%rdx),%rdx
sub    %rdx,%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   10c0 <getpwnam@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    143a <internal_cd+0x24a>
mov    0x2bcd(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xbfd(%rip),%rsi
call   10e0 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    169a <internal_cd+0x4aa>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1495 <internal_cd+0x2a5>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
jmp    14f5 <internal_cd+0x305>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
add    %ecx,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%rcx
lea    0xb28(%rip),%rsi
call   1110 <sprintf@plt>
jmp    14fa <internal_cd+0x30a>
jmp    1543 <internal_cd+0x353>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1100 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10d0 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
jmp    1548 <internal_cd+0x358>
mov    $0x0,%al
call   1080 <get_current_dir@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xae9(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1090 <chdir@plt>
mov    %eax,%ecx
mov    %ecx,-0x48(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     168b <internal_cd+0x49b>
mov    0x2a48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x58(%rbp)
sub    $0x5,%eax
ja     166d <internal_cd+0x47d>
mov    -0x58(%rbp),%rax
lea    0xa4b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2a1b(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xa7c(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    1686 <internal_cd+0x496>
mov    0x29fd(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xa83(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    1686 <internal_cd+0x496>
mov    0x29df(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xa89(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    1686 <internal_cd+0x496>
mov    0x29c1(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xa86(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    1686 <internal_cd+0x496>
mov    0x29a3(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xa9a(%rip),%rsi
call   10e0 <fprintf@plt>
mov    0x298a(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xac1(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    1686 <internal_cd+0x496>
mov    0x296c(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xac5(%rip),%rsi
call   10e0 <fprintf@plt>
jmp    168b <internal_cd+0x49b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<internal_cd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   10a0 <getuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    %rax,%r15
cmp    $0x1,%ebx
je     1282 <internal_cd+0x92>
cmp    $0x2,%ebx
jne    12a8 <internal_cd+0xb8>
mov    0x8(%r14),%rbp
cmpb   $0x7e,0x0(%rbp)
jne    12ac <internal_cd+0xbc>
mov    0x1(%rbp),%al
cmp    $0x2f,%al
je     1239 <internal_cd+0x49>
test   %al,%al
jne    139c <internal_cd+0x1ac>
mov    (%r15),%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    0x8(%r14),%rdi
call   1070 <strlen@plt>
add    %eax,%ebx
mov    %ebx,%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    (%r15),%rdx
mov    0x8(%r14),%rcx
add    $0x1,%rcx
lea    0xda0(%rip),%rsi
mov    %rbx,%rdi
call   1110 <sprintf@plt>
jmp    12d9 <internal_cd+0xe9>
mov    (%r15),%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    (%r15),%rsi
jmp    12d1 <internal_cd+0xe1>
mov    0x8(%r14),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
xor    %eax,%eax
call   1080 <get_current_dir@plt>
mov    %rax,%rbp
lea    0xd5d(%rip),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1090 <chdir@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1389 <internal_cd+0x199>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x5,%eax
ja     133e <internal_cd+0x14e>
lea    0xd29(%rip),%rsi
lea    0xcd4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xd76(%rip),%rsi
jmp    1378 <internal_cd+0x188>
lea    0xe01(%rip),%rsi
jmp    1378 <internal_cd+0x188>
lea    0xd49(%rip),%rsi
jmp    1378 <internal_cd+0x188>
lea    0xd1c(%rip),%rsi
jmp    1378 <internal_cd+0x188>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    %rbx,%rdx
call   10e0 <fprintf@plt>
lea    0xdac(%rip),%rsi
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
call   10e0 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x1(%rbp),%r12
mov    %rbp,%rdi
mov    $0x2f,%esi
call   10b0 <strchr@plt>
mov    %rax,%r15
test   %rax,%rax
je     13e4 <internal_cd+0x1f4>
mov    %r15d,%edi
sub    %ebp,%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
not    %ebp
add    %r15d,%ebp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   10f0 <memcpy@plt>
mov    0x8(%r14),%rax
not    %rax
add    %r15,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1415 <internal_cd+0x225>
mov    %r12,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    0x8(%r14),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   10c0 <getpwnam@plt>
test   %rax,%rax
je     1478 <internal_cd+0x288>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x0(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
add    $0x1,%ebx
test   %r15,%r15
je     149a <internal_cd+0x2aa>
mov    %r15,%rdi
call   1070 <strlen@plt>
add    %eax,%ebx
mov    %ebx,%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    0x0(%rbp),%rdx
lea    0xbab(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rcx
jmp    127b <internal_cd+0x8b>
mov    0x2b61(%rip),%rax
mov    (%rax),%edi
lea    0xb95(%rip),%rsi
mov    %rbx,%rdx
call   10e0 <fprintf@plt>
mov    $0x2,%ebp
jmp    1389 <internal_cd+0x199>
mov    %ebx,%edi
call   1100 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10d0 <assert@plt>
mov    0x0(%rbp),%rsi
jmp    12d1 <internal_cd+0xe1>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Philsh: aucun utilisateur de ce nom : %s\0A\00"
@.str.2 = [7 x i8] c"OLDPWD\00"
@errno = global i32 0
@.str.3 = [37 x i8] c"Philsh: %s permission non accord\C3\A9e\0A\00"
@.str.4 = [36 x i8] c"Philsh: %s erreur d'entr\C3\A9e/sortie\0A\00"
@.str.5 = [27 x i8] c"Philsh: %s path trop long\0A\00"
@.str.6 = [50 x i8] c"Philsh: aucun fichier ou dossier de ce type : %s\0A\00"
@.str.7 = [64 x i8] c"Philsh: %s pointe en dehors de l'espace d'adressage accessible\0A\00"
@.str.8 = [34 x i8] c"Philsh : %s n'est pas un dossier\0A\00"
@.str.9 = [38 x i8] c"Philsh : %s permission non accord\C3\A9e\0A\00"
define i32 @internal_cd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.passwd*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 (...) @getuid()
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call %struct.passwd* @getpwuid(i32 %15)
store %struct.passwd* %16, %struct.passwd** %7
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %38
19: ; preds = %2
%20 = load %struct.passwd*, %struct.passwd** %7
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 1, %23
%25 = sext i32 %24 to i64
%26 = mul i64 1, %25
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = icmp ne i8* %29, null
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i8*, i8** %10
%34 = load %struct.passwd*, %struct.passwd** %7
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcpy(i8* %33, i8* %36)
br label %235
38: ; preds = %2
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %215
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 126
br i1 %48, label %49, label %215
49: ; preds = %41
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 0
br i1 %56, label %65, label %57
57: ; preds = %49
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 47
br i1 %64, label %65, label %92
65: ; preds = %57, %49
%66 = load %struct.passwd*, %struct.passwd** %7
%67 = getelementptr inbounds %struct.passwd, %struct.passwd* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @strlen(i8* %68)
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = call i32 @strlen(i8* %72)
%74 = add nsw i32 %69, %73
%75 = sext i32 %74 to i64
%76 = mul i64 1, %75
%77 = trunc i64 %76 to i32
%78 = call i8* @malloc(i32 %77)
store i8* %78, i8** %10
%79 = load i8*, i8** %10
%80 = icmp ne i8* %79, null
%81 = zext i1 %80 to i32
%82 = call i32 @assert(i32 %81)
%83 = load i8*, i8** %10
%84 = load %struct.passwd*, %struct.passwd** %7
%85 = getelementptr inbounds %struct.passwd, %struct.passwd* %84, i32 0, i32 0
%86 = load i8*, i8** %85
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = getelementptr inbounds i8, i8* %89, i64 1
%91 = call i32 @sprintf(i8* %83, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %86, i8* %90)
br label %214
92: ; preds = %57
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 1
%95 = load i8*, i8** %94
%96 = call i8* @strchr(i8* %95, i8 signext 47)
store i8* %96, i8** %9
%97 = load i8*, i8** %9
%98 = icmp eq i8* %97, null
br i1 %98, label %99, label %120
99: ; preds = %92
%100 = load i8**, i8*** %5
%101 = getelementptr inbounds i8*, i8** %100, i64 1
%102 = load i8*, i8** %101
%103 = getelementptr inbounds i8, i8* %102, i64 1
%104 = call i32 @strlen(i8* %103)
%105 = add nsw i32 1, %104
%106 = sext i32 %105 to i64
%107 = mul i64 1, %106
%108 = trunc i64 %107 to i32
%109 = call i8* @malloc(i32 %108)
store i8* %109, i8** %8
%110 = load i8*, i8** %8
%111 = icmp ne i8* %110, null
%112 = zext i1 %111 to i32
%113 = call i32 @assert(i32 %112)
%114 = load i8*, i8** %8
%115 = load i8**, i8*** %5
%116 = getelementptr inbounds i8*, i8** %115, i64 1
%117 = load i8*, i8** %116
%118 = getelementptr inbounds i8, i8* %117, i64 1
%119 = call i32 @strcpy(i8* %114, i8* %118)
br label %156
120: ; preds = %92
%121 = load i8*, i8** %9
%122 = load i8**, i8*** %5
%123 = getelementptr inbounds i8*, i8** %122, i64 1
%124 = load i8*, i8** %123
%125 = ptrtoint i8* %121 to i64
%126 = ptrtoint i8* %124 to i64
%127 = sub i64 %125, %126
%128 = mul i64 1, %127
%129 = trunc i64 %128 to i32
%130 = call i8* @malloc(i32 %129)
store i8* %130, i8** %8
%131 = load i8*, i8** %8
%132 = load i8**, i8*** %5
%133 = getelementptr inbounds i8*, i8** %132, i64 1
%134 = load i8*, i8** %133
%135 = getelementptr inbounds i8, i8* %134, i64 1
%136 = load i8*, i8** %9
%137 = load i8**, i8*** %5
%138 = getelementptr inbounds i8*, i8** %137, i64 1
%139 = load i8*, i8** %138
%140 = ptrtoint i8* %136 to i64
%141 = ptrtoint i8* %139 to i64
%142 = sub i64 %140, %141
%143 = sub nsw i64 %142, 1
%144 = trunc i64 %143 to i32
%145 = call i32 @memcpy(i8* %131, i8* %135, i32 %144)
%146 = load i8*, i8** %8
%147 = load i8*, i8** %9
%148 = load i8**, i8*** %5
%149 = getelementptr inbounds i8*, i8** %148, i64 1
%150 = load i8*, i8** %149
%151 = ptrtoint i8* %147 to i64
%152 = ptrtoint i8* %150 to i64
%153 = sub i64 %151, %152
%154 = sub nsw i64 %153, 1
%155 = getelementptr inbounds i8, i8* %146, i64 %154
store i8 0, i8* %155
br label %156
156: ; preds = %120, %99
%157 = load i8*, i8** %8
%158 = call %struct.passwd* @getpwnam(i8* %157)
store %struct.passwd* %158, %struct.passwd** %7
%159 = icmp eq %struct.passwd* null, %158
br i1 %159, label %160, label %166
160: ; preds = %156
%161 = load i32, i32* @stderr
%162 = load i8*, i8** %8
%163 = call i32 @fprintf(i32 %161, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %162)
%164 = load i8*, i8** %8
%165 = call i32 @free(i8* %164)
store i32 2, i32* %3
br label %280
166: ; preds = %156
%167 = load i8*, i8** %8
%168 = call i32 @free(i8* %167)
%169 = load i8*, i8** %9
%170 = icmp eq i8* %169, null
br i1 %170, label %171, label %190
171: ; preds = %166
%172 = load %struct.passwd*, %struct.passwd** %7
%173 = getelementptr inbounds %struct.passwd, %struct.passwd* %172, i32 0, i32 0
%174 = load i8*, i8** %173
%175 = call i32 @strlen(i8* %174)
%176 = add nsw i32 1, %175
%177 = sext i32 %176 to i64
%178 = mul i64 1, %177
%179 = trunc i64 %178 to i32
%180 = call i8* @malloc(i32 %179)
store i8* %180, i8** %10
%181 = load i8*, i8** %10
%182 = icmp ne i8* %181, null
%183 = zext i1 %182 to i32
%184 = call i32 @assert(i32 %183)
%185 = load i8*, i8** %10
%186 = load %struct.passwd*, %struct.passwd** %7
%187 = getelementptr inbounds %struct.passwd, %struct.passwd* %186, i32 0, i32 0
%188 = load i8*, i8** %187
%189 = call i32 @strcpy(i8* %185, i8* %188)
br label %213
190: ; preds = %166
%191 = load %struct.passwd*, %struct.passwd** %7
%192 = getelementptr inbounds %struct.passwd, %struct.passwd* %191, i32 0, i32 0
%193 = load i8*, i8** %192
%194 = call i32 @strlen(i8* %193)
%195 = add nsw i32 1, %194
%196 = load i8*, i8** %9
%197 = call i32 @strlen(i8* %196)
%198 = add nsw i32 %195, %197
%199 = sext i32 %198 to i64
%200 = mul i64 1, %199
%201 = trunc i64 %200 to i32
%202 = call i8* @malloc(i32 %201)
store i8* %202, i8** %10
%203 = load i8*, i8** %10
%204 = icmp ne i8* %203, null
%205 = zext i1 %204 to i32
%206 = call i32 @assert(i32 %205)
%207 = load i8*, i8** %10
%208 = load %struct.passwd*, %struct.passwd** %7
%209 = getelementptr inbounds %struct.passwd, %struct.passwd* %208, i32 0, i32 0
%210 = load i8*, i8** %209
%211 = load i8*, i8** %9
%212 = call i32 @sprintf(i8* %207, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %210, i8* %211)
br label %213
213: ; preds = %190, %171
br label %214
214: ; preds = %213, %65
br label %234
215: ; preds = %41, %38
%216 = load i8**, i8*** %5
%217 = getelementptr inbounds i8*, i8** %216, i64 1
%218 = load i8*, i8** %217
%219 = call i32 @strlen(i8* %218)
%220 = add nsw i32 1, %219
%221 = sext i32 %220 to i64
%222 = mul i64 1, %221
%223 = trunc i64 %222 to i32
%224 = call i8* @malloc(i32 %223)
store i8* %224, i8** %10
%225 = load i8*, i8** %10
%226 = icmp ne i8* %225, null
%227 = zext i1 %226 to i32
%228 = call i32 @assert(i32 %227)
%229 = load i8*, i8** %10
%230 = load i8**, i8*** %5
%231 = getelementptr inbounds i8*, i8** %230, i64 1
%232 = load i8*, i8** %231
%233 = call i32 @strcpy(i8* %229, i8* %232)
br label %234
234: ; preds = %215, %214
br label %235
235: ; preds = %234, %19
%236 = call i8* (...) @get_current_dir()
store i8* %236, i8** %11
%237 = load i8*, i8** %11
%238 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %237, i32 1)
%239 = load i8*, i8** %11
%240 = call i32 @free(i8* %239)
%241 = load i8*, i8** %10
%242 = call i32 @chdir(i8* %241)
store i32 %242, i32* %13
%243 = icmp ne i32 0, %242
br i1 %243, label %244, label %276
244: ; preds = %235
%245 = load i32, i32* @errno
store i32 %245, i32* %12
%246 = load i32, i32* %12
switch i32 %246, label %271 [
i32 133, label %247
i32 131, label %251
i32 130, label %255
i32 129, label %259
i32 132, label %263
i32 128, label %267
]
247: ; preds = %244
%248 = load i32, i32* @stderr
%249 = load i8*, i8** %10
%250 = call i32 @fprintf(i32 %248, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %249)
br label %275
251: ; preds = %244
%252 = load i32, i32* @stderr
%253 = load i8*, i8** %10
%254 = call i32 @fprintf(i32 %252, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %253)
br label %275
255: ; preds = %244
%256 = load i32, i32* @stderr
%257 = load i8*, i8** %10
%258 = call i32 @fprintf(i32 %256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %257)
br label %275
259: ; preds = %244
%260 = load i32, i32* @stderr
%261 = load i8*, i8** %10
%262 = call i32 @fprintf(i32 %260, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i8* %261)
br label %275
263: ; preds = %244
%264 = load i32, i32* @stderr
%265 = load i8*, i8** %10
%266 = call i32 @fprintf(i32 %264, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.7, i64 0, i64 0), i8* %265)
br label %267
267: ; preds = %244, %263
%268 = load i32, i32* @stderr
%269 = load i8*, i8** %10
%270 = call i32 @fprintf(i32 %268, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i8* %269)
br label %275
271: ; preds = %244
%272 = load i32, i32* @stderr
%273 = load i8*, i8** %10
%274 = call i32 @fprintf(i32 %272, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.9, i64 0, i64 0), i8* %273)
br label %275
275: ; preds = %271, %267, %259, %255, %251, %247
br label %276
276: ; preds = %275, %235
%277 = load i8*, i8** %10
%278 = call i32 @free(i8* %277)
%279 = load i32, i32* %13
store i32 %279, i32* %3
br label %280
280: ; preds = %276, %160
%281 = load i32, i32* %3
ret i32 %281
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Philsh: aucun utilisateur de ce nom : %s\0A\00"
@.str.2 = [7 x i8] c"OLDPWD\00"
@errno = global i32 0
@.str.3 = [37 x i8] c"Philsh: %s permission non accord\C3\A9e\0A\00"
@.str.4 = [36 x i8] c"Philsh: %s erreur d'entr\C3\A9e/sortie\0A\00"
@.str.5 = [27 x i8] c"Philsh: %s path trop long\0A\00"
@.str.6 = [50 x i8] c"Philsh: aucun fichier ou dossier de ce type : %s\0A\00"
@.str.7 = [64 x i8] c"Philsh: %s pointe en dehors de l'espace d'adressage accessible\0A\00"
@.str.8 = [34 x i8] c"Philsh : %s n'est pas un dossier\0A\00"
@.str.9 = [38 x i8] c"Philsh : %s permission non accord\C3\A9e\0A\00"
define i32 @internal_cd(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @getuid()
%4 = tail call %struct.passwd* @getpwuid(i32 %3)
switch i32 %0, label %5 [
i32 1, label %8
i32 2, label %19
]
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
br label %99
8: ; preds = %2
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = tail call i8* @malloc(i32 %12)
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = load i8*, i8** %9
%18 = tail call i32 @strcpy(i8* %13, i8* %17)
br label %110
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 126
br i1 %23, label %24, label %99
24: ; preds = %19
%25 = getelementptr inbounds i8, i8* %21, i64 1
%26 = load i8, i8* %25
switch i8 %26, label %42 [
i8 0, label %27
i8 47, label %27
]
27: ; preds = %24, %24
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = tail call i32 @strlen(i8* %29)
%31 = load i8*, i8** %20
%32 = tail call i32 @strlen(i8* %31)
%33 = add nsw i32 %32, %30
%34 = tail call i8* @malloc(i32 %33)
%35 = icmp ne i8* %34, null
%36 = zext i1 %35 to i32
%37 = tail call i32 @assert(i32 %36)
%38 = load i8*, i8** %28
%39 = load i8*, i8** %20
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = tail call i32 @sprintf(i8* dereferenceable(1) %34, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %38, i8* %40)
br label %110
42: ; preds = %24
%43 = tail call i8* @strchr(i8* %21, i8 signext 47)
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = tail call i32 @strlen(i8* %25)
%47 = add nsw i32 %46, 1
%48 = tail call i8* @malloc(i32 %47)
%49 = icmp ne i8* %48, null
%50 = zext i1 %49 to i32
%51 = tail call i32 @assert(i32 %50)
%52 = load i8*, i8** %20
%53 = getelementptr inbounds i8, i8* %52, i64 1
%54 = tail call i32 @strcpy(i8* %48, i8* %53)
br label %70
55: ; preds = %42
%56 = ptrtoint i8* %43 to i64
%57 = ptrtoint i8* %21 to i64
%58 = sub i64 %56, %57
%59 = trunc i64 %58 to i32
%60 = tail call i8* @malloc(i32 %59)
%61 = xor i64 %57, -1
%62 = add i64 %56, %61
%63 = trunc i64 %62 to i32
%64 = tail call i32 @memcpy(i8* %60, i8* %25, i32 %63)
%65 = load i8*, i8** %20
%66 = ptrtoint i8* %65 to i64
%67 = xor i64 %66, -1
%68 = add i64 %67, %56
%69 = getelementptr inbounds i8, i8* %60, i64 %68
store i8 0, i8* %69
br label %70
70: ; preds = %55, %45
%71 = phi i8* [ %48, %45 ], [ %60, %55 ]
%72 = tail call %struct.passwd* @getpwnam(i8* %71)
%73 = icmp eq %struct.passwd* %72, null
br i1 %73, label %74, label %77
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = tail call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %71)
br label %131
77: ; preds = %70
%78 = tail call i32 @free(i8* %71)
%79 = getelementptr inbounds %struct.passwd, %struct.passwd* %72, i64 0, i32 0
%80 = load i8*, i8** %79
%81 = tail call i32 @strlen(i8* %80)
%82 = add nsw i32 %81, 1
br i1 %44, label %83, label %90
83: ; preds = %77
%84 = tail call i8* @malloc(i32 %82)
%85 = icmp ne i8* %84, null
%86 = zext i1 %85 to i32
%87 = tail call i32 @assert(i32 %86)
%88 = load i8*, i8** %79
%89 = tail call i32 @strcpy(i8* %84, i8* %88)
br label %110
90: ; preds = %77
%91 = tail call i32 @strlen(i8* %43)
%92 = add nsw i32 %82, %91
%93 = tail call i8* @malloc(i32 %92)
%94 = icmp ne i8* %93, null
%95 = zext i1 %94 to i32
%96 = tail call i32 @assert(i32 %95)
%97 = load i8*, i8** %79
%98 = tail call i32 @sprintf(i8* dereferenceable(1) %93, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %97, i8* %43)
br label %110
99: ; preds = %5, %19
%100 = phi i8* [ %7, %5 ], [ %21, %19 ]
%101 = getelementptr inbounds i8*, i8** %1, i64 1
%102 = tail call i32 @strlen(i8* %100)
%103 = add nsw i32 %102, 1
%104 = tail call i8* @malloc(i32 %103)
%105 = icmp ne i8* %104, null
%106 = zext i1 %105 to i32
%107 = tail call i32 @assert(i32 %106)
%108 = load i8*, i8** %101
%109 = tail call i32 @strcpy(i8* %104, i8* %108)
br label %110
110: ; preds = %99, %83, %90, %27, %8
%111 = phi i8* [ %13, %8 ], [ %34, %27 ], [ %84, %83 ], [ %93, %90 ], [ %104, %99 ]
%112 = tail call i8* (...) @get_current_dir()
%113 = tail call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %112, i32 1)
%114 = tail call i32 @free(i8* %112)
%115 = tail call i32 @chdir(i8* %111)
%116 = icmp eq i32 %115, 0
br i1 %116, label %131, label %117
117: ; preds = %110
%118 = load i32, i32* @errno
switch i32 %118, label %126 [
i32 133, label %127
i32 131, label %119
i32 130, label %120
i32 129, label %121
i32 132, label %122
i32 128, label %125
]
119: ; preds = %117
br label %127
120: ; preds = %117
br label %127
121: ; preds = %117
br label %127
122: ; preds = %117
%123 = load i32, i32* @stderr
%124 = tail call i32 @fprintf(i32 %123, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.7, i64 0, i64 0), i8* %111)
br label %125
125: ; preds = %117, %122
br label %127
126: ; preds = %117
br label %127
127: ; preds = %117, %126, %125, %121, %120, %119
%128 = phi i8* [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), %119 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), %120 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), %121 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), %125 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.9, i64 0, i64 0), %126 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), %117 ]
%129 = load i32, i32* @stderr
%130 = tail call i32 @fprintf(i32 %129, i8* %128, i8* %111)
br label %131
131: ; preds = %110, %127, %74
%132 = phi i8* [ %71, %74 ], [ %111, %127 ], [ %111, %110 ]
%133 = phi i32 [ 2, %74 ], [ %115, %127 ], [ %115, %110 ]
%134 = tail call i32 @free(i8* %132)
ret i32 %133
}