#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*/ printf(char *, ...);
void memset_test(void *addr, int value, int size) {
  int i, errors = 0;
  char *b;
  b = (char *)addr;
  printf("\tTesting: ");
  for (i = 0; i < size; i++) {
    if (b[i] != value) {
      printf("Not match at offset %d (%x!=%x)!\n", i, b[i], value);
      errors++;
      if (errors > 20)
        break;
    }
  }
  if (b[size + 1] == value) {
    errors++;
    printf("Value after the end has wrong value!\n");
  }
  if (errors)
    printf("Failed!\n");
  else
    printf("Passed!\n");
}


x86 assembly (clang, -O0):
<memset_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b7 <memset_test+0xa7>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    -0xc(%rbp),%eax
je     11a4 <memset_test+0x94>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
mov    -0xc(%rbp),%ecx
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jle    119f <memset_test+0x8f>
jmp    11b7 <memset_test+0xa7>
jmp    11a4 <memset_test+0x94>
jmp    11a9 <memset_test+0x99>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <memset_test+0x36>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    -0xc(%rbp),%eax
jne    11e8 <memset_test+0xd8>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1205 <memset_test+0xf5>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1213 <memset_test+0x103>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memset_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r13d
mov    %rdi,%r12
lea    0xec6(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,0x4(%rsp)
test   %ebp,%ebp
jle    118c <memset_test+0x6c>
mov    0x4(%rsp),%r14d
lea    0xeb4(%rip),%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    1169 <memset_test+0x49>
nopl   (%rax)
add    $0x1,%rbp
cmp    %rbp,%r14
je     118c <memset_test+0x6c>
movsbl (%r12,%rbp,1),%edx
cmp    %r13d,%edx
je     1160 <memset_test+0x40>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x1(%rbx),%eax
cmp    $0x13,%ebx
mov    %eax,%ebx
jle    1160 <memset_test+0x40>
movslq 0x4(%rsp),%rax
movsbl 0x1(%rax,%r12,1),%eax
cmp    %r13d,%eax
jne    11ab <memset_test+0x8b>
add    $0x1,%ebx
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
lea    0xe79(%rip),%rax
lea    0xe7a(%rip),%rdi
cmove  %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"\09Testing: \00"
@.str.1 = [34 x i8] c"Not match at offset %d (%x!=%x)!\0A\00"
@.str.2 = [38 x i8] c"Value after the end has wrong value!\0A\00"
@.str.3 = [9 x i8] c"Failed!\0A\00"
@.str.4 = [9 x i8] c"Passed!\0A\00"
define void @memset_test(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
%10 = load i8*, i8** %4
store i8* %10, i8** %9
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %12
12: ; preds = %42, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %45
16: ; preds = %12
%17 = load i8*, i8** %9
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = load i32, i32* %5
%24 = icmp ne i32 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %16
%26 = load i32, i32* %7
%27 = load i8*, i8** %9
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = load i32, i32* %5
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %32, i32 %33)
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp sgt i32 %37, 20
br i1 %38, label %39, label %40
39: ; preds = %25
br label %45
40: ; preds = %25
br label %41
41: ; preds = %40, %16
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %12
45: ; preds = %39, %12
%46 = load i8*, i8** %9
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = load i32, i32* %5
%54 = icmp eq i32 %52, %53
br i1 %54, label %55, label %59
55: ; preds = %45
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %55, %45
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %66
64: ; preds = %59
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %62
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"\09Testing: \00"
@.str.1 = [34 x i8] c"Not match at offset %d (%x!=%x)!\0A\00"
@str = [8 x i8] c"Passed!\00"
@str.5 = [8 x i8] c"Failed!\00"
@str.6 = [37 x i8] c"Value after the end has wrong value!\00"
define void @memset_test(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %24
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %20
%9 = phi i64 [ 0, %6 ], [ %22, %20 ]
%10 = phi i32 [ 0, %6 ], [ %21, %20 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %9
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, %1
br i1 %14, label %20, label %15
15: ; preds = %8
%16 = trunc i64 %9 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %13, i32 %1)
%18 = add nsw i32 %10, 1
%19 = icmp sgt i32 %10, 19
br i1 %19, label %24, label %20
20: ; preds = %8, %15
%21 = phi i32 [ %18, %15 ], [ %10, %8 ]
%22 = add nuw nsw i64 %9, 1
%23 = icmp eq i64 %22, %7
br i1 %23, label %24, label %8
24: ; preds = %20, %15, %3
%25 = phi i32 [ 0, %3 ], [ %18, %15 ], [ %21, %20 ]
%26 = add nsw i32 %2, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, %1
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = add nsw i32 %25, 1
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
br label %35
35: ; preds = %32, %24
%36 = phi i32 [ %33, %32 ], [ %25, %24 ]
%37 = icmp eq i32 %36, 0
%38 = select i1 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str.5, i64 0, i64 0)
%39 = tail call i32 @puts(i8* dereferenceable(1) %38)
ret void
}