#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*/ writeStringToBuffer(char *, char *, int);
char *test_writeToBuffer() {
  char *string = "12345";
  char buffer[40];
  writeStringToBuffer(string, buffer, 4);
  char correct = 1;
  for (int i = 0; i < 5; i++) {
    if (string[i] != buffer[i + 4])
      correct = 0;
  }
  do {
    if (!(correct == 1))
      return "Write to buffer (mid way through)";
  } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<test_writeToBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xee1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1030 <writeStringToBuffer@plt>
movb   $0x1,-0x41(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x5,-0x48(%rbp)
jge    1183 <test_writeToBuffer+0x73>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x48(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
movsbl -0x40(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     1170 <test_writeToBuffer+0x60>
movb   $0x0,-0x41(%rbp)
jmp    1175 <test_writeToBuffer+0x65>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1140 <test_writeToBuffer+0x30>
jmp    1188 <test_writeToBuffer+0x78>
movsbl -0x41(%rbp),%eax
cmp    $0x1,%eax
je     11a5 <test_writeToBuffer+0x95>
lea    0xe6a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <test_writeToBuffer+0xa2>
jmp    11aa <test_writeToBuffer+0x9a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_writeToBuffer>:
sub    $0x28,%rsp
lea    0xee5(%rip),%rdi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1030 <writeStringToBuffer@plt>
xor    %eax,%eax
cmpb   $0x31,0x4(%rsp)
movd   0x5(%rsp),%xmm0
lea    0xeca(%rip),%rcx
cmovne %rcx,%rax
movd   %xmm0,%edx
cmp    $0x35343332,%edx
cmovne %rcx,%rax
add    $0x28,%rsp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"12345\00"
@.str.1 = [34 x i8] c"Write to buffer (mid way through)\00"
define i8* @test_writeToBuffer() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca [40 x i8]
%4 = alloca i8
%5 = alloca i32
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%8 = call i32 @writeStringToBuffer(i8* %6, i8* %7, i32 4)
store i8 1, i8* %4
store i32 0, i32* %5
br label %9
9: ; preds = %28, %0
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 5
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = load i8*, i8** %2
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %18, %24
br i1 %25, label %26, label %27
26: ; preds = %12
store i8 0, i8* %4
br label %27
27: ; preds = %26, %12
br label %28
28: ; preds = %27
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %9
31: ; preds = %9
br label %32
32: ; preds = %31
%33 = load i8, i8* %4
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 1
br i1 %35, label %37, label %36
36: ; preds = %32
store i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %39
37: ; preds = %32
br label %38
38: ; preds = %37
store i8* null, i8** %1
br label %39
39: ; preds = %38, %36
%40 = load i8*, i8** %1
ret i8* %40
}

LLVM IR (-O2):
@.str = [6 x i8] c"12345\00"
@.str.1 = [34 x i8] c"Write to buffer (mid way through)\00"
define i8* @test_writeToBuffer() {
%1 = alloca [40 x i8]
%2 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%3 = call i32 @writeStringToBuffer(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2, i32 4)
%4 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 4
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 49
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 5
%8 = bitcast i8* %7 to <4 x i8>*
%9 = load <4 x i8>, <4 x i8>* %8
%10 = freeze <4 x i8> %9
%11 = bitcast <4 x i8> %10 to i32
%12 = icmp eq i32 %11, 892613426
%13 = select i1 %12, i1 %6, i1 false
%14 = select i1 %13, i8* null, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0)
ret i8* %14
}