#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 */
scalar_t__ malloc(int);
scalar_t__ strchr(char *, char);
char *strcpy(char *, char *);
int strlen(char *);
char *dupstr(char *str) {
  if (strchr(str, ' ')) {
    int len = strlen(str) + 3;
    char *res = (char *)malloc(len);
    strcpy(res + 1, str);
    res[0] = res[len - 2] = '"';
    res[len - 1] = 0;
    return res;
  }
  return strcpy((char *)malloc(strlen(str) + 1), str);
}


x86 assembly (clang, -O0):
<dupstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11c6 <dupstr+0x86>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x22,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
movb   $0x22,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <dupstr+0xa9>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupstr>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
test   %rbx,%rbx
je     118f <dupstr+0x4f>
lea    0x3(%r15),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
movslq %r15d,%rax
movb   $0x22,0x1(%rax,%rbx,1)
movb   $0x22,(%rbx)
movb   $0x0,0x2(%rax,%rbx,1)
jmp    11a9 <dupstr+0x69>
add    $0x1,%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @dupstr(i8* %0) {
%2 = tail call i64 @strchr(i8* %0, i8 signext 32)
%3 = icmp eq i64 %2, 0
%4 = tail call i32 @strlen(i8* %0)
br i1 %3, label %17, label %5
5: ; preds = %1
%6 = add nsw i32 %4, 3
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = tail call i8* @strcpy(i8* %9, i8* dereferenceable(1) %0)
%11 = add nsw i32 %4, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8 34, i8* %13
store i8 34, i8* %8
%14 = add nsw i32 %4, 2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %8, i64 %15
store i8 0, i8* %16
br label %22
17: ; preds = %1
%18 = add nsw i32 %4, 1
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i8* @strcpy(i8* dereferenceable(1) %20, i8* dereferenceable(1) %0)
br label %22
22: ; preds = %17, %5
%23 = phi i8* [ %8, %5 ], [ %20, %17 ]
ret i8* %23
}