#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 */
void *memccpy(void *restrict s1, const void *restrict s2, int c, size_t n) {
  unsigned char *restrict dstptr = (unsigned char *restrict)s1;
  const unsigned char *restrict srcptr = (const unsigned char *restrict)s2;
  for (size_t i = 0; i < n; i++) {
    *dstptr++ = *srcptr++;
    if (*srcptr - 1 == (unsigned char)c) {
      return (void *)dstptr;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<memccpy>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1196 <memccpy+0x96>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
sub    $0x1,%eax
mov    -0x1c(%rbp),%ecx
movzbl %cl,%ecx
cmp    %ecx,%eax
jne    1180 <memccpy+0x80>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119e <memccpy+0x9e>
jmp    1185 <memccpy+0x85>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    112b <memccpy+0x2b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<memccpy>:
test   %rcx,%rcx
je     113d <memccpy+0x3d>
movzbl %dl,%r8d
mov    (%rsi),%r9b
xor    %eax,%eax
xor    %r10d,%r10d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r9b,(%rdi,%r10,1)
movzbl 0x1(%rsi,%r10,1),%r9d
lea    -0x1(%r9),%edx
cmp    %r8d,%edx
je     1140 <memccpy+0x40>
add    $0x1,%r10
cmp    %r10,%rcx
jne    1120 <memccpy+0x20>
ret
xor    %eax,%eax
ret
lea    (%rdi,%r10,1),%rax
add    $0x1,%rax
ret

LLVM IR (-O0):
define i8* @memccpy(i8* noalias %0, i8* noalias %1, i32 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%13 = load i8*, i8** %6
store i8* %13, i8** %10
%14 = load i8*, i8** %7
store i8* %14, i8** %11
store i64 0, i64* %12
br label %15
15: ; preds = %36, %4
%16 = load i64, i64* %12
%17 = load i64, i64* %9
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load i8*, i8** %11
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %11
%22 = load i8, i8* %20
%23 = load i8*, i8** %10
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %10
store i8 %22, i8* %23
%25 = load i8*, i8** %11
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = sub nsw i32 %27, 1
%29 = load i32, i32* %8
%30 = trunc i32 %29 to i8
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %28, %31
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = load i8*, i8** %10
store i8* %34, i8** %5
br label %40
35: ; preds = %19
br label %36
36: ; preds = %35
%37 = load i64, i64* %12
%38 = add i64 %37, 1
store i64 %38, i64* %12
br label %15
39: ; preds = %15
store i8* null, i8** %5
br label %40
40: ; preds = %39, %33
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
define i8* @memccpy(i8* noalias writeonly %0, i8* noalias nocapture %1, i32 %2, i64 %3) {
%5 = icmp eq i64 %3, 0
br i1 %5, label %23, label %6
6: ; preds = %4
%7 = and i32 %2, 255
%8 = load i8, i8* %1
br label %11
9: ; preds = %11
%10 = icmp eq i64 %22, %3
br i1 %10, label %23, label %11
11: ; preds = %6, %9
%12 = phi i8 [ %8, %6 ], [ %18, %9 ]
%13 = phi i64 [ 0, %6 ], [ %22, %9 ]
%14 = phi i8* [ %1, %6 ], [ %16, %9 ]
%15 = phi i8* [ %0, %6 ], [ %17, %9 ]
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = getelementptr inbounds i8, i8* %15, i64 1
store i8 %12, i8* %15
%18 = load i8, i8* %16
%19 = zext i8 %18 to i32
%20 = add nsw i32 %19, -1
%21 = icmp eq i32 %20, %7
%22 = add nuw i64 %13, 1
br i1 %21, label %23, label %9
23: ; preds = %9, %11, %4
%24 = phi i8* [ null, %4 ], [ %17, %11 ], [ null, %9 ]
ret i8* %24
}