#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 {
  int tm_yday;
  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 + 7 - (tm->tm_wday + 6) % 7) / 7;
  if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
    val++;
  if (!val) {
    val = 52;
    int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
    if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year % 400 - 1)))
      val++;
  } else if (val == 53) {
    int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 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
cltd
idiv   %ecx
mov    -0x18(%rbp),%eax
sub    %edx,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
sub    $0x2,%eax
add    $0x173,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
cmp    $0x2,%edx
jg     1179 <week_num+0x69>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11ed <week_num+0xdd>
movl   $0x34,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
sub    $0x1,%eax
add    $0x7,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
je     11df <week_num+0xcf>
cmpl   $0x5,-0x10(%rbp)
jne    11e8 <week_num+0xd8>
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     11e8 <week_num+0xd8>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    124f <week_num+0x13f>
cmpl   $0x35,-0xc(%rbp)
jne    124a <week_num+0x13a>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    -0x8(%rbp),%rcx
sub    (%rcx),%eax
add    $0x173,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
je     1245 <week_num+0x135>
cmpl   $0x3,-0x14(%rbp)
jne    123e <week_num+0x12e>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <is_leap@plt>
cmp    $0x0,%rax
jne    1245 <week_num+0x135>
movl   $0x1,-0xc(%rbp)
jmp    124a <week_num+0x13a>
jmp    124f <week_num+0x13f>
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),%edx
movslq %edx,%rdx
imul   $0xffffffff92492493,%rdx,%rsi
shr    $0x20,%rsi
add    %eax,%esi
add    $0x6,%esi
mov    %esi,%ebx
shr    $0x1f,%ebx
sar    $0x2,%esi
add    %ebx,%esi
lea    0x0(,%rsi,8),%ebx
sub    %esi,%ebx
sub    %edx,%ebx
lea    (%rcx,%rbx,1),%edx
add    $0x7,%edx
add    %ecx,%ebx
movslq %edx,%rdx
imul   $0xffffffff92492493,%rdx,%rdx
shr    $0x20,%rdx
add    %ebx,%edx
add    $0x7,%edx
mov    %edx,%esi
shr    $0x1f,%esi
sar    $0x2,%edx
add    %esi,%edx
sub    %ecx,%eax
lea    0x171(%rax),%ecx
movslq %ecx,%rcx
imul   $0xffffffff92492493,%rcx,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
add    $0x171,%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
sar    $0x2,%ecx
add    %esi,%ecx
lea    0x0(,%rcx,8),%esi
sub    %esi,%ecx
add    %eax,%ecx
add    $0x171,%ecx
xor    %ebx,%ebx
cmp    $0x3,%ecx
setl   %bl
add    %edx,%ebx
cmp    $0x35,%ebx
je     122c <week_num+0x11c>
test   %ebx,%ebx
jne    1285 <week_num+0x175>
mov    %eax,%ecx
add    $0x6,%ecx
movslq %ecx,%rcx
imul   $0xffffffff92492493,%rcx,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
add    $0x6,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x2,%ecx
add    %edx,%ecx
lea    0x0(,%rcx,8),%edx
sub    %edx,%ecx
add    %ecx,%eax
add    $0x6,%eax
cmp    $0x4,%eax
je     1225 <week_num+0x115>
mov    $0x34,%ebx
cmp    $0x5,%eax
jne    1285 <week_num+0x175>
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     1285 <week_num+0x175>
mov    $0x35,%ebx
jmp    1285 <week_num+0x175>
mov    %eax,%ecx
add    $0x173,%ecx
movslq %ecx,%rcx
imul   $0xffffffff92492493,%rcx,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
add    $0x173,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x2,%ecx
add    %edx,%ecx
lea    0x0(,%rcx,8),%edx
sub    %edx,%ecx
add    %ecx,%eax
add    $0x173,%eax
mov    $0x35,%ebx
cmp    $0x4,%eax
je     1285 <week_num+0x175>
cmp    $0x3,%eax
jne    1280 <week_num+0x170>
mov    0x8(%rdi),%edi
call   1030 <is_leap@plt>
test   %rax,%rax
jne    1285 <week_num+0x175>
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 nsw 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 nsw i32 %12, 6
%14 = srem i32 %13, 7
%15 = sub nsw i32 %9, %14
%16 = sdiv 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 = load %struct.tm*, %struct.tm** %2
%21 = getelementptr inbounds %struct.tm, %struct.tm* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = sub nsw i32 %19, %22
%24 = sub nsw i32 %23, 2
%25 = add nsw i32 %24, 371
%26 = srem i32 %25, 7
%27 = icmp sle 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 = load %struct.tm*, %struct.tm** %2
%39 = getelementptr inbounds %struct.tm, %struct.tm* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = sub nsw i32 %37, %40
%42 = sub nsw i32 %41, 1
%43 = add nsw i32 %42, 7
%44 = srem 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 = load %struct.tm*, %struct.tm** %2
%70 = getelementptr inbounds %struct.tm, %struct.tm* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = sub nsw i32 %68, %71
%73 = add nsw i32 %72, 371
%74 = srem 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 nsw i32 %3, 7
%5 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%6 = load i32, i32* %5
%7 = add nsw i32 %6, 6
%8 = srem i32 %7, 7
%9 = sub i32 %4, %8
%10 = sdiv i32 %9, 7
%11 = sub nsw i32 %6, %3
%12 = add nsw i32 %11, 369
%13 = srem i32 %12, 7
%14 = icmp slt i32 %13, 3
%15 = zext i1 %14 to i32
%16 = add nsw i32 %10, %15
switch i32 %16, label %38 [
i32 0, label %17
i32 53, label %29
]
17: ; preds = %1
%18 = sub i32 6, %3
%19 = add i32 %18, %6
%20 = srem i32 %19, 7
switch i32 %20, label %38 [
i32 4, label %28
i32 5, label %21
]
21: ; preds = %17
%22 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%23 = load i32, i32* %22
%24 = srem i32 %23, 400
%25 = add nsw i32 %24, -1
%26 = tail call i64 @is_leap(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %17, %21
br label %38
29: ; preds = %1
%30 = add nsw i32 %11, 371
%31 = srem i32 %30, 7
switch i32 %31, label %37 [
i32 4, label %38
i32 3, label %32
]
32: ; preds = %29
%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 = %29, %32
br label %38
38: ; preds = %32, %37, %29, %1, %21, %28, %17
%39 = phi i32 [ 53, %28 ], [ 52, %21 ], [ 52, %17 ], [ %16, %1 ], [ 1, %37 ], [ 53, %32 ], [ 53, %29 ]
ret i32 %39
}