#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  char *ptr = NULL;
  ptr = malloc(66);
  strncpy(ptr, "Hello world", 66);
  printf("%s\n", ptr);
  if (ptr) {
    printf("frees  the  memory  space pointed to by ptr\n");
    free(ptr);
  }
  strncpy(ptr, ";-)", 66);
  printf("%s\n", ptr);
  ptr = 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)
movq   $0x0,-0x18(%rbp)
mov    $0x42,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe89(%rip),%rsi
mov    $0x42,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     11b5 <main+0x75>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x42,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x42,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xeab(%rip),%rsi
mov    %rax,%rdi
mov    $0x42,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
test   %rbx,%rbx
je     1183 <main+0x43>
lea    0xe9a(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe82(%rip),%rsi
mov    %rbx,%rdi
mov    $0x42,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Hello world\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [45 x i8] c"frees the memory space pointed to by ptr\0A\00"
@.str.3 = [4 x i8] c";-)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
%7 = call i8* @malloc(i32 66)
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 66)
%10 = load i8*, i8** %6
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%16 = load i8*, i8** %6
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %6
%20 = call i32 @strncpy(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 66)
%21 = load i8*, i8** %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i8* null, i8** %6
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"Hello world\00"
@.str.3 = [4 x i8] c";-)\00"
@str = [44 x i8] c"frees the memory space pointed to by ptr\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(66) i8* @malloc(i32 66)
%4 = tail call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 66)
%5 = tail call i32 @puts(i8* dereferenceable(1) %3)
%6 = icmp eq i8* %3, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @free(i8* %3)
br label %10
10: ; preds = %7, %2
%11 = tail call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 66)
%12 = tail call i32 @puts(i8* dereferenceable(1) %3)
ret i32 0
}