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

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


x86 assembly (clang, -O0):
<weeknumber>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1142 <weeknumber+0x42>
cmpl   $0x0,-0x10(%rbp)
jne    1134 <weeknumber+0x34>
movl   $0x6,-0x10(%rbp)
jmp    113d <weeknumber+0x3d>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1142 <weeknumber+0x42>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x7,%eax
sub    -0x10(%rbp),%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    116b <weeknumber+0x6b>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<weeknumber>:
mov    (%rdi),%eax
mov    %eax,%ecx
sub    $0x1,%ecx
mov    $0x6,%edx
cmovae %ecx,%edx
mov    0x4(%rdi),%ecx
cmp    $0x1,%esi
cmovne %eax,%edx
sub    %edx,%ecx
mov    %ecx,%eax
add    $0x7,%eax
cltq
imul   $0xffffffff92492493,%rax,%rax
shr    $0x20,%rax
add    %ecx,%eax
add    $0x7,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x2,%eax
xor    %edx,%edx
add    %ecx,%eax
cmovs  %edx,%eax
ret

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

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