#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*/ printf(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int set_str(char **dst, char *src) {
  size_t len;
  if (src == NULL) {
    printf("null source string\n");
    return 0;
  }
  len = strlen(src) + 1;
  *dst = malloc(sizeof(char) * len);
  if (*dst == NULL) {
    printf("Allocation of memory to string failed.\n");
    return 0;
  }
  strcpy(*dst, src);
  return 1;
}


x86 assembly (clang, -O0):
<set_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1173 <set_str+0x33>
lea    0xe9e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <set_str+0x9b>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c4 <set_str+0x84>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <set_str+0x9b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_str>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1179 <set_str+0x39>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1182 <set_str+0x42>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    $0x1,%eax
jmp    1190 <set_str+0x50>
lea    0xea7(%rip),%rdi
jmp    1189 <set_str+0x49>
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"null source string\0A\00"
@.str.1 = [40 x i8] c"Allocation of memory to string failed.\0A\00"
define i32 @set_str(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
store i8** %0, i8*** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %31
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i8* @malloc(i32 %18)
%20 = load i8**, i8*** %4
store i8* %19, i8** %20
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %11
%25 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %31
26: ; preds = %11
%27 = load i8**, i8*** %4
%28 = load i8*, i8** %27
%29 = load i8*, i8** %5
%30 = call i32 @strcpy(i8* %28, i8* %29)
store i32 1, i32* %3
br label %31
31: ; preds = %26, %24, %9
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@str = [39 x i8] c"Allocation of memory to string failed.\00"
@str.2 = [19 x i8] c"null source string\00"
define i32 @set_str(i8** nocapture writeonly %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0))
br label %15
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %1)
%8 = add nsw i32 %7, 1
%9 = tail call i8* @malloc(i32 %8)
store i8* %9, i8** %0
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %15
13: ; preds = %6
%14 = tail call i32 @strcpy(i8* %9, i8* %1)
br label %15
15: ; preds = %13, %11, %4
%16 = phi i32 [ 0, %4 ], [ 0, %11 ], [ 1, %13 ]
ret i32 %16
}