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

/* Variables and functions */
int weeknumber(const struct tm *timeptr, int firstweekday) {
  if (firstweekday == 0)
    return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  else
    return (timeptr->tm_yday + 7 -
            (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) /
           7;
}


x86 assembly (clang, -O0):
<weeknumber>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1135 <weeknumber+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x7,%eax
mov    -0x10(%rbp),%rcx
sub    0x4(%rcx),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    1181 <weeknumber+0x81>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x7,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,0x4(%rax)
je     1161 <weeknumber+0x61>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <weeknumber+0x6e>
mov    $0x6,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <weeknumber+0x6e>
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    %ecx,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<weeknumber>:
mov    (%rdi),%eax
mov    0x4(%rdi),%ecx
add    $0x7,%eax
test   %esi,%esi
je     1121 <weeknumber+0x21>
mov    $0x1,%edx
sub    %ecx,%edx
test   %ecx,%ecx
mov    $0xfffffffa,%ecx
cmovne %edx,%ecx
add    %ecx,%eax
jmp    1123 <weeknumber+0x23>
sub    %ecx,%eax
cltq
imul   $0xffffffff92492493,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x2,%eax
add    %ecx,%eax
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32 }
define i32 @weeknumber(%struct.tm* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.tm*
%5 = alloca i32
store %struct.tm* %0, %struct.tm** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %2
%9 = load %struct.tm*, %struct.tm** %4
%10 = getelementptr inbounds %struct.tm, %struct.tm* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 7
%13 = load %struct.tm*, %struct.tm** %4
%14 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = sub nsw i32 %12, %15
%17 = sdiv i32 %16, 7
store i32 %17, i32* %3
br label %37
18: ; preds = %2
%19 = load %struct.tm*, %struct.tm** %4
%20 = getelementptr inbounds %struct.tm, %struct.tm* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 7
%23 = load %struct.tm*, %struct.tm** %4
%24 = getelementptr inbounds %struct.tm, %struct.tm* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load %struct.tm*, %struct.tm** %4
%29 = getelementptr inbounds %struct.tm, %struct.tm* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = sub nsw i32 %30, 1
br label %33
32: ; preds = %18
br label %33
33: ; preds = %32, %27
%34 = phi i32 [ %31, %27 ], [ 6, %32 ]
%35 = sub nsw i32 %22, %34
%36 = sdiv i32 %35, 7
store i32 %36, i32* %3
br label %37
37: ; preds = %33, %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.tm = type { i32, i32 }
define i32 @weeknumber(%struct.tm* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = add nsw i32 %5, 7
%7 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%8 = load i32, i32* %7
br i1 %3, label %9, label %11
9: ; preds = %2
%10 = sub i32 %6, %8
br label %16
11: ; preds = %2
%12 = icmp eq i32 %8, 0
%13 = sub i32 1, %8
%14 = select i1 %12, i32 -6, i32 %13
%15 = add i32 %6, %14
br label %16
16: ; preds = %11, %9
%17 = phi i32 [ %10, %9 ], [ %15, %11 ]
%18 = sdiv i32 %17, 7
ret i32 %18
}