#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 PATH_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int check_name(char *name, char **target) {
  char *curdir;
  if (*name != '/') {
    curdir = (char *)malloc(PATH_MAX + 1 + strlen(name) + 1);
    if (curdir == NULL) {
      fprintf(stderr, "Error while allocating memory\n");
      return 1;
    }
    if (getcwd(curdir, PATH_MAX) == NULL) {
      exit(1);
    }
    sprintf(curdir + strlen(curdir), "/%s", name);
    *target = curdir;
  } else {
    *target = name;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     1235 <check_name+0xd5>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d5 <check_name+0x75>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1247 <check_name+0xe7>
mov    -0x20(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    11fb <check_name+0x9b>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1240 <check_name+0xe0>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
je     11cd <check_name+0x6d>
mov    0x2e76(%rip),%r12
mov    (%r12),%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11de <check_name+0x7e>
mov    %rax,%r15
mov    (%r12),%esi
mov    %rax,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
je     11fa <check_name+0x9a>
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
mov    %r15,%rdi
add    %rax,%rdi
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r15,%rbx
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    11d2 <check_name+0x72>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error while allocating memory\0A\00"
@.str.1 = [4 x i8] c"/%s\00"
define i32 @check_name(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 47
br i1 %10, label %11, label %42
11: ; preds = %2
%12 = load i32, i32* @PATH_MAX
%13 = add nsw i32 %12, 1
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = add nsw i32 %16, 1
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %46
25: ; preds = %11
%26 = load i8*, i8** %6
%27 = load i32, i32* @PATH_MAX
%28 = call i32* @getcwd(i8* %26, i32 %27)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i8*, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8*, i8** %4
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %6
%41 = load i8**, i8*** %5
store i8* %40, i8** %41
br label %45
42: ; preds = %2
%43 = load i8*, i8** %4
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
br label %45
45: ; preds = %42, %32
store i32 0, i32* %3
br label %46
46: ; preds = %45, %22
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error while allocating memory\0A\00"
@.str.1 = [4 x i8] c"/%s\00"
define i32 @check_name(i8* %0, i8** nocapture writeonly %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 47
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = load i32, i32* @PATH_MAX
%7 = tail call i32 @strlen(i8* %0)
%8 = add i32 %6, 2
%9 = add i32 %8, %7
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %29
16: ; preds = %5
%17 = load i32, i32* @PATH_MAX
%18 = tail call i32* @getcwd(i8* %11, i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = tail call i32 @strlen(i8* %11)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %11, i64 %24
%26 = tail call i32 @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %2, %22
%28 = phi i8* [ %11, %22 ], [ %0, %2 ]
store i8* %28, i8** %1
br label %29
29: ; preds = %27, %13
%30 = phi i32 [ 1, %13 ], [ 0, %27 ]
ret i32 %30
}