#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_wday;
  int tm_yday;
  int tm_mon;
  int tm_mday;
  int /*<<< orphan*/ tm_year;
};

/* Variables and functions */
int /*<<< orphan*/ isleap(int /*<<< orphan*/);
int weeknumber(struct tm const *, int);
int iso8601wknum(const struct tm *timeptr) {
  int weeknum, jan1day;
  weeknum = weeknumber(timeptr, 1);
  jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
  if (jan1day < 0)
    jan1day += 7;
  switch (jan1day) {
  case 1:
    break;
  case 2:
  case 3:
  case 4:
    weeknum++;
    break;
  case 5:
  case 6:
  case 0:
    if (weeknum == 0) {
      struct tm dec31ly;
      dec31ly = *timeptr;
      dec31ly.tm_year--;
      dec31ly.tm_mon = 11;
      dec31ly.tm_mday = 31;
      dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
      dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900L);
      weeknum = iso8601wknum(&dec31ly);
    }
    break;
  }
  if (timeptr->tm_mon == 11) {
    int wday, mday;
    wday = timeptr->tm_wday;
    mday = timeptr->tm_mday;
    if ((wday == 1 && (mday >= 29 && mday <= 31)) ||
        (wday == 2 && (mday == 30 || mday == 31)) || (wday == 3 && mday == 31))
      weeknum = 1;
  }
  return weeknum;
}


