#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int uint64_t;
typedef int uint32_t;

/* Variables and functions */
void *memset(void *dest, int c, size_t n) {
  unsigned long pattern = (unsigned long)c;
  switch (sizeof(unsigned long)) {
  case 4:
    pattern |= ((uint32_t)c) << 8;
    pattern |= ((uint32_t)c) << 16;
    pattern |= ((uint32_t)c) << 24;
    do {
      unsigned long *ip = (unsigned long *)dest;
      unsigned char *cp;
      for (; n >= sizeof(unsigned long); n -= sizeof(unsigned long))
        *ip++ = pattern;
      cp = (unsigned char *)ip;
      while (n--)
        *cp++ = c;
    } while (0);
    break;
  case 8:
    pattern |= ((uint64_t)c) << 8;
    pattern |= ((uint64_t)c) << 16;
    pattern |= ((uint64_t)c) << 24;
    pattern |= ((uint64_t)c) << 32;
    pattern |= ((uint64_t)c) << 40;
    pattern |= ((uint64_t)c) << 48;
    pattern |= ((uint64_t)c) << 56;
    do {
      unsigned long *ip = (unsigned long *)dest;
      unsigned char *cp;
      for (; n >= sizeof(unsigned long); n -= sizeof(unsigned long))
        *ip++ = pattern;
      cp = (unsigned char *)ip;
      while (n--)
        *cp++ = c;
    } while (0);
    break;
  default:
    do {
      uint8_t *p = (uint8_t *)dest;
      for (; n >= sizeof(uint8_t); n -= sizeof(uint8_t))
        *p++ = c;
    } while (0);
    break;
  }
  return (dest);
}


x86 assembly (clang, -O0):
<memset>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0xc(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x8,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x10,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x18,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
shl    %cl,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x28,%ecx
shl    %cl,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x30,%ecx
shl    %cl,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x38,%ecx
shl    %cl,%eax
cltq
or     -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x8,-0x18(%rbp)
jb     11d1 <memset+0xd1>
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    119f <memset+0x9f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     120d <memset+0x10d>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    11d9 <memset+0xd9>
jmp    1212 <memset+0x112>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<memset>:
mov    %rdi,%rax
mov    %rdx,%rcx
and    $0x7,%rcx
je     1164 <memset+0x64>
mov    %rdx,%rdi
and    $0xfffffffffffffff8,%rdi
xor    %r8d,%r8d
cmp    $0x8,%rdx
cmovae %rdi,%r8
mov    %sil,(%rax,%r8,1)
cmp    $0x1,%rcx
je     1164 <memset+0x64>
mov    %sil,0x1(%rax,%r8,1)
cmp    $0x2,%rcx
je     1164 <memset+0x64>
mov    %sil,0x2(%rax,%r8,1)
cmp    $0x3,%rcx
je     1164 <memset+0x64>
mov    %sil,0x3(%rax,%r8,1)
cmp    $0x4,%rcx
je     1164 <memset+0x64>
mov    %sil,0x4(%rax,%r8,1)
cmp    $0x5,%rcx
je     1164 <memset+0x64>
mov    %sil,0x5(%rax,%r8,1)
cmp    $0x6,%rcx
je     1164 <memset+0x64>
mov    %sil,0x6(%rax,%r8,1)
ret

LLVM IR (-O0):
define i8* @memset(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
store i64 %11, i64* %7
%12 = load i32, i32* %5
%13 = shl i32 %12, 8
%14 = sext i32 %13 to i64
%15 = load i64, i64* %7
%16 = or i64 %15, %14
store i64 %16, i64* %7
%17 = load i32, i32* %5
%18 = shl i32 %17, 16
%19 = sext i32 %18 to i64
%20 = load i64, i64* %7
%21 = or i64 %20, %19
store i64 %21, i64* %7
%22 = load i32, i32* %5
%23 = shl i32 %22, 24
%24 = sext i32 %23 to i64
%25 = load i64, i64* %7
%26 = or i64 %25, %24
store i64 %26, i64* %7
%27 = load i32, i32* %5
%28 = shl i32 %27, 32
%29 = sext i32 %28 to i64
%30 = load i64, i64* %7
%31 = or i64 %30, %29
store i64 %31, i64* %7
%32 = load i32, i32* %5
%33 = shl i32 %32, 40
%34 = sext i32 %33 to i64
%35 = load i64, i64* %7
%36 = or i64 %35, %34
store i64 %36, i64* %7
%37 = load i32, i32* %5
%38 = shl i32 %37, 48
%39 = sext i32 %38 to i64
%40 = load i64, i64* %7
%41 = or i64 %40, %39
store i64 %41, i64* %7
%42 = load i32, i32* %5
%43 = shl i32 %42, 56
%44 = sext i32 %43 to i64
%45 = load i64, i64* %7
%46 = or i64 %45, %44
store i64 %46, i64* %7
br label %47
47: ; preds = %3
%48 = load i8*, i8** %4
%49 = bitcast i8* %48 to i64*
store i64* %49, i64** %8
br label %50
50: ; preds = %57, %47
%51 = load i64, i64* %6
%52 = icmp uge i64 %51, 8
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = load i64, i64* %7
%55 = load i64*, i64** %8
%56 = getelementptr inbounds i64, i64* %55, i32 1
store i64* %56, i64** %8
store i64 %54, i64* %55
br label %57
57: ; preds = %53
%58 = load i64, i64* %6
%59 = sub i64 %58, 8
store i64 %59, i64* %6
br label %50
60: ; preds = %50
%61 = load i64*, i64** %8
%62 = bitcast i64* %61 to i8*
store i8* %62, i8** %9
br label %63
63: ; preds = %67, %60
%64 = load i64, i64* %6
%65 = add i64 %64, -1
store i64 %65, i64* %6
%66 = icmp ne i64 %64, 0
br i1 %66, label %67, label %72
67: ; preds = %63
%68 = load i32, i32* %5
%69 = trunc i32 %68 to i8
%70 = load i8*, i8** %9
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %9
store i8 %69, i8* %70
br label %63
72: ; preds = %63
br label %73
73: ; preds = %72
%74 = load i8*, i8** %4
ret i8* %74
}

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