#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*/ memset(void *, int, size_t);
void *safe_memset(void *s, int c, size_t n) {
  if (n > 0) {
    volatile unsigned volatile_zero = 0;
    volatile unsigned char *vs = (volatile unsigned char *)s;
    do {
      memset(s, c, n);
    } while (vs[volatile_zero] != (unsigned char)c);
  }
  return s;
}


x86 assembly (clang, -O0):
<safe_memset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    116f <safe_memset+0x5f>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <memset@plt>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,1),%al
movzbl %al,%eax
mov    -0xc(%rbp),%ecx
movzbl %cl,%ecx
cmp    %ecx,%eax
jne    113d <safe_memset+0x2d>
jmp    116f <safe_memset+0x5f>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_memset>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
test   %rdx,%rdx
je     114a <safe_memset+0x3a>
mov    %rdx,%r14
mov    %esi,%ebp
movl   $0x0,0xc(%rsp)
nopl   (%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    0xc(%rsp),%eax
movzbl (%rbx,%rax,1),%eax
cmp    %bpl,%al
jne    1130 <safe_memset+0x20>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @safe_memset(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 0
br i1 %10, label %11, label %30
11: ; preds = %3
store volatile i32 0, i32* %7
%12 = load i8*, i8** %4
store i8* %12, i8** %8
br label %13
13: ; preds = %18, %11
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = load i64, i64* %6
%17 = call i32 @memset(i8* %14, i32 %15, i64 %16)
br label %18
18: ; preds = %13
%19 = load i8*, i8** %8
%20 = load volatile i32, i32* %7
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load volatile i8, i8* %22
%24 = zext i8 %23 to i32
%25 = load i32, i32* %5
%26 = trunc i32 %25 to i8
%27 = zext i8 %26 to i32
%28 = icmp ne i32 %24, %27
br i1 %28, label %13, label %29
29: ; preds = %18
br label %30
30: ; preds = %29, %3
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
define i8* @safe_memset(i8* returned %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = icmp eq i64 %2, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = bitcast i32* %4 to i8*
store volatile i32 0, i32* %4
%8 = trunc i32 %1 to i8
br label %9
9: ; preds = %9, %6
%10 = tail call i32 @memset(i8* %0, i32 %1, i64 %2)
%11 = load volatile i32, i32* %4
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load volatile i8, i8* %13
%15 = icmp eq i8 %14, %8
br i1 %15, label %16, label %9
16: ; preds = %9
br label %17
17: ; preds = %16, %3
ret i8* %0
}