x86 assembly (clang, -O0):
<iso8601wknum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <weeknumber@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x34(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1180 <iso8601wknum+0x50>
mov    -0x10(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x38(%rbp)
test   %eax,%eax
je     11df <iso8601wknum+0xaf>
jmp    1193 <iso8601wknum+0x63>
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
je     11cc <iso8601wknum+0x9c>
jmp    11a4 <iso8601wknum+0x74>
mov    -0x38(%rbp),%eax
add    $0xfffffffe,%eax
sub    $0x3,%eax
jb     11d1 <iso8601wknum+0xa1>
jmp    11b8 <iso8601wknum+0x88>
mov    -0x38(%rbp),%eax
add    $0xfffffffb,%eax
sub    $0x2,%eax
jb     11df <iso8601wknum+0xaf>
jmp    1269 <iso8601wknum+0x139>
jmp    1269 <iso8601wknum+0x139>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1269 <iso8601wknum+0x139>
cmpl   $0x0,-0xc(%rbp)
jne    1264 <iso8601wknum+0x134>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    %rcx,-0x28(%rbp)
mov    0x8(%rax),%rcx
mov    %rcx,-0x20(%rbp)
mov    0x10(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
movl   $0xb,-0x20(%rbp)
movl   $0x1f,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1230 <iso8601wknum+0x100>
mov    $0x6,%eax
mov    %eax,-0x3c(%rbp)
jmp    1239 <iso8601wknum+0x109>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x28(%rbp)
movslq -0x18(%rbp),%rax
add    $0x76c,%rax
mov    %eax,%edi
call   1030 <isleap@plt>
add    $0x16c,%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <iso8601wknum@plt>
mov    %eax,-0xc(%rbp)
jmp    1269 <iso8601wknum+0x139>
mov    -0x8(%rbp),%rax
cmpl   $0xb,0x8(%rax)
jne    12e6 <iso8601wknum+0x1b6>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jne    12a8 <iso8601wknum+0x178>
cmpl   $0x1d,-0x30(%rbp)
jl     12a8 <iso8601wknum+0x178>
cmpl   $0x1f,-0x30(%rbp)
jle    12da <iso8601wknum+0x1aa>
cmpl   $0x2,-0x2c(%rbp)
jne    12c6 <iso8601wknum+0x196>
cmpl   $0x1e,-0x30(%rbp)
je     12da <iso8601wknum+0x1aa>
cmpl   $0x1f,-0x30(%rbp)
je     12da <iso8601wknum+0x1aa>
cmpl   $0x3,-0x2c(%rbp)
jne    12e1 <iso8601wknum+0x1b1>
cmpl   $0x1f,-0x30(%rbp)
jne    12e1 <iso8601wknum+0x1b1>
movl   $0x1,-0xc(%rbp)
jmp    12e6 <iso8601wknum+0x1b6>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iso8601wknum>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    $0x1,%esi
call   1040 <weeknumber@plt>
mov    (%rbx),%edx
movslq 0x4(%rbx),%rcx
imul   $0xffffffff92492493,%rcx,%rsi
shr    $0x20,%rsi
add    %ecx,%esi
mov    %esi,%edi
shr    $0x1f,%edi
sar    $0x2,%esi
add    %edi,%esi
lea    0x0(,%rsi,8),%edi
sub    %esi,%edi
sub    %ecx,%edi
lea    (%rdx,%rdi,1),%ecx
add    $0x7,%ecx
add    %edi,%edx
cmovns %edx,%ecx
cmp    $0x6,%ecx
ja     11e5 <iso8601wknum+0xb5>
mov    $0x1c,%edx
bt     %ecx,%edx
jb     11e2 <iso8601wknum+0xb2>
mov    $0x61,%edx
bt     %ecx,%edx
jae    11e5 <iso8601wknum+0xb5>
test   %eax,%eax
jne    11e5 <iso8601wknum+0xb5>
mov    0x10(%rbx),%edi
mov    %edi,0x10(%rsp)
movups (%rbx),%xmm0
movaps %xmm0,(%rsp)
lea    -0x1(%rdi),%eax
mov    %eax,0x10(%rsp)
movabs $0x1f0000000b,%rax
mov    %rax,0x8(%rsp)
sub    $0x1,%ecx
mov    $0x6,%eax
cmovae %ecx,%eax
mov    %eax,(%rsp)
add    $0x76b,%edi
call   1030 <isleap@plt>
add    $0x16c,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
call   1050 <iso8601wknum@plt>
jmp    11e5 <iso8601wknum+0xb5>
add    $0x1,%eax
cmpl   $0xb,0x8(%rbx)
jne    121d <iso8601wknum+0xed>
mov    (%rbx),%edx
mov    0xc(%rbx),%ecx
cmp    $0x1,%edx
jne    11ff <iso8601wknum+0xcf>
cmp    $0x1d,%ecx
jl     11ff <iso8601wknum+0xcf>
cmp    $0x20,%ecx
jl     1218 <iso8601wknum+0xe8>
cmp    $0x2,%edx
jne    120e <iso8601wknum+0xde>
mov    %ecx,%esi
and    $0xfffffffe,%esi
cmp    $0x1e,%esi
je     1218 <iso8601wknum+0xe8>
cmp    $0x3,%edx
jne    121d <iso8601wknum+0xed>
cmp    $0x1f,%ecx
jne    121d <iso8601wknum+0xed>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32, i32, i32 }
define i32 @iso8601wknum(%struct.tm* %0) {
%2 = alloca %struct.tm*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.tm
%6 = alloca i32
%7 = alloca i32
store %struct.tm* %0, %struct.tm** %2
%8 = load %struct.tm*, %struct.tm** %2
%9 = call i32 @weeknumber(%struct.tm* %8, i32 1)
store i32 %9, i32* %3
%10 = load %struct.tm*, %struct.tm** %2
%11 = getelementptr inbounds %struct.tm, %struct.tm* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load %struct.tm*, %struct.tm** %2
%14 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = srem i32 %15, 7
%17 = sub nsw i32 %12, %16
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 7
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %1
%24 = load i32, i32* %4
switch i32 %24, label %60 [
i32 1, label %25
i32 2, label %26
i32 3, label %26
i32 4, label %26
i32 5, label %29
i32 6, label %29
i32 0, label %29
]
25: ; preds = %23
br label %60
26: ; preds = %23, %23, %23
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %60
29: ; preds = %23, %23, %23
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %59
32: ; preds = %29
%33 = load %struct.tm*, %struct.tm** %2
%34 = bitcast %struct.tm* %5 to i8*
%35 = bitcast %struct.tm* %33 to i8*
%36 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 4
%37 = load i32, i32* %36
%38 = add nsw i32 %37, -1
store i32 %38, i32* %36
%39 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 2
store i32 11, i32* %39
%40 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 3
store i32 31, i32* %40
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %32
br label %47
44: ; preds = %32
%45 = load i32, i32* %4
%46 = sub nsw i32 %45, 1
br label %47
47: ; preds = %44, %43
%48 = phi i32 [ 6, %43 ], [ %46, %44 ]
%49 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 0
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 4
%51 = load i32, i32* %50
%52 = sext i32 %51 to i64
%53 = add nsw i64 %52, 1900
%54 = trunc i64 %53 to i32
%55 = call i32 @isleap(i32 %54)
%56 = add nsw i32 364, %55
%57 = getelementptr inbounds %struct.tm, %struct.tm* %5, i32 0, i32 1
store i32 %56, i32* %57
%58 = call i32 @iso8601wknum(%struct.tm* %5)
store i32 %58, i32* %3
br label %59
59: ; preds = %47, %29
br label %60
60: ; preds = %23, %59, %26, %25
%61 = load %struct.tm*, %struct.tm** %2
%62 = getelementptr inbounds %struct.tm, %struct.tm* %61, i32 0, i32 2
%63 = load i32, i32* %62
%64 = icmp eq i32 %63, 11
br i1 %64, label %65, label %97
65: ; preds = %60
%66 = load %struct.tm*, %struct.tm** %2
%67 = getelementptr inbounds %struct.tm, %struct.tm* %66, i32 0, i32 0
%68 = load i32, i32* %67
store i32 %68, i32* %6
%69 = load %struct.tm*, %struct.tm** %2
%70 = getelementptr inbounds %struct.tm, %struct.tm* %69, i32 0, i32 3
%71 = load i32, i32* %70
store i32 %71, i32* %7
%72 = load i32, i32* %6
%73 = icmp eq i32 %72, 1
br i1 %73, label %74, label %80
74: ; preds = %65
%75 = load i32, i32* %7
%76 = icmp sge i32 %75, 29
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i32, i32* %7
%79 = icmp sle i32 %78, 31
br i1 %79, label %95, label %80
80: ; preds = %77, %74, %65
%81 = load i32, i32* %6
%82 = icmp eq i32 %81, 2
br i1 %82, label %83, label %89
83: ; preds = %80
%84 = load i32, i32* %7
%85 = icmp eq i32 %84, 30
br i1 %85, label %95, label %86
86: ; preds = %83
%87 = load i32, i32* %7
%88 = icmp eq i32 %87, 31
br i1 %88, label %95, label %89
89: ; preds = %86, %80
%90 = load i32, i32* %6
%91 = icmp eq i32 %90, 3
br i1 %91, label %92, label %96
92: ; preds = %89
%93 = load i32, i32* %7
%94 = icmp eq i32 %93, 31
br i1 %94, label %95, label %96
95: ; preds = %92, %86, %83, %77
store i32 1, i32* %3
br label %96
96: ; preds = %95, %92, %89
br label %97
97: ; preds = %96, %60
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32, i32 }
define i32 @iso8601wknum(%struct.tm* %0) {
%2 = alloca %struct.tm
%3 = tail call i32 @weeknumber(%struct.tm* %0, i32 1)
%4 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%7 = load i32, i32* %6
%8 = srem i32 %7, 7
%9 = sub nsw i32 %5, %8
%10 = icmp slt i32 %9, 0
%11 = add nsw i32 %9, 7
%12 = select i1 %10, i32 %11, i32 %9
switch i32 %12, label %34 [
i32 0, label %15
i32 2, label %13
i32 3, label %13
i32 4, label %13
i32 5, label %15
i32 6, label %15
]
13: ; preds = %1, %1, %1
%14 = add nsw i32 %3, 1
br label %34
15: ; preds = %1, %1, %1
%16 = icmp eq i32 %3, 0
br i1 %16, label %17, label %34
17: ; preds = %15
%18 = bitcast %struct.tm* %2 to i8*
%19 = bitcast %struct.tm* %0 to i8*
%20 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 4
%21 = load i32, i32* %20
%22 = add nsw i32 %21, -1
store i32 %22, i32* %20
%23 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 2
store i32 11, i32* %23
%24 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 3
store i32 31, i32* %24
%25 = icmp eq i32 %12, 0
%26 = add nsw i32 %12, -1
%27 = select i1 %25, i32 6, i32 %26
%28 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 0
store i32 %27, i32* %28
%29 = add i32 %21, 1899
%30 = tail call i32 @isleap(i32 %29)
%31 = add nsw i32 %30, 364
%32 = getelementptr inbounds %struct.tm, %struct.tm* %2, i64 0, i32 1
store i32 %31, i32* %32
%33 = call i32 @iso8601wknum(%struct.tm* %2)
br label %34
34: ; preds = %15, %17, %1, %13
%35 = phi i32 [ %3, %1 ], [ %14, %13 ], [ %33, %17 ], [ %3, %15 ]
%36 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 11
br i1 %38, label %39, label %58
39: ; preds = %34
%40 = load i32, i32* %4
%41 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 3
%42 = load i32, i32* %41
%43 = icmp eq i32 %40, 1
%44 = icmp sgt i32 %42, 28
%45 = select i1 %43, i1 %44, i1 false
%46 = icmp slt i32 %42, 32
%47 = select i1 %45, i1 %46, i1 false
br i1 %47, label %57, label %48
48: ; preds = %39
%49 = icmp eq i32 %40, 2
%50 = and i32 %42, -2
%51 = icmp eq i32 %50, 30
%52 = select i1 %49, i1 %51, i1 false
br i1 %52, label %57, label %53
53: ; preds = %48
%54 = icmp eq i32 %40, 3
%55 = icmp eq i32 %42, 31
%56 = select i1 %54, i1 %55, i1 false
br i1 %56, label %57, label %58
57: ; preds = %48, %53, %39
br label %58
58: ; preds = %53, %57, %34
%59 = phi i32 [ %35, %34 ], [ 1, %57 ], [ %35, %53 ]
ret i32 %59
}