#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 rand();
int /*<<< orphan*/ sprintf(char *, char *, ...);
void create_magic_pattern(char *buf, const char *name) {
  int i, magic, mask, offset;
  magic = rand() % (16 + 1);
  mask = (rand() % 2) ? magic : 0;
  offset = 128 - magic;
  offset = rand() % (offset + 1);
  buf += sprintf(buf, ":%s:M:%d:", name, offset);
  for (i = 0; i < magic; i++)
    buf += sprintf(buf, "\\x%02x", rand() % 256);
  buf += sprintf(buf, ":");
  for (i = 0; i < mask; i++)
    buf += sprintf(buf, "\\x%02x", rand() % 256);
  sprintf(buf, ":/bin/interpreter:OCP");
}


x86 assembly (clang, -O0):
<create_magic_pattern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x11,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1165 <create_magic_pattern+0x45>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    116f <create_magic_pattern+0x4f>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    116f <create_magic_pattern+0x4f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x80,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1216 <create_magic_pattern+0xf6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x30(%rbp),%rdi
mov    $0x100,%ecx
cltd
idiv   %ecx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c3 <create_magic_pattern+0xa3>
mov    -0x8(%rbp),%rdi
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1292 <create_magic_pattern+0x172>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x38(%rbp),%rdi
mov    $0x100,%ecx
cltd
idiv   %ecx
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123f <create_magic_pattern+0x11f>
mov    -0x8(%rbp),%rdi
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_magic_pattern>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rbp
imul   $0x78787879,%rbp,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
mov    %eax,%ecx
shl    $0x4,%ecx
add    %eax,%ecx
sub    %ecx,%ebp
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,%r15d
and    $0x1,%r15d
neg    %r15d
and    %ebp,%r15d
xor    %eax,%eax
call   1040 <rand@plt>
mov    $0x81,%ecx
sub    %ebp,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
lea    0xe7e(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <sprintf@plt>
movslq %eax,%rbx
add    %r14,%rbx
test   %ebp,%ebp
jle    11d3 <create_magic_pattern+0xb3>
lea    0xe6a(%rip),%r14
xor    %eax,%eax
call   1040 <rand@plt>
lea    0xff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffff00,%ecx
sub    %ecx,%eax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0xffffffff,%ebp
jne    11a0 <create_magic_pattern+0x80>
movw   $0x3a,(%rbx)
add    $0x1,%rbx
test   %r15d,%r15d
jle    1224 <create_magic_pattern+0x104>
lea    0xe22(%rip),%r14
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
lea    0xff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffff00,%ecx
sub    %ecx,%eax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0xffffffff,%r15d
jne    11f0 <create_magic_pattern+0xd0>
movups 0xde6(%rip),%xmm0
movups %xmm0,(%rbx)
movabs $0x50434f3a726574,%rax
mov    %rax,0xe(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c":%s:M:%d:\00"
@.str.1 = [7 x i8] c"\\x%02x\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [22 x i8] c":/bin/interpreter:OCP\00"
define void @create_magic_pattern(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = call i32 (...) @rand()
%10 = srem i32 %9, 17
store i32 %10, i32* %6
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %6
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %14
%18 = phi i32 [ %15, %14 ], [ 0, %16 ]
store i32 %18, i32* %7
%19 = load i32, i32* %6
%20 = sub nsw i32 128, %19
store i32 %20, i32* %8
%21 = call i32 (...) @rand()
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
%24 = srem i32 %21, %23
store i32 %24, i32* %8
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = load i32, i32* %8
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %27)
%29 = load i8*, i8** %3
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %3
store i32 0, i32* %5
br label %32
32: ; preds = %44, %17
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = call i32 (...) @rand()
%39 = srem i32 %38, 256
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = load i8*, i8** %3
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %3
br label %44
44: ; preds = %36
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %32
47: ; preds = %32
%48 = load i8*, i8** %3
%49 = call i32 (i8*, i8*, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%50 = load i8*, i8** %3
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %3
store i32 0, i32* %5
br label %53
53: ; preds = %65, %47
%54 = load i32, i32* %5
%55 = load i32, i32* %7
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %53
%58 = load i8*, i8** %3
%59 = call i32 (...) @rand()
%60 = srem i32 %59, 256
%61 = call i32 (i8*, i8*, ...) @sprintf(i8* %58, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %60)
%62 = load i8*, i8** %3
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i8, i8* %62, i64 %63
store i8* %64, i8** %3
br label %65
65: ; preds = %57
%66 = load i32, i32* %5
%67 = add nsw i32 %66, 1
store i32 %67, i32* %5
br label %53
68: ; preds = %53
%69 = load i8*, i8** %3
%70 = call i32 (i8*, i8*, ...) @sprintf(i8* %69, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c":%s:M:%d:\00"
@.str.1 = [7 x i8] c"\\x%02x\00"
@.str.3 = [22 x i8] c":/bin/interpreter:OCP\00"
define void @create_magic_pattern(i8* nocapture writeonly %0, i8* %1) {
%3 = tail call i32 (...) @rand()
%4 = srem i32 %3, 17
%5 = tail call i32 (...) @rand()
%6 = and i32 %5, 1
%7 = icmp eq i32 %6, 0
%8 = select i1 %7, i32 0, i32 %4
%9 = tail call i32 (...) @rand()
%10 = sub nsw i32 129, %4
%11 = srem i32 %9, %10
%12 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = icmp sgt i32 %4, 0
br i1 %15, label %16, label %26
16: ; preds = %2, %16
%17 = phi i8* [ %23, %16 ], [ %14, %2 ]
%18 = phi i32 [ %24, %16 ], [ 0, %2 ]
%19 = tail call i32 (...) @rand()
%20 = srem i32 %19, 256
%21 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, %4
br i1 %25, label %26, label %16
26: ; preds = %16, %2
%27 = phi i8* [ %14, %2 ], [ %23, %16 ]
%28 = bitcast i8* %27 to i16*
store i16 58, i16* %28
%29 = getelementptr inbounds i8, i8* %27, i64 1
%30 = icmp sgt i32 %8, 0
br i1 %30, label %31, label %41
31: ; preds = %26, %31
%32 = phi i8* [ %38, %31 ], [ %29, %26 ]
%33 = phi i32 [ %39, %31 ], [ 0, %26 ]
%34 = tail call i32 (...) @rand()
%35 = srem i32 %34, 256
%36 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %32, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %32, i64 %37
%39 = add nuw nsw i32 %33, 1
%40 = icmp eq i32 %39, %8
br i1 %40, label %41, label %31
41: ; preds = %31, %26
%42 = phi i8* [ %29, %26 ], [ %38, %31 ]
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(22) %42, i8* align 1 dereferenceable(22) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 22, i1 false)
ret void
}