#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *strdup(char *);
int main(void) {
  char *str = strdup("Hello world");
  *str = 'X';
  printf("%s\n", str);
  free(str);
  char array[] = "Hello world";
  array[0] = 'X';
  printf("%s\n", array);
  if (0) {
    char *literal = "Hello world";
    *literal = 'X';
    printf("%s\n", literal);
  }
  if (0) {
    char *mallocStr = (char *)malloc(256);
    mallocStr = "Hello world";
    free(mallocStr);
  }
  char *literal1 = "This is a literal.";
  char *literal2 = "This is a literal.";
  if (literal1 == literal2)
    printf("literal1 and literal2 point to the same address\n");
  else
    printf("literal1 and literal2 point to different addresses\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x58,(%rax)
mov    -0x10(%rbp),%rsi
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    0xe88(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe86(%rip),%eax
mov    %eax,-0x14(%rbp)
movb   $0x58,-0x1c(%rbp)
lea    -0x1c(%rbp),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe6e(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    11d2 <main+0xa2>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e0 <main+0xb0>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,%rbx
movb   $0x58,(%rax)
mov    %rax,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movabs $0x6f77206f6c6c6548,%rax
20 77 6f
mov    %rax,(%rsp)
movl   $0x646c72,0x8(%rsp)
movb   $0x58,(%rsp)
mov    %rsp,%rdi
call   1040 <puts@plt>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Hello world\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@__const.main.array = [12 x i8] c"Hello world\00"
@.str.2 = [19 x i8] c"This is a literal.\00"
@.str.3 = [49 x i8] c"literal1 and literal2 point to the same address\0A\00"
@.str.4 = [52 x i8] c"literal1 and literal2 point to different addresses\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [12 x i8]
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %2
%7 = load i8*, i8** %2
store i8 88, i8* %7
%8 = load i8*, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %2
%11 = call i32 @free(i8* %10)
%12 = bitcast [12 x i8]* %3 to i8*
%13 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
store i8 88, i8* %13
%14 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %5
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %23
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
ret i32 0
}

LLVM IR (-O2):
@__const.main.array = [12 x i8] c"Hello world\00"
@str = [48 x i8] c"literal1 and literal2 point to the same address\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = tail call dereferenceable_or_null(12) i8* @strdup(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @__const.main.array, i64 0, i64 0))
store i8 88, i8* %2
%3 = tail call i32 @puts(i8* %2)
%4 = tail call i32 @free(i8* %2)
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
store i8 88, i8* %5
%6 = call i32 @puts(i8* %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
ret i32 0
}