#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*/ dump_buf();
int /*<<< orphan*/ exit(int);
void *memset(void *, int, unsigned int);
int /*<<< orphan*/ printf(char *, int, ...);
void checked_memset(void *s, int c, unsigned n) {
  void *ret = memset(s, c, n);
  if (ret != s) {
    printf("Wrong memset return value: %p != %p\n", ret, s);
    exit(1);
  }
  char *s_char = (char *)s;
  char *dst_char = s_char;
  for (unsigned i = 0; i < n; ++i, ++dst_char) {
    if (*dst_char != c) {
      printf("memset failure: index %d (%p) = %u\n", i, dst_char, *dst_char);
      dump_buf();
      exit(1);
    }
  }
  if (*dst_char == c) {
    printf("memset failure: wrote %d past the end of buffer\n", c);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<checked_memset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <memset@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1195 <checked_memset+0x55>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    1214 <checked_memset+0xd4>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    -0xc(%rbp),%eax
je     11f5 <checked_memset+0xb5>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <dump_buf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11fa <checked_memset+0xba>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ac <checked_memset+0x6c>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    -0xc(%rbp),%eax
jne    123f <checked_memset+0xff>
mov    -0xc(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_memset>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <memset@plt>
cmp    %rbx,%rax
jne    11b4 <checked_memset+0x74>
test   %r14d,%r14d
je     1183 <checked_memset+0x43>
lea    -0x1(%r14),%eax
add    %rbx,%rax
add    $0x1,%rax
mov    %r14d,%edx
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
movsbl (%rbx,%rsi,1),%ecx
cmp    %ebp,%ecx
jne    118f <checked_memset+0x4f>
add    $0x1,%rsi
cmp    %esi,%edx
jne    1170 <checked_memset+0x30>
mov    %rax,%rbx
movsbl (%rbx),%eax
cmp    %ebp,%eax
je     11d1 <checked_memset+0x91>
pop    %rbx
pop    %r14
pop    %rbp
ret
add    %rsi,%rbx
lea    0xe8c(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <dump_buf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe45(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe71(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Wrong memset return value: %p != %p\0A\00"
@.str.1 = [36 x i8] c"memset failure: index %d (%p) = %u\0A\00"
@.str.2 = [49 x i8] c"memset failure: wrote %d past the end of buffer\0A\00"
define void @checked_memset(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i8* @memset(i8* %11, i32 %12, i32 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = load i8*, i8** %4
%17 = icmp ne i8* %15, %16
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = ptrtoint i8* %19 to i32
%21 = load i8*, i8** %4
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %3
%25 = load i8*, i8** %4
store i8* %25, i8** %8
%26 = load i8*, i8** %8
store i8* %26, i8** %9
store i32 0, i32* %10
br label %27
27: ; preds = %47, %24
%28 = load i32, i32* %10
%29 = load i32, i32* %6
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i32, i32* %5
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %46
37: ; preds = %31
%38 = load i32, i32* %10
%39 = load i8*, i8** %9
%40 = load i8*, i8** %9
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %38, i8* %39, i32 %42)
%44 = call i32 (...) @dump_buf()
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %31
br label %47
47: ; preds = %46
%48 = load i32, i32* %10
%49 = add i32 %48, 1
store i32 %49, i32* %10
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
br label %27
52: ; preds = %27
%53 = load i8*, i8** %9
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = load i32, i32* %5
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %52
%59 = load i32, i32* %5
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %52
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Wrong memset return value: %p != %p\0A\00"
@.str.1 = [36 x i8] c"memset failure: index %d (%p) = %u\0A\00"
@.str.2 = [49 x i8] c"memset failure: wrote %d past the end of buffer\0A\00"
define void @checked_memset(i8* %0, i32 %1, i32 %2) {
%4 = tail call i8* @memset(i8* %0, i32 %1, i32 %2)
%5 = icmp eq i8* %4, %0
br i1 %5, label %6, label %13
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %18, label %8
8: ; preds = %6
%9 = add i32 %2, -1
%10 = zext i32 %9 to i64
%11 = add nuw nsw i64 %10, 1
%12 = getelementptr i8, i8* %0, i64 %11
br label %23
13: ; preds = %3
%14 = ptrtoint i8* %4 to i64
%15 = trunc i64 %14 to i32
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %34, %6
%19 = phi i8* [ %0, %6 ], [ %12, %34 ]
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, %1
br i1 %22, label %38, label %41
23: ; preds = %8, %34
%24 = phi i32 [ %35, %34 ], [ 0, %8 ]
%25 = phi i8* [ %36, %34 ], [ %0, %8 ]
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, %1
br i1 %28, label %34, label %29
29: ; preds = %23
%30 = sext i8 %26 to i32
%31 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %24, i8* %25, i32 %30)
%32 = tail call i32 (...) @dump_buf()
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %23
%35 = add nuw i32 %24, 1
%36 = getelementptr inbounds i8, i8* %25, i64 1
%37 = icmp eq i32 %35, %2
br i1 %37, label %18, label %23
38: ; preds = %18
%39 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %18
ret void
}