#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_year;
  int tm_yday;
};

/* Variables and functions */
int days_in_thisyear(int);
int isoweeknumber(const struct tm *timeptr, int *year_ptr) {
  int weeknum;
  int weekday = timeptr->tm_wday;
  int nearest_thursday;
  int year = timeptr->tm_year + 1900;
  int yday = timeptr->tm_yday;
  if (weekday == 0)
    weekday = 7;
  if (weekday <= 4) {
    nearest_thursday = yday + (4 - weekday);
    if (nearest_thursday >= days_in_thisyear(year)) {
      nearest_thursday -= days_in_thisyear(year);
      year++;
    }
  } else {
    nearest_thursday = yday - (weekday - 4);
    if (nearest_thursday < 0) {
      year--;
      nearest_thursday += days_in_thisyear(year);
    }
  }
  weeknum = 1 + (nearest_thursday / 7);
  *year_ptr = year;
  return weeknum;
}


x86 assembly (clang, -O0):
<isoweeknumber>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x76c,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1153 <isoweeknumber+0x43>
movl   $0x7,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jg     11a8 <isoweeknumber+0x98>
mov    -0x24(%rbp),%eax
mov    $0x4,%ecx
sub    -0x18(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <days_in_thisyear@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jl     11a3 <isoweeknumber+0x93>
mov    -0x20(%rbp),%edi
call   1030 <days_in_thisyear@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11dc <isoweeknumber+0xcc>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x4,%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d7 <isoweeknumber+0xc7>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <days_in_thisyear@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11dc <isoweeknumber+0xcc>
mov    -0x1c(%rbp),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isoweeknumber>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    (%rdi),%eax
mov    0x4(%rdi),%ebx
lea    0x76c(%rbx),%r15d
test   %eax,%eax
mov    $0x7,%ecx
cmovne %eax,%ecx
mov    0x8(%rdi),%ebp
sub    %ecx,%ebp
add    $0x4,%ebp
cmp    $0x4,%ecx
jg     115b <isoweeknumber+0x4b>
mov    %r15d,%edi
call   1030 <days_in_thisyear@plt>
cmp    %eax,%ebp
jl     1171 <isoweeknumber+0x61>
mov    %r15d,%edi
call   1030 <days_in_thisyear@plt>
sub    %eax,%ebp
add    $0x76d,%ebx
jmp    116e <isoweeknumber+0x5e>
test   %ebp,%ebp
jns    1171 <isoweeknumber+0x61>
add    $0x76b,%ebx
mov    %ebx,%edi
call   1030 <days_in_thisyear@plt>
add    %eax,%ebp
mov    %ebx,%r15d
movslq %ebp,%rax
imul   $0xffffffff92492493,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
sar    $0x2,%ecx
shr    $0x1f,%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %r15d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
define i32 @isoweeknumber(%struct.tm* nocapture %0, i32* nocapture writeonly %1) {
%3 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%6 = load i32, i32* %5
%7 = add nsw i32 %6, 1900
%8 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%9 = load i32, i32* %8
%10 = icmp eq i32 %4, 0
%11 = select i1 %10, i32 7, i32 %4
%12 = icmp slt i32 %11, 5
%13 = sub nsw i32 4, %11
%14 = add i32 %13, %9
br i1 %12, label %15, label %22
15: ; preds = %2
%16 = tail call i32 @days_in_thisyear(i32 %7)
%17 = icmp slt i32 %14, %16
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = tail call i32 @days_in_thisyear(i32 %7)
%20 = sub nsw i32 %14, %19
%21 = add nsw i32 %6, 1901
br label %28
22: ; preds = %2
%23 = icmp slt i32 %14, 0
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = add nsw i32 %6, 1899
%26 = tail call i32 @days_in_thisyear(i32 %25)
%27 = add nsw i32 %26, %14
br label %28
28: ; preds = %22, %24, %15, %18
%29 = phi i32 [ %20, %18 ], [ %14, %15 ], [ %27, %24 ], [ %14, %22 ]
%30 = phi i32 [ %21, %18 ], [ %7, %15 ], [ %25, %24 ], [ %7, %22 ]
%31 = sdiv i32 %29, 7
%32 = add nsw i32 %31, 1
store i32 %30, i32* %1
ret i32 %32
}