#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long long, char const *);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
char *strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *copy_string(const char *s) {
  char *ss;
  if (s == NULL)
    return NULL;
  ss = malloc(strlen(s) + 1);
  if (ss == NULL) {
    fprintf(stderr, "failed to allocate %lld bytes to copy \"%s\"\n",
            (long long)(strlen(s) + 1), s);
    exit(EXIT_FAILURE);
  }
  return strcpy(ss, s);
}


x86 assembly (clang, -O0):
<copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <copy_string+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11e6 <copy_string+0x96>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <copy_string+0x85>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x10(%rbp),%rcx
lea    0xe3e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%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):
<copy_string>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1186 <copy_string+0x36>
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,%r15
add    $0x1,%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1191 <copy_string+0x41>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1188 <copy_string+0x38>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <fprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"failed to allocate %lld bytes to copy \22%s\22\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @copy_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %8
7: ; preds = %1
store i8* null, i8** %2
br label %28
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %24
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %18, 1
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i64 %19, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %8
%25 = load i8*, i8** %4
%26 = load i8*, i8** %3
%27 = call i8* @strcpy(i8* %25, i8* %26)
store i8* %27, i8** %2
br label %28
28: ; preds = %24, %7
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"failed to allocate %lld bytes to copy \22%s\22\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @copy_string(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = add nsw i64 %4, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i64 %5, i8* %0)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %3
%14 = tail call i8* @strcpy(i8* %6, i8* %0)
br label %15
15: ; preds = %1, %13
%16 = phi i8* [ %6, %13 ], [ null, %1 ]
ret i8* %16
}