#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 */
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *copypath(char *p) {
  int len = strlen(p);
  char *retbuf;
  if (len < 1)
    return NULL;
  if (p[len - 1] == '\n') {
    p[len - 1] = '\0';
    len--;
  }
  retbuf = malloc(len + 1);
  if (!retbuf)
    return NULL;
  strcpy(retbuf, p);
  return retbuf;
}


x86 assembly (clang, -O0):
<copypath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    115f <copypath+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11cf <copypath+0x9f>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1193 <copypath+0x63>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <copypath+0x8a>
movq   $0x0,-0x8(%rbp)
jmp    11cf <copypath+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copypath>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
jle    1170 <copypath+0x40>
mov    %eax,%edi
add    $0xffffffff,%eax
cmpb   $0xa,(%r14,%rax,1)
jne    1153 <copypath+0x23>
movb   $0x0,(%r14,%rax,1)
mov    %eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1170 <copypath+0x40>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1172 <copypath+0x42>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @copypath(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %40
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %28
20: ; preds = %11
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
br label %28
28: ; preds = %20, %11
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %28
store i8* null, i8** %2
br label %40
35: ; preds = %28
%36 = load i8*, i8** %5
%37 = load i8*, i8** %3
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i8*, i8** %5
store i8* %39, i8** %2
br label %40
40: ; preds = %35, %34, %10
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
define i8* @copypath(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = add nsw i32 %2, -1
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 10
br i1 %9, label %10, label %11
10: ; preds = %4
store i8 0, i8* %7
br label %11
11: ; preds = %10, %4
%12 = phi i32 [ %5, %10 ], [ %2, %4 ]
%13 = add nuw nsw i32 %12, 1
%14 = tail call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @strcpy(i8* %14, i8* %0)
br label %18
18: ; preds = %11, %1, %16
%19 = phi i8* [ %14, %16 ], [ null, %1 ], [ null, %11 ]
ret i8* %19
}