#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
struct stat *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int check_and_open(const char *url, const char *document_root) {
  int fd;
  char *path;
  struct stat *s;
  s = malloc(sizeof(struct stat));
  int status;
  path = (char *)malloc(strlen(url) + strlen(document_root) + 1);
  strcpy(path, document_root);
  strcat(path, url);
  printf("av st path = %s \n", path);
  status = stat(path, s);
  if (status == -1) {
    printf("a1 \n");
    printf("path = \"%s\"  \n", path);
    perror("stat");
    return -1;
  }
  if (S_ISDIR(s->st_mode))
    printf("dossier.");
  if (S_ISREG(s->st_mode))
    printf("fichier. ");
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    printf("b1 \n");
    printf("path = \"%s\"  \n", path);
    perror("open");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<check_and_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x4,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x28(%rbp),%rsi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1262 <check_and_open+0xc2>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdd5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <check_and_open+0x165>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1285 <check_and_open+0xe5>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%rax
je     12a8 <check_and_open+0x108>
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ff <check_and_open+0x15f>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd55(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <check_and_open+0x165>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_open>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
mov    %rdi,%r12
mov    $0x4,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <strcat@plt>
lea    0xdf7(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1279 <check_and_open+0xc9>
mov    (%r14),%ebx
mov    %ebx,%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1240 <check_and_open+0x90>
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     125a <check_and_open+0xaa>
lea    0xddc(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1289 <check_and_open+0xd9>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xda1(%rip),%rbx
lea    0xdbb(%rip),%rdi
jmp    1297 <check_and_open+0xe7>
lea    0xda9(%rip),%rbx
lea    0xda7(%rip),%rdi
call   1040 <puts@plt>
lea    0xd6f(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%eax
jmp    126f <check_and_open+0xbf>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"av st path = %s \0A\00"
@.str.1 = [5 x i8] c"a1 \0A\00"
@.str.2 = [15 x i8] c"path = \22%s\22 \0A\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [9 x i8] c"dossier.\00"
@.str.5 = [10 x i8] c"fichier. \00"
@O_RDONLY = global i32 0
@.str.6 = [5 x i8] c"b1 \0A\00"
@.str.7 = [5 x i8] c"open\00"
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call %struct.stat* @malloc(i32 4)
store %struct.stat* %10, %struct.stat** %8
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %12, %14
%16 = add nsw i32 %15, 1
%17 = call %struct.stat* @malloc(i32 %16)
%18 = bitcast %struct.stat* %17 to i8*
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** %5
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = call i32 @strcat(i8* %22, i8* %23)
%25 = load i8*, i8** %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %7
%28 = load %struct.stat*, %struct.stat** %8
%29 = call i32 @stat(i8* %27, %struct.stat* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %2
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i8*, i8** %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
37: ; preds = %2
%38 = load %struct.stat*, %struct.stat** %8
%39 = getelementptr inbounds %struct.stat, %struct.stat* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @S_ISDIR(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = load %struct.stat*, %struct.stat** %8
%47 = getelementptr inbounds %struct.stat, %struct.stat* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @S_ISREG(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
%54 = load i8*, i8** %7
%55 = load i32, i32* @O_RDONLY
%56 = call i32 @open(i8* %54, i32 %55)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%61 = load i8*, i8** %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
64: ; preds = %53
%65 = load i32, i32* %6
store i32 %65, i32* %3
br label %66
66: ; preds = %64, %59, %32
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"av st path = %s \0A\00"
@.str.2 = [15 x i8] c"path = \22%s\22 \0A\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [9 x i8] c"dossier.\00"
@.str.5 = [10 x i8] c"fichier. \00"
@O_RDONLY = global i32 0
@.str.7 = [5 x i8] c"open\00"
@str = [4 x i8] c"b1 \00"
@str.8 = [4 x i8] c"a1 \00"
define i32 @check_and_open(i8* %0, i8* %1) {
%3 = tail call %struct.stat* @malloc(i32 4)
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %4, 1
%7 = add i32 %6, %5
%8 = tail call %struct.stat* @malloc(i32 %7)
%9 = bitcast %struct.stat* %8 to i8*
%10 = tail call i32 @strcpy(i8* %9, i8* %1)
%11 = tail call i32 @strcat(i8* %9, i8* %0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.stat* %8)
%13 = tail call i32 @stat(i8* %9, %struct.stat* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %31, label %15
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i64 @S_ISDIR(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = tail call i64 @S_ISREG(i32 %17)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = load i32, i32* @O_RDONLY
%29 = tail call i32 @open(i8* %9, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %27, %2
%32 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @str.8, i64 0, i64 0), %2 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0), %27 ]
%33 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), %27 ]
%34 = tail call i32 @puts(i8* dereferenceable(1) %32)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %8)
%36 = tail call i32 @perror(i8* %33)
br label %37
37: ; preds = %31, %27
%38 = phi i32 [ %29, %27 ], [ -1, %31 ]
ret i32 %38
}