#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 long time_t;
struct tm {
  int /*<<< orphan*/ tm_wday;
  scalar_t__ tm_sec;
  scalar_t__ tm_min;
  scalar_t__ tm_hour;
};

/* Variables and functions */
int /*<<< orphan*/ free(struct tm *);
long getRelativeSecondsOfDayofweek(int /*<<< orphan*/, int /*<<< orphan*/);
long getSeconds(struct tm *);
struct tm *localtime(long *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct tm *, struct tm *, int);
long mktime(struct tm *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ time(long *);
long int getRelativeIntervalOfWeek(struct tm *tp) {
  time_t current_time;
  struct tm *current, *midnight;
  time_t delayed_time = 0;
  time(&current_time);
  current = localtime(&current_time);
  if (current == NULL) {
    printf("ERROR; Getting local time fails\n");
    return 0;
  }
  midnight = (struct tm *)malloc(sizeof(struct tm));
  if (midnight == NULL) {
    printf("ERROR; Memory allocation fails\n");
    return 0;
  }
  memcpy(midnight, current, sizeof(struct tm));
  midnight->tm_hour = 0;
  midnight->tm_min = 0;
  midnight->tm_sec = 0;
  delayed_time = current_time - mktime(midnight);
  delayed_time = getRelativeSecondsOfDayofweek(current->tm_wday, tp->tm_wday) -
                 delayed_time;
  delayed_time = delayed_time + getSeconds(tp);
  free(midnight);
  return delayed_time;
}


x86 assembly (clang, -O0):
<getRelativeIntervalOfWeek>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1090 <time@plt>
lea    -0x18(%rbp),%rdi
call   1050 <localtime@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11de <getRelativeIntervalOfWeek+0x4e>
lea    0xe34(%rip),%rdi
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ae <getRelativeIntervalOfWeek+0x11e>
mov    $0x20,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1210 <getRelativeIntervalOfWeek+0x80>
lea    0xe23(%rip),%rdi
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ae <getRelativeIntervalOfWeek+0x11e>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x20,%edx
call   1080 <memcpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <mktime@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1030 <getRelativeSecondsOfDayofweek@plt>
sub    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getSeconds@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRelativeIntervalOfWeek>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <time@plt>
mov    %rbx,%rdi
call   1050 <localtime@plt>
test   %rax,%rax
je     122d <getRelativeIntervalOfWeek+0x9d>
mov    %rax,%r15
mov    $0x20,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1236 <getRelativeIntervalOfWeek+0xa6>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x20,%edx
call   1080 <memcpy@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%r12)
movq   $0x0,0x18(%r12)
mov    0x8(%rsp),%r13
mov    %r12,%rdi
call   10b0 <mktime@plt>
mov    %rax,%rbx
sub    %r13,%rbx
mov    (%r15),%edi
mov    (%r14),%esi
call   1030 <getRelativeSecondsOfDayofweek@plt>
mov    %rax,%r15
add    %rbx,%r15
mov    %r14,%rdi
call   1070 <getSeconds@plt>
mov    %rax,%rbx
add    %r15,%rbx
mov    %r12,%rdi
call   1040 <free@plt>
jmp    1244 <getRelativeIntervalOfWeek+0xb4>
lea    0xdeb(%rip),%rdi
jmp    123d <getRelativeIntervalOfWeek+0xad>
lea    0xdc3(%rip),%rdi
call   1060 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32, i64, i64, i64 }
@.str = [33 x i8] c"ERROR; Getting local time fails\0A\00"
@.str.1 = [32 x i8] c"ERROR; Memory allocation fails\0A\00"
define i64 @getRelativeIntervalOfWeek(%struct.tm* %0) {
%2 = alloca i64
%3 = alloca %struct.tm*
%4 = alloca i64
%5 = alloca %struct.tm*
%6 = alloca %struct.tm*
%7 = alloca i64
store %struct.tm* %0, %struct.tm** %3
store i64 0, i64* %7
%8 = call i32 @time(i64* %4)
%9 = call %struct.tm* @localtime(i64* %4)
store %struct.tm* %9, %struct.tm** %5
%10 = load %struct.tm*, %struct.tm** %5
%11 = icmp eq %struct.tm* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %2
br label %51
14: ; preds = %1
%15 = call i64 @malloc(i32 32)
%16 = inttoptr i64 %15 to %struct.tm*
store %struct.tm* %16, %struct.tm** %6
%17 = load %struct.tm*, %struct.tm** %6
%18 = icmp eq %struct.tm* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %2
br label %51
21: ; preds = %14
%22 = load %struct.tm*, %struct.tm** %6
%23 = load %struct.tm*, %struct.tm** %5
%24 = call i32 @memcpy(%struct.tm* %22, %struct.tm* %23, i32 32)
%25 = load %struct.tm*, %struct.tm** %6
%26 = getelementptr inbounds %struct.tm, %struct.tm* %25, i32 0, i32 3
store i64 0, i64* %26
%27 = load %struct.tm*, %struct.tm** %6
%28 = getelementptr inbounds %struct.tm, %struct.tm* %27, i32 0, i32 2
store i64 0, i64* %28
%29 = load %struct.tm*, %struct.tm** %6
%30 = getelementptr inbounds %struct.tm, %struct.tm* %29, i32 0, i32 1
store i64 0, i64* %30
%31 = load i64, i64* %4
%32 = load %struct.tm*, %struct.tm** %6
%33 = call i64 @mktime(%struct.tm* %32)
%34 = sub nsw i64 %31, %33
store i64 %34, i64* %7
%35 = load %struct.tm*, %struct.tm** %5
%36 = getelementptr inbounds %struct.tm, %struct.tm* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load %struct.tm*, %struct.tm** %3
%39 = getelementptr inbounds %struct.tm, %struct.tm* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @getRelativeSecondsOfDayofweek(i32 %37, i32 %40)
%42 = load i64, i64* %7
%43 = sub nsw i64 %41, %42
store i64 %43, i64* %7
%44 = load i64, i64* %7
%45 = load %struct.tm*, %struct.tm** %3
%46 = call i64 @getSeconds(%struct.tm* %45)
%47 = add nsw i64 %44, %46
store i64 %47, i64* %7
%48 = load %struct.tm*, %struct.tm** %6
%49 = call i32 @free(%struct.tm* %48)
%50 = load i64, i64* %7
store i64 %50, i64* %2
br label %51
51: ; preds = %21, %19, %12
%52 = load i64, i64* %2
ret i64 %52
}

