#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 */

/* Variables and functions */
double fb_DateSerial(int, int, int);
int /*<<< orphan*/ fb_hGetBeginOfWeek(int *, int *, int *, int);
double fb_hGetFirstWeekOfYear(int year, int first_day_of_year,
                              int first_day_of_week) {
  int first_week_year, first_week_month, first_week_day;
  double serial_week_begin, serial_year_begin;
  int remaining_weekdays;
  if (first_day_of_year == 0) {
    first_day_of_year = 1;
  }
  serial_year_begin = fb_DateSerial(year, 1, 1);
  first_week_day = 1;
  first_week_month = 1;
  first_week_year = year;
  fb_hGetBeginOfWeek(&first_week_year, &first_week_month, &first_week_day,
                     first_day_of_week);
  serial_week_begin =
      fb_DateSerial(first_week_year, first_week_month, first_week_day);
  remaining_weekdays = (int)((serial_week_begin + 7.0l) - serial_year_begin);
  switch (first_day_of_year) {
  case 1:
    break;
  case 2:
    if (remaining_weekdays < 4) {
      serial_week_begin += 7.0l;
    }
    break;
  case 3:
    if (remaining_weekdays < 7) {
      serial_week_begin += 7.0l;
    }
    break;
  }
  return serial_week_begin;
}


