#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 + 1900);
      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    1266 <iso8601wknum+0x136>
jmp    1266 <iso8601wknum+0x136>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1266 <iso8601wknum+0x136>
cmpl   $0x0,-0xc(%rbp)
jne    1261 <iso8601wknum+0x131>
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)
mov    -0x18(%rbp),%edi
add    $0x76c,%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    1266 <iso8601wknum+0x136>
mov    -0x8(%rbp),%rax
cmpl   $0xb,0x8(%rax)
jne    12e3 <iso8601wknum+0x1b3>
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    12a5 <iso8601wknum+0x175>
cmpl   $0x1d,-0x30(%rbp)
jl     12a5 <iso8601wknum+0x175>
cmpl   $0x1f,-0x30(%rbp)
jle    12d7 <iso8601wknum+0x1a7>
cmpl   $0x2,-0x2c(%rbp)
jne    12c3 <iso8601wknum+0x193>
cmpl   $0x1e,-0x30(%rbp)
je     12d7 <iso8601wknum+0x1a7>
cmpl   $0x1f,-0x30(%rbp)
je     12d7 <iso8601wknum+0x1a7>
cmpl   $0x3,-0x2c(%rbp)
jne    12de <iso8601wknum+0x1ae>
cmpl   $0x1f,-0x30(%rbp)
jne    12de <iso8601wknum+0x1ae>
movl   $0x1,-0xc(%rbp)
jmp    12e3 <iso8601wknum+0x1b3>
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 %59 [
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 %59
27: ; preds = %24, %24, %24
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %59
30: ; preds = %24, %24, %24
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %58
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 = add nsw i32 %52, 1900
%54 = call i32 @isleap(i32 %53)
%55 = add nsw i32 364, %54
%56 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 1
store i32 %55, i32* %56
%57 = call i32 @iso8601wknum(%struct.tm* %6)
store i32 %57, i32* %3
br label %58
58: ; preds = %48, %30
br label %59
59: ; preds = %24, %58, %27, %26
%60 = load %struct.tm*, %struct.tm** %2
%61 = getelementptr inbounds %struct.tm, %struct.tm* %60, i32 0, i32 2
%62 = load i32, i32* %61
%63 = icmp eq i32 %62, 11
br i1 %63, label %64, label %96
64: ; preds = %59
%65 = load %struct.tm*, %struct.tm** %2
%66 = getelementptr inbounds %struct.tm, %struct.tm* %65, i32 0, i32 0
%67 = load i32, i32* %66
store i32 %67, i32* %7
%68 = load %struct.tm*, %struct.tm** %2
%69 = getelementptr inbounds %struct.tm, %struct.tm* %68, i32 0, i32 3
%70 = load i32, i32* %69
store i32 %70, i32* %8
%71 = load i32, i32* %7
%72 = icmp eq i32 %71, 1
br i1 %72, label %73, label %79
73: ; preds = %64
%74 = load i32, i32* %8
%75 = icmp sge i32 %74, 29
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i32, i32* %8
%78 = icmp sle i32 %77, 31
br i1 %78, label %94, label %79
79: ; preds = %76, %73, %64
%80 = load i32, i32* %7
%81 = icmp eq i32 %80, 2
br i1 %81, label %82, label %88
82: ; preds = %79
%83 = load i32, i32* %8
%84 = icmp eq i32 %83, 30
br i1 %84, label %94, label %85
85: ; preds = %82
%86 = load i32, i32* %8
%87 = icmp eq i32 %86, 31
br i1 %87, label %94, label %88
88: ; preds = %85, %79
%89 = load i32, i32* %7
%90 = icmp eq i32 %89, 3
br i1 %90, label %91, label %95
91: ; preds = %88
%92 = load i32, i32* %8
%93 = icmp eq i32 %92, 31
br i1 %93, label %94, label %95
94: ; preds = %91, %85, %82, %76
store i32 1, i32* %3
br label %95
95: ; preds = %94, %91, %88
br label %96
96: ; preds = %95, %59
%97 = load i32, i32* %3
ret i32 %97
}

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 nsw 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
}