#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct ntptimeval {
  TYPE_1__ time;
};

/* Variables and functions */
int EINVAL;
int ntp_gettime(struct ntptimeval *);
int pegasus_gettimeofday(struct timeval *tv) {
  struct ntptimeval ntp;
  int err;
  if (tv == NULL)
    return (EINVAL);
  err = ntp_gettime(&ntp);
  tv->tv_sec = ntp.time.tv_sec;
  tv->tv_usec = ntp.time.tv_usec;
  return (err);
}


x86 assembly (clang, -O0):
<pegasus_gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1138 <pegasus_gettimeofday+0x28>
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    115d <pegasus_gettimeofday+0x4d>
lea    -0x18(%rbp),%rdi
call   1030 <ntp_gettime@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pegasus_gettimeofday>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1136 <pegasus_gettimeofday+0x26>
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1030 <ntp_gettime@plt>
mov    0xc(%rsp),%ecx
mov    %ecx,0x4(%rbx)
mov    0x8(%rsp),%ecx
mov    %ecx,(%rbx)
jmp    113f <pegasus_gettimeofday+0x2f>
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.ntptimeval = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@EINVAL = global i32 0
define i32 @pegasus_gettimeofday(%struct.timeval* %0) {
%2 = alloca i32
%3 = alloca %struct.timeval*
%4 = alloca %struct.ntptimeval
%5 = alloca i32
store %struct.timeval* %0, %struct.timeval** %3
%6 = load %struct.timeval*, %struct.timeval** %3
%7 = icmp eq %struct.timeval* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @EINVAL
store i32 %9, i32* %2
br label %23
10: ; preds = %1
%11 = call i32 @ntp_gettime(%struct.ntptimeval* %4)
store i32 %11, i32* %5
%12 = getelementptr inbounds %struct.ntptimeval, %struct.ntptimeval* %4, i32 0, i32 0
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load %struct.timeval*, %struct.timeval** %3
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.ntptimeval, %struct.ntptimeval* %4, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load %struct.timeval*, %struct.timeval** %3
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %23
23: ; preds = %10, %8
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.ntptimeval = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@EINVAL = global i32 0
define i32 @pegasus_gettimeofday(%struct.timeval* writeonly %0) {
%2 = alloca %struct.ntptimeval
%3 = bitcast %struct.ntptimeval* %2 to i8*
%4 = icmp eq %struct.timeval* %0, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = load i32, i32* @EINVAL
br label %15
7: ; preds = %1
%8 = call i32 @ntp_gettime(%struct.ntptimeval* %2)
%9 = getelementptr inbounds %struct.ntptimeval, %struct.ntptimeval* %2, i64 0, i32 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.ntptimeval, %struct.ntptimeval* %2, i64 0, i32 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
store i32 %13, i32* %14
br label %15
15: ; preds = %7, %5
%16 = phi i32 [ %6, %5 ], [ %8, %7 ]
ret i32 %16
}