LLVM IR (-O2):
%struct.tm = type { i32, i64, i64, i64 }
@str = [31 x i8] c"ERROR; Memory allocation fails\00"
@str.2 = [32 x i8] c"ERROR; Getting local time fails\00"
define i64 @getRelativeIntervalOfWeek(%struct.tm* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = call i32 @time(i64* %2)
%5 = call %struct.tm* @localtime(i64* %2)
%6 = icmp eq %struct.tm* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.2, i64 0, i64 0))
br label %31
9: ; preds = %1
%10 = call i64 @malloc(i32 32)
%11 = inttoptr i64 %10 to %struct.tm*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %31
15: ; preds = %9
%16 = call i32 @memcpy(%struct.tm* %11, %struct.tm* %5, i32 32)
%17 = getelementptr inbounds %struct.tm, %struct.tm* %11, i64 0, i32 1
%18 = bitcast i64* %17 to i8*
%19 = load i64, i64* %2
%20 = call i64 @mktime(%struct.tm* %11)
%21 = sub i64 %20, %19
%22 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.tm, %struct.tm* %0, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @getRelativeSecondsOfDayofweek(i32 %23, i32 %25)
%27 = add i64 %21, %26
%28 = call i64 @getSeconds(%struct.tm* %0)
%29 = add nsw i64 %27, %28
%30 = call i32 @free(%struct.tm* %11)
br label %31
31: ; preds = %15, %13, %7
%32 = phi i64 [ 0, %7 ], [ 0, %13 ], [ %29, %15 ]
ret i64 %32
}