#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*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
size_t strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  size_t n, str_len = 0;
  const char *str = "foobar";
  char *str_cpy = malloc(1024);
  srand(time(NULL));
  n = 0;
  while (n++ < 1024) {
    str_cpy[n] = rand() % 255;
  }
  printf("%s\n", str);
  memcpy(str_cpy, str, 6);
  str_len = strlen(str_cpy);
  printf("%zu\n", str_len);
  printf("%s\n", str_cpy);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe72(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x400,%rax
jae    11f2 <main+0x82>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11b8 <main+0x48>
mov    -0x20(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0xffffffff80808081,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
shr    $0x7,%ecx
add    %edx,%ecx
add    %eax,%ecx
mov    %cl,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x401,%rbx
jne    11b0 <main+0x30>
lea    0xe16(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xdf6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"foobar\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [5 x i8] c"%zu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i64 0, i64* %3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = call i8* @malloc(i32 1024)
store i8* %6, i8** %5
%7 = call i32 @time(i32* null)
%8 = call i32 @srand(i32 %7)
store i64 0, i64* %2
br label %9
9: ; preds = %13, %0
%10 = load i64, i64* %2
%11 = add i64 %10, 1
store i64 %11, i64* %2
%12 = icmp ult i64 %10, 1024
br i1 %12, label %13, label %20
13: ; preds = %9
%14 = call i32 (...) @rand()
%15 = srem i32 %14, 255
%16 = trunc i32 %15 to i8
%17 = load i8*, i8** %5
%18 = load i64, i64* %2
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 %16, i8* %19
br label %9
20: ; preds = %9
%21 = load i8*, i8** %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %5
%24 = load i8*, i8** %4
%25 = call i32 @memcpy(i8* %23, i8* %24, i32 6)
%26 = load i8*, i8** %5
%27 = call i64 @strlen(i8* %26)
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = load i8*, i8** %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"foobar\00"
@.str.2 = [5 x i8] c"%zu\0A\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 1, %0 ], [ %10, %4 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 255
%8 = trunc i32 %7 to i8
%9 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 %8, i8* %9
%10 = add nuw nsw i64 %5, 1
%11 = icmp eq i64 %10, 1025
br i1 %11, label %12, label %4
12: ; preds = %4
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @memcpy(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%17 = tail call i32 @puts(i8* dereferenceable(1) %1)
ret i32 0
}