x86 assembly (clang, -O0):
<fb_hGetFirstWeekOfYear>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1142 <fb_hGetFirstWeekOfYear+0x22>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fb_DateSerial@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%ecx
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fb_hGetBeginOfWeek@plt>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <fb_DateSerial@plt>
movsd  %xmm0,-0x20(%rbp)
fldl   -0x20(%rbp)
flds   0xe66(%rip)
faddp  %st,%st(1)
fldl   -0x28(%rbp)
fsubrp %st,%st(1)
fnstcw -0x42(%rbp)
movzwl -0x42(%rbp),%eax
or     $0xc00,%eax
mov    %ax,-0x44(%rbp)
fldcw  -0x44(%rbp)
fistpl -0x30(%rbp)
fldcw  -0x42(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
sub    $0x1,%eax
je     11f6 <fb_hGetFirstWeekOfYear+0xd6>
jmp    11d4 <fb_hGetFirstWeekOfYear+0xb4>
mov    -0x48(%rbp),%eax
sub    $0x2,%eax
je     11fb <fb_hGetFirstWeekOfYear+0xdb>
jmp    11e5 <fb_hGetFirstWeekOfYear+0xc5>
mov    -0x48(%rbp),%eax
sub    $0x3,%eax
je     1222 <fb_hGetFirstWeekOfYear+0x102>
jmp    1249 <fb_hGetFirstWeekOfYear+0x129>
jmp    1249 <fb_hGetFirstWeekOfYear+0x129>
cmpl   $0x4,-0x2c(%rbp)
jge    121d <fb_hGetFirstWeekOfYear+0xfd>
fldl   -0x20(%rbp)
flds   0xdf2(%rip)
faddp  %st,%st(1)
fstpl  -0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1249 <fb_hGetFirstWeekOfYear+0x129>
cmpl   $0x7,-0x2c(%rbp)
jge    1244 <fb_hGetFirstWeekOfYear+0x124>
fldl   -0x20(%rbp)
flds   0xdcb(%rip)
faddp  %st,%st(1)
fstpl  -0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1249 <fb_hGetFirstWeekOfYear+0x129>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_hGetFirstWeekOfYear>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
test   %esi,%esi
mov    $0x1,%eax
cmove  %eax,%ebx
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <fb_DateSerial@plt>
movsd  %xmm0,0x18(%rsp)
movl   $0x1,0x8(%rsp)
movl   $0x1,0xc(%rsp)
mov    %ebp,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%ecx
call   1040 <fb_hGetBeginOfWeek@plt>
mov    0x10(%rsp),%edi
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
call   1030 <fb_DateSerial@plt>
movsd  %xmm0,0x30(%rsp)
fldl   0x30(%rsp)
fadds  0xe66(%rip)
movsd  0x18(%rsp),%xmm1
movsd  %xmm1,0x38(%rsp)
fld    %st(0)
fsubl  0x38(%rsp)
fnstcw 0x4(%rsp)
movzwl 0x4(%rsp),%eax
or     $0xc00,%eax
mov    %ax,0x6(%rsp)
fldcw  0x6(%rsp)
fistpl 0x14(%rsp)
fldcw  0x4(%rsp)
mov    0x14(%rsp),%eax
cmp    $0x3,%ebx
je     11ea <fb_hGetFirstWeekOfYear+0xca>
cmp    $0x2,%ebx
jne    11fb <fb_hGetFirstWeekOfYear+0xdb>
fstpl  0x28(%rsp)
cmp    $0x4,%eax
jge    11fd <fb_hGetFirstWeekOfYear+0xdd>
movsd  0x28(%rsp),%xmm0
jmp    11fd <fb_hGetFirstWeekOfYear+0xdd>
fstpl  0x20(%rsp)
cmp    $0x7,%eax
jge    11fd <fb_hGetFirstWeekOfYear+0xdd>
movsd  0x20(%rsp),%xmm0
jmp    11fd <fb_hGetFirstWeekOfYear+0xdd>
fstp   %st(0)
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @fb_hGetFirstWeekOfYear(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 1, i32* %5
br label %16
16: ; preds = %15, %3
%17 = load i32, i32* %4
%18 = call double @fb_DateSerial(i32 %17, i32 1, i32 1)
store double %18, double* %11
store i32 1, i32* %9
store i32 1, i32* %8
%19 = load i32, i32* %4
store i32 %19, i32* %7
%20 = load i32, i32* %6
%21 = call i32 @fb_hGetBeginOfWeek(i32* %7, i32* %8, i32* %9, i32 %20)
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = call double @fb_DateSerial(i32 %22, i32 %23, i32 %24)
store double %25, double* %10
%26 = load double, double* %10
%27 = fpext double %26 to x86_fp80
%28 = fadd x86_fp80 %27, 0xK4001E000000000000000
%29 = load double, double* %11
%30 = fpext double %29 to x86_fp80
%31 = fsub x86_fp80 %28, %30
%32 = fptosi x86_fp80 %31 to i32
store i32 %32, i32* %12
%33 = load i32, i32* %5
switch i32 %33, label %53 [
i32 1, label %34
i32 2, label %35
i32 3, label %44
]
34: ; preds = %16
br label %53
35: ; preds = %16
%36 = load i32, i32* %12
%37 = icmp slt i32 %36, 4
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load double, double* %10
%40 = fpext double %39 to x86_fp80
%41 = fadd x86_fp80 %40, 0xK4001E000000000000000
%42 = fptrunc x86_fp80 %41 to double
store double %42, double* %10
br label %43
43: ; preds = %38, %35
br label %53
44: ; preds = %16
%45 = load i32, i32* %12
%46 = icmp slt i32 %45, 7
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load double, double* %10
%49 = fpext double %48 to x86_fp80
%50 = fadd x86_fp80 %49, 0xK4001E000000000000000
%51 = fptrunc x86_fp80 %50 to double
store double %51, double* %10
br label %52
52: ; preds = %47, %44
br label %53
53: ; preds = %16, %52, %43, %34
%54 = load double, double* %10
ret double %54
}

LLVM IR (-O2):
define double @fb_hGetFirstWeekOfYear(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = icmp eq i32 %1, 0
%11 = select i1 %10, i32 1, i32 %1
%12 = tail call double @fb_DateSerial(i32 %0, i32 1, i32 1)
store i32 1, i32* %6
store i32 1, i32* %5
store i32 %0, i32* %4
%13 = call i32 @fb_hGetBeginOfWeek(i32* %4, i32* %5, i32* %6, i32 %2)
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call double @fb_DateSerial(i32 %14, i32 %15, i32 %16)
%18 = fpext double %17 to x86_fp80
%19 = fadd x86_fp80 %18, 0xK4001E000000000000000
%20 = fpext double %12 to x86_fp80
%21 = fsub x86_fp80 %19, %20
%22 = fptosi x86_fp80 %21 to i32
switch i32 %11, label %31 [
i32 3, label %27
i32 2, label %23
]
23: ; preds = %3
%24 = icmp slt i32 %22, 4
%25 = fptrunc x86_fp80 %19 to double
%26 = select i1 %24, double %25, double %17
br label %31
27: ; preds = %3
%28 = icmp slt i32 %22, 7
%29 = fptrunc x86_fp80 %19 to double
%30 = select i1 %28, double %29, double %17
br label %31
31: ; preds = %27, %23, %3
%32 = phi double [ %17, %3 ], [ %26, %23 ], [ %30, %27 ]
ret double %32
}