#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*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int const);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char const *, int const);
int main(int argc, char **argv) {
  const static int strSize = 1024;
  const char *source = "This is a string";
  char *string = NULL;
  printf("Allocating space for string.\n");
  string = malloc(strSize * sizeof(char));
  if (string == NULL) {
    fputs("cannot allocate memory\n", stderr);
    return 1;
  }
  strncpy(string, source, strSize);
  printf("The string is: %s\n", string);
  free(string);
  printf("string space freed.\n");
  string = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c1 <main+0x71>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    120b <main+0xbb>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xed2(%rip),%rdi
call   1050 <puts@plt>
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11be <main+0x5e>
mov    %rax,%rbx
lea    0xe78(%rip),%rsi
mov    %rax,%rdi
mov    $0x400,%edx
call   1040 <strncpy@plt>
lea    0xe8d(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xea2(%rip),%rdi
call   1050 <puts@plt>
jmp    11d8 <main+0x78>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@main.strSize = internal constant i32 1024
@.str = [17 x i8] c"This is a string\00"
@.str.1 = [30 x i8] c"Allocating space for string.\0A\00"
@.str.2 = [24 x i8] c"cannot allocate memory\0A\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"The string is: %s\0A\00"
@.str.4 = [21 x i8] c"string space freed.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* null, i8** %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%9 = call i8* @malloc(i32 1024)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fputs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i32 1, i32* %3
br label %24
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = load i8*, i8** %6
%18 = call i32 @strncpy(i8* %16, i8* %17, i32 1024)
%19 = load i8*, i8** %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %7
%22 = call i32 @free(i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %7
store i32 0, i32* %3
br label %24
24: ; preds = %15, %12
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@.str = [17 x i8] c"This is a string\00"
@.str.2 = [24 x i8] c"cannot allocate memory\0A\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"The string is: %s\0A\00"
@str = [29 x i8] c"Allocating space for string.\00"
@str.5 = [20 x i8] c"string space freed.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%4 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fputs(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %14
9: ; preds = %2
%10 = tail call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 1024)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%12 = tail call i32 @free(i8* %4)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %14
14: ; preds = %9, %6
%15 = phi i32 [ 1, %6 ], [ 0, %9 ]
ret i32 %15
}