#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 _wday(int, int);
int _week_num(int year, int yday, int wday, int first) {
  int result = 0;
  if (first <= (0 + 1)) {
    while (yday > (6 + 1)) {
      result += (_wday(year, yday) == first) ? 1 : 0;
      yday -= (6 + 1);
    }
  } else {
    result = ((yday - wday) + 10) / 7;
    if (result < 1) {
      result = 52;
    } else if (result > 52) {
      result = 1;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<_week_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x1,-0x10(%rbp)
jg     1179 <_week_num+0x69>
jmp    113a <_week_num+0x2a>
cmpl   $0x7,-0x8(%rbp)
jle    1174 <_week_num+0x64>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <_wday@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%esi
xor    %eax,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x7,%eax
mov    %eax,-0x8(%rbp)
jmp    113a <_week_num+0x2a>
jmp    11be <_week_num+0xae>
mov    -0x8(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0xa,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11a3 <_week_num+0x93>
movl   $0x34,-0x14(%rbp)
jmp    11b9 <_week_num+0xa9>
cmpl   $0x34,-0x14(%rbp)
jle    11b4 <_week_num+0xa4>
movl   $0x1,-0x14(%rbp)
jmp    11b9 <_week_num+0xa9>
jmp    11be <_week_num+0xae>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_week_num>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
cmp    $0x1,%ecx
jg     1150 <_week_num+0x40>
xor    %ebp,%ebp
cmp    $0x8,%ebx
jl     1184 <_week_num+0x74>
mov    %ecx,%r14d
mov    %edi,%r15d
xor    %ebp,%ebp
nopl   (%rax)
mov    %r15d,%edi
mov    %ebx,%esi
call   1030 <_wday@plt>
xor    %ecx,%ecx
cmp    %r14d,%eax
sete   %cl
add    %ecx,%ebp
lea    -0x7(%rbx),%eax
cmp    $0xe,%ebx
mov    %eax,%ebx
ja     1130 <_week_num+0x20>
jmp    1184 <_week_num+0x74>
sub    %edx,%ebx
mov    $0x34,%ebp
cmp    $0xfffffffd,%ebx
jl     1184 <_week_num+0x74>
mov    $0x1,%ebp
cmp    $0x168,%ebx
jg     1184 <_week_num+0x74>
add    $0xa,%ebx
mov    %ebx,%eax
imul   $0x24924925,%rax,%rax
shr    $0x20,%rax
sub    %eax,%ebx
shr    %ebx
add    %eax,%ebx
shr    $0x2,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @_week_num(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
%10 = load i32, i32* %8
%11 = icmp sle i32 %10, 1
br i1 %11, label %12, label %29
12: ; preds = %4
br label %13
13: ; preds = %16, %12
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 7
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = call i32 @_wday(i32 %17, i32 %18)
%20 = load i32, i32* %8
%21 = icmp eq i32 %19, %20
%22 = zext i1 %21 to i64
%23 = select i1 %21, i32 1, i32 0
%24 = load i32, i32* %9
%25 = add nsw i32 %24, %23
store i32 %25, i32* %9
%26 = load i32, i32* %6
%27 = sub nsw i32 %26, 7
store i32 %27, i32* %6
br label %13
28: ; preds = %13
br label %44
29: ; preds = %4
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = sub nsw i32 %30, %31
%33 = add nsw i32 %32, 10
%34 = sdiv i32 %33, 7
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 52, i32* %9
br label %43
38: ; preds = %29
%39 = load i32, i32* %9
%40 = icmp sgt i32 %39, 52
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 1, i32* %9
br label %42
42: ; preds = %41, %38
br label %43
43: ; preds = %42, %37
br label %44
44: ; preds = %43, %28
%45 = load i32, i32* %9
ret i32 %45
}

LLVM IR (-O2):
define i32 @_week_num(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp slt i32 %3, 2
br i1 %5, label %6, label %17
6: ; preds = %4
%7 = icmp sgt i32 %1, 7
br i1 %7, label %8, label %25
8: ; preds = %6, %8
%9 = phi i32 [ %14, %8 ], [ 0, %6 ]
%10 = phi i32 [ %15, %8 ], [ %1, %6 ]
%11 = tail call i32 @_wday(i32 %0, i32 %10)
%12 = icmp eq i32 %11, %3
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %9, %13
%15 = add nsw i32 %10, -7
%16 = icmp ugt i32 %10, 14
br i1 %16, label %8, label %25
17: ; preds = %4
%18 = sub nsw i32 %1, %2
%19 = icmp slt i32 %18, -3
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = add nsw i32 %18, 10
%22 = udiv i32 %21, 7
%23 = icmp sgt i32 %18, 360
%24 = select i1 %23, i32 1, i32 %22
br label %25
25: ; preds = %8, %6, %17, %20
%26 = phi i32 [ %24, %20 ], [ 52, %17 ], [ 0, %6 ], [ %14, %8 ]
ret i32 %26
}