#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 */
typedef int uint16_t;
struct tm {
  int tm_mday;
  int tm_mon;
  int tm_year;
};

/* Variables and functions */
int htole16(int);
uint16_t tm_struct_to_fat_date(struct tm *time) {
  uint16_t day = time->tm_mday < 1    ? 1
                 : time->tm_mday > 31 ? 31
                                      : time->tm_mday;
  uint16_t month = time->tm_mon + 1;
  month = month < 1 ? 1 : month > 12 ? 12 : month;
  uint16_t year = time->tm_year < 80 ? 0 : time->tm_year - 80;
  return htole16((year << 9) + (month << 5) + day);
}


x86 assembly (clang, -O0):
<tm_struct_to_fat_date>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1136 <tm_struct_to_fat_date+0x26>
mov    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115f <tm_struct_to_fat_date+0x4f>
mov    -0x8(%rbp),%rax
cmpl   $0x1f,(%rax)
jle    1150 <tm_struct_to_fat_date+0x40>
mov    $0x1f,%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <tm_struct_to_fat_date+0x49>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jge    1189 <tm_struct_to_fat_date+0x79>
mov    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ac <tm_struct_to_fat_date+0x9c>
cmpl   $0xc,-0x10(%rbp)
jle    11a0 <tm_struct_to_fat_date+0x90>
mov    $0xc,%eax
mov    %eax,-0x24(%rbp)
jmp    11a6 <tm_struct_to_fat_date+0x96>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x50,0x8(%rax)
jge    11ca <tm_struct_to_fat_date+0xba>
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
jmp    11d7 <tm_struct_to_fat_date+0xc7>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
sub    $0x50,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
shl    $0x9,%edi
mov    -0x10(%rbp),%eax
shl    $0x5,%eax
add    %eax,%edi
add    -0xc(%rbp),%edi
call   1030 <htole16@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tm_struct_to_fat_date>:
mov    (%rdi),%eax
mov    0x4(%rdi),%ecx
cmp    $0x1f,%eax
mov    $0x1f,%edx
cmovl  %eax,%edx
test   %edx,%edx
mov    $0x1,%eax
cmovg  %edx,%eax
cmp    $0xb,%ecx
mov    $0xb,%edx
cmovl  %ecx,%edx
mov    0x8(%rdi),%ecx
mov    %ecx,%esi
shl    $0x9,%esi
add    $0xffff6000,%esi
xor    %edi,%edi
cmp    $0x50,%ecx
cmovl  %edi,%esi
test   %edx,%edx
cmovs  %edi,%edx
shl    $0x5,%edx
add    %eax,%esi
lea    (%rdx,%rsi,1),%edi
add    $0x20,%edi
jmp    1030 <htole16@plt>

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32 }
define i32 @tm_struct_to_fat_date(%struct.tm* %0) {
%2 = alloca %struct.tm*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store %struct.tm* %0, %struct.tm** %2
%6 = load %struct.tm*, %struct.tm** %2
%7 = getelementptr inbounds %struct.tm, %struct.tm* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
br label %23
11: ; preds = %1
%12 = load %struct.tm*, %struct.tm** %2
%13 = getelementptr inbounds %struct.tm, %struct.tm* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, 31
br i1 %15, label %16, label %17
16: ; preds = %11
br label %21
17: ; preds = %11
%18 = load %struct.tm*, %struct.tm** %2
%19 = getelementptr inbounds %struct.tm, %struct.tm* %18, i32 0, i32 0
%20 = load i32, i32* %19
br label %21
21: ; preds = %17, %16
%22 = phi i32 [ 31, %16 ], [ %20, %17 ]
br label %23
23: ; preds = %21, %10
%24 = phi i32 [ 1, %10 ], [ %22, %21 ]
store i32 %24, i32* %3
%25 = load %struct.tm*, %struct.tm** %2
%26 = getelementptr inbounds %struct.tm, %struct.tm* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %23
br label %40
32: ; preds = %23
%33 = load i32, i32* %4
%34 = icmp sgt i32 %33, 12
br i1 %34, label %35, label %36
35: ; preds = %32
br label %38
36: ; preds = %32
%37 = load i32, i32* %4
br label %38
38: ; preds = %36, %35
%39 = phi i32 [ 12, %35 ], [ %37, %36 ]
br label %40
40: ; preds = %38, %31
%41 = phi i32 [ 1, %31 ], [ %39, %38 ]
store i32 %41, i32* %4
%42 = load %struct.tm*, %struct.tm** %2
%43 = getelementptr inbounds %struct.tm, %struct.tm* %42, i32 0, i32 2
%44 = load i32, i32* %43
%45 = icmp slt i32 %44, 80
br i1 %45, label %46, label %47
46: ; preds = %40
br label %52
47: ; preds = %40
%48 = load %struct.tm*, %struct.tm** %2
%49 = getelementptr inbounds %struct.tm, %struct.tm* %48, i32 0, i32 2
%50 = load i32, i32* %49
%51 = sub nsw i32 %50, 80
br label %52
52: ; preds = %47, %46
%53 = phi i32 [ 0, %46 ], [ %51, %47 ]
store i32 %53, i32* %5
%54 = load i32, i32* %5
%55 = shl i32 %54, 9
%56 = load i32, i32* %4
%57 = shl i32 %56, 5
%58 = add nsw i32 %55, %57
%59 = load i32, i32* %3
%60 = add nsw i32 %58, %59
%61 = call i32 @htole16(i32 %60)
ret i32 %61
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
define i32 @tm_struct_to_fat_date(%struct.tm* nocapture %0) {
%2 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = icmp slt i32 %3, 31
%5 = select i1 %4, i32 %3, i32 31
%6 = icmp sgt i32 %5, 1
%7 = select i1 %6, i32 %5, i32 1
%8 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = icmp slt i32 %9, 11
%11 = select i1 %10, i32 %9, i32 11
%12 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 2
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, 80
%15 = shl i32 %13, 9
%16 = add i32 %15, -40960
%17 = select i1 %14, i32 0, i32 %16
%18 = icmp sgt i32 %11, 0
%19 = select i1 %18, i32 %11, i32 0
%20 = shl nuw nsw i32 %19, 5
%21 = add nuw nsw i32 %7, 32
%22 = add i32 %21, %17
%23 = add i32 %22, %20
%24 = tail call i32 @htole16(i32 %23)
ret i32 %24
}