#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_year;
  int tm_mon;
  int tm_mday;
  int tm_wday;
};

/* Variables and functions */
int INT_MIN;
int /*<<< orphan*/ memset(struct tm *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mktime(struct tm *);
int first_wday(int year, int mon, int wday) {
  struct tm tm;
  if (wday == INT_MIN)
    return 1;
  memset(&tm, 0, sizeof(struct tm));
  tm.tm_year = year;
  tm.tm_mon = mon;
  tm.tm_mday = 1;
  mktime(&tm);
  return (1 + (wday - tm.tm_wday + 7) % 7);
}


x86 assembly (clang, -O0):
<first_wday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2ead(%rip),%rcx
cmp    (%rcx),%eax
jne    114f <first_wday+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    1192 <first_wday+0x72>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <mktime@plt>
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
add    $0x7,%eax
mov    $0x7,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<first_wday>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2eb7(%rip),%rcx
mov    $0x1,%eax
cmp    %edx,(%rcx)
je     11a1 <first_wday+0x81>
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    %ebp,0x8(%rsp)
mov    %r14d,0xc(%rsp)
movl   $0x1,0x10(%rsp)
mov    %r15,%rdi
call   1040 <mktime@plt>
sub    0x14(%rsp),%ebx
mov    %ebx,%eax
add    $0x7,%eax
cltq
imul   $0xffffffff92492493,%rax,%rax
shr    $0x20,%rax
add    %ebx,%eax
add    $0x7,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x2,%eax
add    %ecx,%eax
lea    0x0(,%rax,8),%ecx
sub    %ecx,%eax
add    %ebx,%eax
add    $0x8,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32 }
@INT_MIN = global i32 0
define i32 @first_wday(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.tm
%5 = bitcast %struct.tm* %4 to i8*
%6 = load i32, i32* @INT_MIN
%7 = icmp eq i32 %6, %2
br i1 %7, label %20, label %8
8: ; preds = %3
%9 = call i32 @memset(%struct.tm* %4, i32 0, i32 16)
%10 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
store i32 %0, i32* %10
%11 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
store i32 %1, i32* %11
%12 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
store i32 1, i32* %12
%13 = call i32 @mktime(%struct.tm* %4)
%14 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 3
%15 = load i32, i32* %14
%16 = add i32 %2, 7
%17 = sub i32 %16, %15
%18 = srem i32 %17, 7
%19 = add nsw i32 %18, 1
br label %20
20: ; preds = %3, %8
%21 = phi i32 [ %19, %8 ], [ 1, %3 ]
ret i32 %21
}