#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;
  scalar_t__ tm_year;
};

/* Variables and functions */
int tm_isleapyear(scalar_t__);
int tm_getisoweek(struct tm date) {
  int week = (date.tm_yday - (date.tm_wday + 6) % 7 + 10) / 7;
  if (week == 0)
    return (date.tm_yday + 365 + tm_isleapyear(date.tm_year + 1900 - 1) -
            (date.tm_wday + 6) % 7 + 10) /
           7;
  else if (week > 52 &&
           ((date.tm_yday - 365 - tm_isleapyear(date.tm_year + 1900) -
             (date.tm_wday + 6) % 7 + 10) /
            7) > 0)
    return (date.tm_yday - 365 - tm_isleapyear(date.tm_year + 1900) -
            (date.tm_wday + 6) % 7 + 10) /
           7;
  else
    return week;
}


x86 assembly (clang, -O0):
<tm_getisoweek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x6,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%eax
sub    %edx,%eax
add    $0xa,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a0 <tm_getisoweek+0x90>
mov    -0x18(%rbp),%eax
add    $0x16d,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x76c,%rdi
sub    $0x1,%rdi
call   1030 <tm_isleapyear@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    $0x6,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x24(%rbp),%eax
sub    %edx,%eax
add    $0xa,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    1247 <tm_getisoweek+0x137>
cmpl   $0x34,-0x1c(%rbp)
jle    1241 <tm_getisoweek+0x131>
mov    -0x18(%rbp),%eax
sub    $0x16d,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x76c,%rdi
call   1030 <tm_isleapyear@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x6,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x2c(%rbp),%eax
sub    %edx,%eax
add    $0xa,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
cmp    $0x0,%eax
jle    1241 <tm_getisoweek+0x131>
mov    -0x18(%rbp),%eax
sub    $0x16d,%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x76c,%rdi
call   1030 <tm_isleapyear@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
add    $0x6,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    -0x34(%rbp),%eax
sub    %edx,%eax
add    $0xa,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    1247 <tm_getisoweek+0x137>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tm_getisoweek>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rdi,%rax
shr    $0x20,%rax
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
lea    (%rax,%rcx,1),%r15d
add    $0x6,%r15d
mov    %ebp,%ebx
sub    %r15d,%ebx
lea    0x10(%rbx),%eax
cmp    $0xc,%eax
ja     1181 <tm_getisoweek+0x71>
add    $0x76b,%r14
mov    %r14,%rdi
call   1030 <tm_isleapyear@plt>
sub    %r15d,%ebp
lea    (%rax,%rbp,1),%ebx
add    $0x16d,%ebx
jmp    11ba <tm_getisoweek+0xaa>
cmp    $0x169,%ebx
jl     11ba <tm_getisoweek+0xaa>
add    $0xfffffe93,%ebp
add    $0x76c,%r14
mov    %r14,%rdi
call   1030 <tm_isleapyear@plt>
add    %r15d,%eax
mov    %ebp,%ecx
sub    %eax,%ecx
cmp    $0xfffffffd,%ecx
jl     11ba <tm_getisoweek+0xaa>
mov    %r14,%rdi
call   1030 <tm_isleapyear@plt>
add    %eax,%r15d
sub    %r15d,%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0xa,%eax
cltq
imul   $0xffffffff92492493,%rax,%rax
shr    $0x20,%rax
add    %ebx,%eax
add    $0xa,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x2,%eax
add    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i64 }
define i32 @tm_getisoweek(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.tm
%5 = alloca i32
%6 = bitcast %struct.tm* %4 to { i64, i64 }*
%7 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %6, i32 0, i32 0
store i64 %0, i64* %7
%8 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %6, i32 0, i32 1
store i64 %1, i64* %8
%9 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 1
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 6
%14 = srem i32 %13, 7
%15 = sub nsw i32 %10, %14
%16 = add nsw i32 %15, 10
%17 = sdiv i32 %16, 7
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %2
%21 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 365
%24 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 2
%25 = load i64, i64* %24
%26 = add nsw i64 %25, 1900
%27 = sub nsw i64 %26, 1
%28 = call i32 @tm_isleapyear(i64 %27)
%29 = add nsw i32 %23, %28
%30 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 6
%33 = srem i32 %32, 7
%34 = sub nsw i32 %29, %33
%35 = add nsw i32 %34, 10
%36 = sdiv i32 %35, 7
store i32 %36, i32* %3
br label %75
37: ; preds = %2
%38 = load i32, i32* %5
%39 = icmp sgt i32 %38, 52
br i1 %39, label %40, label %73
40: ; preds = %37
%41 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sub nsw i32 %42, 365
%44 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 2
%45 = load i64, i64* %44
%46 = add nsw i64 %45, 1900
%47 = call i32 @tm_isleapyear(i64 %46)
%48 = sub nsw i32 %43, %47
%49 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 1
%50 = load i32, i32* %49
%51 = add nsw i32 %50, 6
%52 = srem i32 %51, 7
%53 = sub nsw i32 %48, %52
%54 = add nsw i32 %53, 10
%55 = sdiv i32 %54, 7
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %40
%58 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 0
%59 = load i32, i32* %58
%60 = sub nsw i32 %59, 365
%61 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 2
%62 = load i64, i64* %61
%63 = add nsw i64 %62, 1900
%64 = call i32 @tm_isleapyear(i64 %63)
%65 = sub nsw i32 %60, %64
%66 = getelementptr inbounds %struct.tm, %struct.tm* %4, i32 0, i32 1
%67 = load i32, i32* %66
%68 = add nsw i32 %67, 6
%69 = srem i32 %68, 7
%70 = sub nsw i32 %65, %69
%71 = add nsw i32 %70, 10
%72 = sdiv i32 %71, 7
store i32 %72, i32* %3
br label %75
73: ; preds = %40, %37
%74 = load i32, i32* %5
store i32 %74, i32* %3
br label %75
75: ; preds = %73, %57, %20
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
define i32 @tm_getisoweek(i64 %0, i64 %1) {
%3 = trunc i64 %0 to i32
%4 = lshr i64 %0, 32
%5 = trunc i64 %4 to i32
%6 = add nsw i32 %5, 6
%7 = srem i32 %6, 7
%8 = sub nsw i32 %3, %7
%9 = add i32 %8, 16
%10 = icmp ult i32 %9, 13
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = add nsw i32 %3, 365
%13 = add nsw i64 %1, 1899
%14 = tail call i32 @tm_isleapyear(i64 %13)
%15 = sub i32 %12, %7
%16 = add i32 %15, %14
br label %30
17: ; preds = %2
%18 = icmp sgt i32 %8, 360
br i1 %18, label %19, label %30
19: ; preds = %17
%20 = add nsw i32 %3, -365
%21 = add nsw i64 %1, 1900
%22 = tail call i32 @tm_isleapyear(i64 %21)
%23 = add i32 %7, %22
%24 = sub i32 %20, %23
%25 = icmp sgt i32 %24, -4
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = tail call i32 @tm_isleapyear(i64 %21)
%28 = add i32 %7, %27
%29 = sub i32 %20, %28
br label %30
30: ; preds = %17, %19, %26, %11
%31 = phi i32 [ %16, %11 ], [ %29, %26 ], [ %8, %19 ], [ %8, %17 ]
%32 = add nsw i32 %31, 10
%33 = sdiv i32 %32, 7
ret i32 %33
}