#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;
  }
  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,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x2c(%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,-0x30(%rbp)
test   %eax,%eax
je     11df <iso8601wknum+0xaf>
jmp    1193 <iso8601wknum+0x63>
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
je     11cc <iso8601wknum+0x9c>
jmp    11a4 <iso8601wknum+0x74>
mov    -0x30(%rbp),%eax
add    $0xfffffffe,%eax
sub    $0x3,%eax
jb     11d1 <iso8601wknum+0xa1>
jmp    11b8 <iso8601wknum+0x88>
mov    -0x30(%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,-0x34(%rbp)
jmp    1239 <iso8601wknum+0x109>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%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    -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
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
store %struct.tm* %0, %struct.tm** %2
%7 = load %struct.tm*, %struct.tm** %2
%8 = call i32 @weeknumber(%struct.tm* %7, i32 1)
store i32 %8, i32* %3
%9 = load %struct.tm*, %struct.tm** %2
%10 = getelementptr inbounds %struct.tm, %struct.tm* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load %struct.tm*, %struct.tm** %2
%13 = getelementptr inbounds %struct.tm, %struct.tm* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = srem i32 %14, 7
%16 = sub nsw i32 %11, %15
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 7
store i32 %21, i32* %4
br label %22
22: ; preds = %19, %1
%23 = load i32, i32* %4
switch i32 %23, label %57 [
i32 1, label %24
i32 2, label %25
i32 3, label %25
i32 4, label %25
i32 5, label %28
i32 6, label %28
i32 0, label %28
]
24: ; preds = %22
br label %57
25: ; preds = %22, %22, %22
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %57
28: ; preds = %22, %22, %22
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %56
31: ; preds = %28
%32 = load %struct.tm*, %struct.tm** %2
%33 = bitcast %struct.tm* %6 to i8*
%34 = bitcast %struct.tm* %32 to i8*
%35 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 4
%36 = load i32, i32* %35
%37 = add nsw i32 %36, -1
store i32 %37, i32* %35
%38 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 2
store i32 11, i32* %38
%39 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 3
store i32 31, i32* %39
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %31
br label %46
43: ; preds = %31
%44 = load i32, i32* %4
%45 = sub nsw i32 %44, 1
br label %46
46: ; preds = %43, %42
%47 = phi i32 [ 6, %42 ], [ %45, %43 ]
%48 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 0
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 4
%50 = load i32, i32* %49
%51 = add nsw i32 %50, 1900
%52 = call i32 @isleap(i32 %51)
%53 = add nsw i32 364, %52
%54 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 1
store i32 %53, i32* %54
%55 = call i32 @iso8601wknum(%struct.tm* %6)
store i32 %55, i32* %3
br label %56
56: ; preds = %46, %28
br label %57
57: ; preds = %22, %56, %25, %24
%58 = load i32, i32* %3
ret i32 %58
}

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 ]
ret i32 %35
}