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

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
}