#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 tpg_data {
  int dummy;
};

/* Variables and functions */
unsigned int tpg_get_pat_lines(struct tpg_data const *);
int tpg_pattern_avg(const struct tpg_data *tpg, unsigned pat1, unsigned pat2) {
  unsigned pat_lines = tpg_get_pat_lines(tpg);
  if (pat1 == (pat2 + 1) % pat_lines)
    return pat2;
  if (pat2 == (pat1 + 1) % pat_lines)
    return pat1;
  return -1;
}


x86 assembly (clang, -O0):
<tpg_pattern_avg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tpg_get_pat_lines@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    -0x20(%rbp),%eax
cmp    %edx,%eax
jne    1155 <tpg_pattern_avg+0x45>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1183 <tpg_pattern_avg+0x73>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    -0x24(%rbp),%eax
cmp    %edx,%eax
jne    117c <tpg_pattern_avg+0x6c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1183 <tpg_pattern_avg+0x73>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tpg_pattern_avg>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
call   1030 <tpg_get_pat_lines@plt>
mov    %eax,%ecx
lea    0x1(%rbx),%eax
xor    %edx,%edx
div    %ecx
cmp    %r14d,%edx
je     113f <tpg_pattern_avg+0x2f>
lea    0x1(%r14),%eax
xor    %edx,%edx
div    %ecx
cmp    %ebx,%edx
mov    $0xffffffff,%ebx
cmove  %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.tpg_data = type { i32 }
define i32 @tpg_pattern_avg(%struct.tpg_data* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.tpg_data*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.tpg_data* %0, %struct.tpg_data** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load %struct.tpg_data*, %struct.tpg_data** %5
%10 = call i32 @tpg_get_pat_lines(%struct.tpg_data* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = add i32 %12, 1
%14 = load i32, i32* %8
%15 = urem i32 %13, %14
%16 = icmp eq i32 %11, %15
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %7
store i32 %18, i32* %4
br label %29
19: ; preds = %3
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = add i32 %21, 1
%23 = load i32, i32* %8
%24 = urem i32 %22, %23
%25 = icmp eq i32 %20, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = load i32, i32* %6
store i32 %27, i32* %4
br label %29
28: ; preds = %19
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %26, %17
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.tpg_data = type { i32 }
define i32 @tpg_pattern_avg(%struct.tpg_data* %0, i32 %1, i32 %2) {
%4 = tail call i32 @tpg_get_pat_lines(%struct.tpg_data* %0)
%5 = add i32 %2, 1
%6 = urem i32 %5, %4
%7 = icmp eq i32 %6, %1
br i1 %7, label %13, label %8
8: ; preds = %3
%9 = add i32 %1, 1
%10 = urem i32 %9, %4
%11 = icmp eq i32 %10, %2
%12 = select i1 %11, i32 %1, i32 -1
br label %13
13: ; preds = %8, %3
%14 = phi i32 [ %2, %3 ], [ %12, %8 ]
ret i32 %14
}