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

/* Variables and functions */
int EINVAL;
int ENOMEM;
int /*<<< orphan*/ path_cstr_is_valid(char const *);
int /*<<< orphan*/ path_delete(struct path *);
struct path *path_new();
int path_set_from_string(struct path *, char const *);
int path_new_from_string(struct path **path, const char *cstr) {
  if (!(path_cstr_is_valid(cstr)))
    return EINVAL;
  *path = path_new();
  if (*path == NULL)
    return ENOMEM;
  int ret = path_set_from_string(*path, cstr);
  if (ret != 0) {
    path_delete(*path);
    *path = NULL;
  }
  return ret;
}


x86 assembly (clang, -O0):
<path_new_from_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <path_cstr_is_valid@plt>
cmp    $0x0,%eax
jne    1173 <path_new_from_string+0x33>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <path_new_from_string+0x9d>
mov    $0x0,%al
call   1060 <path_new@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a3 <path_new_from_string+0x63>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <path_new_from_string+0x9d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <path_set_from_string@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11d7 <path_new_from_string+0x97>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <path_delete@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_new_from_string>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <path_cstr_is_valid@plt>
test   %eax,%eax
je     118b <path_new_from_string+0x4b>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <path_new@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1194 <path_new_from_string+0x54>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <path_set_from_string@plt>
test   %eax,%eax
je     119d <path_new_from_string+0x5d>
mov    %eax,%ebx
mov    (%r14),%rdi
call   1040 <path_delete@plt>
movq   $0x0,(%r14)
mov    %ebx,%ebp
jmp    119d <path_new_from_string+0x5d>
mov    0x2e46(%rip),%rax
jmp    119b <path_new_from_string+0x5b>
mov    0x2e45(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.path = type { i32 }
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @path_new_from_string(%struct.path** %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.path**
%5 = alloca i8*
%6 = alloca i32
store %struct.path** %0, %struct.path*** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i32 @path_cstr_is_valid(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* %3
br label %34
12: ; preds = %2
%13 = call %struct.path* (...) @path_new()
%14 = load %struct.path**, %struct.path*** %4
store %struct.path* %13, %struct.path** %14
%15 = load %struct.path**, %struct.path*** %4
%16 = load %struct.path*, %struct.path** %15
%17 = icmp eq %struct.path* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i32, i32* @ENOMEM
store i32 %19, i32* %3
br label %34
20: ; preds = %12
%21 = load %struct.path**, %struct.path*** %4
%22 = load %struct.path*, %struct.path** %21
%23 = load i8*, i8** %5
%24 = call i32 @path_set_from_string(%struct.path* %22, i8* %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load %struct.path**, %struct.path*** %4
%29 = load %struct.path*, %struct.path** %28
%30 = call i32 @path_delete(%struct.path* %29)
%31 = load %struct.path**, %struct.path*** %4
store %struct.path* null, %struct.path** %31
br label %32
32: ; preds = %27, %20
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %18, %10
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.path = type { i32 }
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @path_new_from_string(%struct.path** nocapture %0, i8* %1) {
%3 = tail call i32 @path_cstr_is_valid(i8* %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @EINVAL
br label %18
7: ; preds = %2
%8 = tail call %struct.path* (...) @path_new()
store %struct.path* %8, %struct.path** %0
%9 = icmp eq %struct.path* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load i32, i32* @ENOMEM
br label %18
12: ; preds = %7
%13 = tail call i32 @path_set_from_string(%struct.path* %8, i8* %1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load %struct.path*, %struct.path** %0
%17 = tail call i32 @path_delete(%struct.path* %16)
store %struct.path* null, %struct.path** %0
br label %18
18: ; preds = %12, %15, %10, %5
%19 = phi i32 [ %11, %10 ], [ %6, %5 ], [ %13, %15 ], [ 0, %12 ]
ret i32 %19
}