#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 dummy;
};

/* Variables and functions */
int S_IRGRP;
int S_IROTH;
int S_IRWXU;
int S_IXGRP;
int S_IXOTH;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gettext(char *);
int mkdir(char *, int);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int check_and_create(char *dir) {
  int tmp;
  struct stat s;
  tmp = stat(dir, &s);
  if (tmp == -1) {
    fprintf(stderr, gettext("Directory '%s' not found, creating it...\n"), dir);
    tmp = mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    if (tmp == -1) {
      fprintf(stderr, gettext("Error while creating directory '%s': "), dir);
      perror(NULL);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_and_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1221 <check_and_create+0xd1>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe77(%rip),%rdi
call   1040 <gettext@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rsi
mov    -0x10(%rbp),%rdx
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121c <check_and_create+0xcc>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xe37(%rip),%rdi
call   1040 <gettext@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rsi
mov    -0x10(%rbp),%rdx
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <check_and_create+0xd8>
jmp    1221 <check_and_create+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    11c7 <check_and_create+0x77>
mov    0x2e58(%rip),%r15
mov    (%r15),%r14d
lea    0xe86(%rip),%rdi
call   1040 <gettext@plt>
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    0x2e54(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
je     11d4 <check_and_create+0x84>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%ebp
lea    0xe4c(%rip),%rdi
call   1040 <gettext@plt>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11c7 <check_and_create+0x77>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Directory '%s' not found, creating it...\0A\00"
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str.1 = [38 x i8] c"Error while creating directory '%s': \00"
define i32 @check_and_create(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @stat(i8* %6, %struct.stat* %5)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %35
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i8* @gettext(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %3
%14 = call i32 @fprintf(i32 %11, i8* %12, i8* %13)
%15 = load i8*, i8** %3
%16 = load i32, i32* @S_IRWXU
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IXGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IXOTH
%24 = or i32 %22, %23
%25 = call i32 @mkdir(i8* %15, i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %10
%29 = load i32, i32* @stderr
%30 = call i8* @gettext(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%31 = load i8*, i8** %3
%32 = call i32 @fprintf(i32 %29, i8* %30, i8* %31)
%33 = call i32 @perror(i32* null)
store i32 -1, i32* %2
br label %36
34: ; preds = %10
br label %35
35: ; preds = %34, %1
store i32 0, i32* %2
br label %36
36: ; preds = %35, %28
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Directory '%s' not found, creating it...\0A\00"
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str.1 = [38 x i8] c"Error while creating directory '%s': \00"
define i32 @check_and_create(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %26
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i8* @gettext(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @fprintf(i32 %7, i8* %8, i8* %0)
%10 = load i32, i32* @S_IRWXU
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IXGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IXOTH
%18 = or i32 %16, %17
%19 = tail call i32 @mkdir(i8* %0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %6
%22 = load i32, i32* @stderr
%23 = tail call i8* @gettext(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @fprintf(i32 %22, i8* %23, i8* %0)
%25 = tail call i32 @perror(i32* null)
br label %26
26: ; preds = %1, %6, %21
%27 = phi i32 [ -1, %21 ], [ 0, %6 ], [ 0, %1 ]
ret i32 %27
}