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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, unsigned int);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strptime(char const *, char *, struct tm *);
int meetup_day_of_month(unsigned int year, unsigned int month, const char *week,
                        const char *day_of_week) {
  struct tm tm;
  char buf[100];
  sprintf(buf, "%d-%d-01", year, month);
  strptime(buf, "%Y-%m-%d", &tm);
  struct tm weekday_tm;
  strptime(day_of_week, "%A", &weekday_tm);
  int weekday = weekday_tm.tm_wday;
  int weekday_on_1st = tm.tm_wday;
  int day_of_month = 1;
  day_of_month += (weekday - weekday_on_1st);
  if (day_of_month < 1)
    day_of_month += 7;
  int multiplier;
  if (strcmp(week, "first") == 0) {
    multiplier = 1;
  } else {
    multiplier = 0;
  }
  day_of_month += 7 * (multiplier - 1);
  return day_of_month;
}


x86 assembly (clang, -O0):
<meetup_day_of_month>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xea3(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <strptime@plt>
mov    -0x18(%rbp),%rdi
lea    0xe8e(%rip),%rsi
lea    -0x98(%rbp),%rdx
call   1050 <strptime@plt>
mov    -0x98(%rbp),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0xa0(%rbp)
movl   $0x1,-0xa4(%rbp)
mov    -0x9c(%rbp),%eax
sub    -0xa0(%rbp),%eax
add    -0xa4(%rbp),%eax
mov    %eax,-0xa4(%rbp)
cmpl   $0x1,-0xa4(%rbp)
jge    11e3 <meetup_day_of_month+0xb3>
mov    -0xa4(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0xa4(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    120c <meetup_day_of_month+0xdc>
movl   $0x1,-0xa8(%rbp)
jmp    1216 <meetup_day_of_month+0xe6>
movl   $0x0,-0xa8(%rbp)
mov    -0xa8(%rbp),%eax
sub    $0x1,%eax
imul   $0x7,%eax,%eax
add    -0xa4(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<meetup_day_of_month>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%ecx
mov    %edi,%edx
lea    0xeb3(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <sprintf@plt>
lea    0xea8(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <strptime@plt>
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1050 <strptime@plt>
mov    (%rsp),%eax
sub    0x8(%rsp),%eax
lea    0x1(%rax),%ecx
lea    0x8(%rax),%ebx
test   %eax,%eax
cmovns %ecx,%ebx
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcmp@plt>
lea    -0x7(%rbx),%ecx
test   %rax,%rax
cmove  %ebx,%ecx
mov    %ecx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [9 x i8] c"%d-%d-01\00"
@.str.1 = [9 x i8] c"%Y-%m-%d\00"
@.str.2 = [3 x i8] c"%A\00"
@.str.3 = [6 x i8] c"first\00"
define i32 @meetup_day_of_month(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.tm
%10 = alloca [100 x i8]
%11 = alloca %struct.tm
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%21 = call i32 @strptime(i8* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %9)
%22 = load i8*, i8** %8
%23 = call i32 @strptime(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %11)
%24 = getelementptr inbounds %struct.tm, %struct.tm* %11, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %12
%26 = getelementptr inbounds %struct.tm, %struct.tm* %9, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %13
store i32 1, i32* %14
%28 = load i32, i32* %12
%29 = load i32, i32* %13
%30 = sub nsw i32 %28, %29
%31 = load i32, i32* %14
%32 = add nsw i32 %31, %30
store i32 %32, i32* %14
%33 = load i32, i32* %14
%34 = icmp slt i32 %33, 1
br i1 %34, label %35, label %38
35: ; preds = %4
%36 = load i32, i32* %14
%37 = add nsw i32 %36, 7
store i32 %37, i32* %14
br label %38
38: ; preds = %35, %4
%39 = load i8*, i8** %7
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 1, i32* %15
br label %44
43: ; preds = %38
store i32 0, i32* %15
br label %44
44: ; preds = %43, %42
%45 = load i32, i32* %15
%46 = sub nsw i32 %45, 1
%47 = mul nsw i32 7, %46
%48 = load i32, i32* %14
%49 = add nsw i32 %48, %47
store i32 %49, i32* %14
%50 = load i32, i32* %14
ret i32 %50
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [9 x i8] c"%d-%d-01\00"
@.str.1 = [9 x i8] c"%Y-%m-%d\00"
@.str.2 = [3 x i8] c"%A\00"
@.str.3 = [6 x i8] c"first\00"
define i32 @meetup_day_of_month(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca %struct.tm
%6 = alloca [100 x i8]
%7 = alloca %struct.tm
%8 = bitcast %struct.tm* %5 to i8*
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%11 = call i32 @strptime(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %5)
%12 = bitcast %struct.tm* %7 to i8*
%13 = call i32 @strptime(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %7)
%14 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = sub nsw i32 %15, %17
%19 = icmp slt i32 %18, 0
%20 = select i1 %19, i32 8, i32 1
%21 = add nsw i32 %20, %18
%22 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i32 0, i32 -7
%25 = add nsw i32 %21, %24
ret i32 %25
}