#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 */
struct dat {
  int dummy;
};

/* Variables and functions */
int insert_pattern(struct dat *, unsigned char *, unsigned long,
                   int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, unsigned char *, ...);
unsigned long strlen(char *);
int build_dat(struct dat *d, unsigned char *pats[256], int pat_count) {
  int i = 0;
  int ret = 0;
  unsigned long len = 0;
  unsigned char *pattern = (unsigned char *)(pats);
  for (; i < pat_count; i++, pattern += 256) {
    len = strlen((char *)pattern);
    if (len >= 256 - 1) {
      printf("pattern [index=%d %s] too long, length=%lu\n", i, pattern, len);
      continue;
    }
    ret = insert_pattern(d, pattern, len, 0, NULL);
    if (ret < 0) {
      printf("insert pattern index = %d %s failed\n", i, pattern);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<build_dat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1201 <build_dat+0xd1>
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xff,-0x28(%rbp)
jb     11a6 <build_dat+0x76>
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e5 <build_dat+0xb5>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <insert_pattern@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e0 <build_dat+0xb0>
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e5 <build_dat+0xb5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x30(%rbp)
jmp    1161 <build_dat+0x31>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_dat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11b9 <build_dat+0x89>
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xeb1(%rip),%r15
lea    0xed6(%rip),%r12
xor    %ebp,%ebp
jmp    1181 <build_dat+0x51>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
add    $0x100,%rbx
cmp    %ebp,%r13d
je     11b9 <build_dat+0x89>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0xff,%rax
jae    1160 <build_dat+0x30>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <insert_pattern@plt>
test   %eax,%eax
jns    1172 <build_dat+0x42>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1172 <build_dat+0x42>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dat = type { i32 }
@.str = [44 x i8] c"pattern [index=%d %s] too long, length=%lu\0A\00"
@.str.1 = [37 x i8] c"insert pattern index = %d %s failed\0A\00"
define i32 @build_dat(%struct.dat* %0, i8** %1, i32 %2) {
%4 = alloca %struct.dat*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
store %struct.dat* %0, %struct.dat** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
%11 = load i8**, i8*** %5
%12 = bitcast i8** %11 to i8*
store i8* %12, i8** %10
br label %13
13: ; preds = %39, %3
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %44
17: ; preds = %13
%18 = load i8*, i8** %10
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = icmp uge i64 %20, 255
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* %7
%24 = load i8*, i8** %10
%25 = load i64, i64* %9
%26 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %23, i8* %24, i64 %25)
br label %39
27: ; preds = %17
%28 = load %struct.dat*, %struct.dat** %4
%29 = load i8*, i8** %10
%30 = load i64, i64* %9
%31 = call i32 @insert_pattern(%struct.dat* %28, i8* %29, i64 %30, i32 0, i32* null)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32, i32* %7
%36 = load i8*, i8** %10
%37 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %35, i8* %36)
br label %38
38: ; preds = %34, %27
br label %39
39: ; preds = %38, %22
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i64 256
store i8* %43, i8** %10
br label %13
44: ; preds = %13
ret i32 0
}

LLVM IR (-O2):
%struct.dat = type { i32 }
@.str = [44 x i8] c"pattern [index=%d %s] too long, length=%lu\0A\00"
@.str.1 = [37 x i8] c"insert pattern index = %d %s failed\0A\00"
define i32 @build_dat(%struct.dat* %0, i8** %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %23
5: ; preds = %3
%6 = bitcast i8** %1 to i8*
br label %7
7: ; preds = %5, %19
%8 = phi i8* [ %21, %19 ], [ %6, %5 ]
%9 = phi i32 [ %20, %19 ], [ 0, %5 ]
%10 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%11 = icmp ugt i64 %10, 254
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %8, i64 %10)
br label %19
14: ; preds = %7
%15 = tail call i32 @insert_pattern(%struct.dat* %0, i8* %8, i64 %10, i32 0, i32* null)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %8)
br label %19
19: ; preds = %14, %17, %12
%20 = add nuw nsw i32 %9, 1
%21 = getelementptr inbounds i8, i8* %8, i64 256
%22 = icmp eq i32 %20, %2
br i1 %22, label %23, label %7
23: ; preds = %19, %3
ret i32 0
}