#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*/ exit(int);
void *memset(void *, int, unsigned int);
int /*<<< orphan*/ printf(char *, char *, char);
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);
  }
  for (unsigned i = 0; i < n; ++i) {
    char *dst_char = (char *)s + i;
    if (*dst_char != c) {
      printf("memmove failure: %p = %u\n", dst_char, *dst_char);
      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     1184 <checked_memset+0x54>
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rax
lea    0xe8e(%rip),%rdi
movsbl %al,%edx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    11e9 <checked_memset+0xb9>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    -0xc(%rbp),%eax
je     11d6 <checked_memset+0xa6>
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rax
lea    0xe61(%rip),%rdi
movsbl (%rax),%edx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11db <checked_memset+0xab>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <checked_memset+0x5b>
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    1182 <checked_memset+0x52>
test   %r14d,%r14d
je     1161 <checked_memset+0x31>
mov    %r14d,%eax
xchg   %ax,%ax
movsbl (%rbx),%ecx
cmp    %ebp,%ecx
jne    1166 <checked_memset+0x36>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rax
jne    1150 <checked_memset+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret
movsbl %cl,%edx
lea    0xeb5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movsbl %bl,%edx
lea    0xe74(%rip),%rdi
mov    %rax,%rsi
jmp    1173 <checked_memset+0x43>

LLVM IR (-O0):
@.str = [37 x i8] c"Wrong memset return value: %p != %p\0A\00"
@.str.1 = [26 x i8] c"memmove failure: %p = %u\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 i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = call i8* @memset(i8* %10, i32 %11, i32 %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = load i8*, i8** %4
%16 = icmp ne i8* %14, %15
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = load i8*, i8** %4
%20 = ptrtoint i8* %19 to i8
%21 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %18, i8 signext %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %3
store i32 0, i32* %8
br label %24
24: ; preds = %45, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %48
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = load i32, i32* %8
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i32, i32* %5
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %44
38: ; preds = %28
%39 = load i8*, i8** %9
%40 = load i8*, i8** %9
%41 = load i8, i8* %40
%42 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8 signext %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %28
br label %45
45: ; preds = %44
%46 = load i32, i32* %8
%47 = add i32 %46, 1
store i32 %47, i32* %8
br label %24
48: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Wrong memset return value: %p != %p\0A\00"
@.str.1 = [26 x i8] c"memmove failure: %p = %u\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 %10
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %15, label %8
8: ; preds = %6
%9 = zext i32 %2 to i64
br label %16
10: ; preds = %3
%11 = ptrtoint i8* %0 to i64
%12 = trunc i64 %11 to i8
%13 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %4, i8 signext %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %27, %6
ret void
16: ; preds = %8, %27
%17 = phi i64 [ 0, %8 ], [ %28, %27 ]
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, %1
br i1 %21, label %27, label %22
22: ; preds = %16
%23 = and i64 %17, 4294967295
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8 signext %19)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %16
%28 = add nuw nsw i64 %17, 1
%29 = icmp eq i64 %28, %9
br i1 %29, label %15, label %16
}