#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 tm {
  unsigned int tm_yday;
  unsigned int tm_wday;
  int tm_year;
};

/* Variables and functions */
scalar_t__ is_leap(int);
int week_num(const struct tm *tm) {
  int val = (tm->tm_yday + 7U - (tm->tm_wday + 6U) % 7) / 7;
  if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2)
    val++;
  if (!val) {
    val = 52;
    int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7;
    if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year % 400 - 1)))
      val++;
  } else if (val == 53) {
    int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7;
    if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
      val = 1;
  }
  return val;
}


x86 assembly (clang, -O0):
<week_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
add    $0x7,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x6,%eax
mov    $0x7,%ecx
xor    %edx,%edx
div    %ecx
mov    -0x18(%rbp),%eax
sub    %edx,%eax
mov    $0x7,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x173,%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
sub    $0x2,%eax
mov    $0x7,%ecx
xor    %edx,%edx
div    %ecx
cmp    $0x2,%edx
ja     117c <week_num+0x6c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11f1 <week_num+0xe1>
movl   $0x34,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x7,%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
sub    $0x1,%eax
mov    $0x7,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
je     11e3 <week_num+0xd3>
cmpl   $0x5,-0x10(%rbp)
jne    11ec <week_num+0xdc>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    $0x190,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
sub    $0x1,%edi
call   1030 <is_leap@plt>
cmp    $0x0,%rax
je     11ec <week_num+0xdc>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1254 <week_num+0x144>
cmpl   $0x35,-0xc(%rbp)
jne    124f <week_num+0x13f>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x173,%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
mov    $0x7,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
je     124a <week_num+0x13a>
cmpl   $0x3,-0x14(%rbp)
jne    1243 <week_num+0x133>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <is_leap@plt>
cmp    $0x0,%rax
jne    124a <week_num+0x13a>
movl   $0x1,-0xc(%rbp)
jmp    124f <week_num+0x13f>
jmp    1254 <week_num+0x144>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<week_num>:
push   %rbx
mov    (%rdi),%ecx
mov    0x4(%rdi),%eax
lea    0x6(%rax),%ebx
imul   $0x24924925,%rbx,%rsi
shr    $0x20,%rsi
mov    %ebx,%edx
sub    %esi,%edx
shr    %edx
add    %esi,%edx
shr    $0x2,%edx
lea    0x0(,%rdx,8),%esi
sub    %edx,%esi
sub    %ebx,%esi
lea    (%rcx,%rsi,1),%ebx
add    $0x7,%ebx
imul   $0x24924925,%rbx,%rdx
shr    $0x20,%rdx
sub    %edx,%ebx
shr    %ebx
add    %edx,%ebx
shr    $0x2,%ebx
sub    %ecx,%eax
lea    0x171(%rax),%ecx
imul   $0x24924925,%rcx,%rdx
shr    $0x20,%rdx
sub    %edx,%ecx
shr    %ecx
add    %edx,%ecx
shr    $0x2,%ecx
lea    0x0(,%rcx,8),%edx
sub    %edx,%ecx
add    %eax,%ecx
add    $0x171,%ecx
cmp    $0x3,%ecx
adc    $0x0,%ebx
cmp    $0x35,%ebx
je     11fb <week_num+0xeb>
test   %ebx,%ebx
jne    1244 <week_num+0x134>
lea    0x6(%rax),%ecx
imul   $0x24924925,%rcx,%rdx
shr    $0x20,%rdx
sub    %edx,%ecx
shr    %ecx
add    %edx,%ecx
shr    $0x2,%ecx
lea    0x0(,%rcx,8),%edx
sub    %edx,%ecx
add    %ecx,%eax
add    $0x6,%eax
cmp    $0x4,%eax
je     11f4 <week_num+0xe4>
mov    $0x34,%ebx
cmp    $0x5,%eax
jne    1244 <week_num+0x134>
movslq 0x8(%rdi),%rax
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x27,%rcx
add    %edx,%ecx
imul   $0x190,%ecx,%edi
not    %edi
add    %eax,%edi
call   1030 <is_leap@plt>
test   %rax,%rax
je     1244 <week_num+0x134>
mov    $0x35,%ebx
jmp    1244 <week_num+0x134>
add    $0x173,%eax
mov    %eax,%ecx
imul   $0x24924925,%rcx,%rdx
shr    $0x20,%rdx
mov    %eax,%ecx
sub    %edx,%ecx
shr    %ecx
add    %edx,%ecx
shr    $0x2,%ecx
lea    0x0(,%rcx,8),%edx
sub    %edx,%ecx
add    %eax,%ecx
mov    $0x35,%ebx
cmp    $0x4,%ecx
je     1244 <week_num+0x134>
cmp    $0x3,%ecx
jne    123f <week_num+0x12f>
mov    0x8(%rdi),%edi
call   1030 <is_leap@plt>
test   %rax,%rax
jne    1244 <week_num+0x134>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32 }
define i32 @week_num(%struct.tm* %0) {
%2 = alloca %struct.tm*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store %struct.tm* %0, %struct.tm** %2
%6 = load %struct.tm*, %struct.tm** %2
%7 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = add i32 %8, 7
%10 = load %struct.tm*, %struct.tm** %2
%11 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 1
%12 = load i32, i32* %11
%13 = add i32 %12, 6
%14 = urem i32 %13, 7
%15 = sub i32 %9, %14
%16 = udiv i32 %15, 7
store i32 %16, i32* %3
%17 = load %struct.tm*, %struct.tm** %2
%18 = getelementptr inbounds %struct.tm, %struct.tm* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = add i32 %19, 371
%21 = load %struct.tm*, %struct.tm** %2
%22 = getelementptr inbounds %struct.tm, %struct.tm* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sub i32 %20, %23
%25 = sub i32 %24, 2
%26 = urem i32 %25, 7
%27 = icmp ule i32 %26, 2
br i1 %27, label %28, label %31
28: ; preds = %1
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %1
%32 = load i32, i32* %3
%33 = icmp ne i32 %32, 0
br i1 %33, label %62, label %34
34: ; preds = %31
store i32 52, i32* %3
%35 = load %struct.tm*, %struct.tm** %2
%36 = getelementptr inbounds %struct.tm, %struct.tm* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = add i32 %37, 7
%39 = load %struct.tm*, %struct.tm** %2
%40 = getelementptr inbounds %struct.tm, %struct.tm* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sub i32 %38, %41
%43 = sub i32 %42, 1
%44 = urem i32 %43, 7
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 4
br i1 %46, label %58, label %47
47: ; preds = %34
%48 = load i32, i32* %4
%49 = icmp eq i32 %48, 5
br i1 %49, label %50, label %61
50: ; preds = %47
%51 = load %struct.tm*, %struct.tm** %2
%52 = getelementptr inbounds %struct.tm, %struct.tm* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = srem i32 %53, 400
%55 = sub nsw i32 %54, 1
%56 = call i64 @is_leap(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %50, %34
%59 = load i32, i32* %3
%60 = add nsw i32 %59, 1
store i32 %60, i32* %3
br label %61
61: ; preds = %58, %50, %47
br label %89
62: ; preds = %31
%63 = load i32, i32* %3
%64 = icmp eq i32 %63, 53
br i1 %64, label %65, label %88
65: ; preds = %62
%66 = load %struct.tm*, %struct.tm** %2
%67 = getelementptr inbounds %struct.tm, %struct.tm* %66, i32 0, i32 1
%68 = load i32, i32* %67
%69 = add i32 %68, 371
%70 = load %struct.tm*, %struct.tm** %2
%71 = getelementptr inbounds %struct.tm, %struct.tm* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = sub i32 %69, %72
%74 = urem i32 %73, 7
store i32 %74, i32* %5
%75 = load i32, i32* %5
%76 = icmp ne i32 %75, 4
br i1 %76, label %77, label %87
77: ; preds = %65
%78 = load i32, i32* %5
%79 = icmp ne i32 %78, 3
br i1 %79, label %86, label %80
80: ; preds = %77
%81 = load %struct.tm*, %struct.tm** %2
%82 = getelementptr inbounds %struct.tm, %struct.tm* %81, i32 0, i32 2
%83 = load i32, i32* %82
%84 = call i64 @is_leap(i32 %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %87, label %86
86: ; preds = %80, %77
store i32 1, i32* %3
br label %87
87: ; preds = %86, %80, %65
br label %88
88: ; preds = %87, %62
br label %89
89: ; preds = %88, %61
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
define i32 @week_num(%struct.tm* nocapture %0) {
%2 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = add i32 %3, 7
%5 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%6 = load i32, i32* %5
%7 = add i32 %6, 6
%8 = urem i32 %7, 7
%9 = sub i32 %4, %8
%10 = udiv i32 %9, 7
%11 = sub i32 371, %3
%12 = add i32 %11, %6
%13 = add i32 %12, -2
%14 = urem i32 %13, 7
%15 = icmp ult i32 %14, 3
%16 = zext i1 %15 to i32
%17 = add nuw nsw i32 %10, %16
switch i32 %17, label %38 [
i32 0, label %18
i32 53, label %30
]
18: ; preds = %1
%19 = sub i32 6, %3
%20 = add i32 %19, %6
%21 = urem i32 %20, 7
switch i32 %21, label %38 [
i32 4, label %29
i32 5, label %22
]
22: ; preds = %18
%23 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%24 = load i32, i32* %23
%25 = srem i32 %24, 400
%26 = add nsw i32 %25, -1
%27 = tail call i64 @is_leap(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %38, label %29
29: ; preds = %18, %22
br label %38
30: ; preds = %1
%31 = urem i32 %12, 7
switch i32 %31, label %37 [
i32 4, label %38
i32 3, label %32
]
32: ; preds = %30
%33 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%34 = load i32, i32* %33
%35 = tail call i64 @is_leap(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30, %32
br label %38
38: ; preds = %32, %37, %30, %1, %22, %29, %18
%39 = phi i32 [ 53, %29 ], [ 52, %22 ], [ 52, %18 ], [ %17, %1 ], [ 1, %37 ], [ 53, %32 ], [ 53, %30 ]
ret i32 %39
}