#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 timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ t2p_normalize_timespec(struct timespec *);
int /*<<< orphan*/ t2p_time2posix(int /*<<< orphan*/, int *);
struct timespec *t2p_time2posix_timespec(struct timespec *ts) {
  int state;
  ts->tv_sec = t2p_time2posix(ts->tv_sec, &state);
  if (state == 1)
    ts->tv_nsec >>= 1;
  else if (state == 2)
    ts->tv_nsec = (ts->tv_nsec + 1000000000) >> 1;
  else if (state == -1) {
    ts->tv_nsec <<= 1;
    t2p_normalize_timespec(ts);
  }
  return ts;
}


x86 assembly (clang, -O0):
<t2p_time2posix_timespec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0xc(%rbp),%rsi
call   1030 <t2p_time2posix@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x1,-0xc(%rbp)
jne    115f <t2p_time2posix_timespec+0x3f>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sar    $0x1,%ecx
mov    %ecx,(%rax)
jmp    11ab <t2p_time2posix_timespec+0x8b>
cmpl   $0x2,-0xc(%rbp)
jne    1183 <t2p_time2posix_timespec+0x63>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x3b9aca00,%ecx
sar    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    11a6 <t2p_time2posix_timespec+0x86>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a1 <t2p_time2posix_timespec+0x81>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <t2p_normalize_timespec@plt>
jmp    11a6 <t2p_time2posix_timespec+0x86>
jmp    11ab <t2p_time2posix_timespec+0x8b>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<t2p_time2posix_timespec>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
lea    0xc(%rsp),%rsi
call   1030 <t2p_time2posix@plt>
mov    %eax,0x4(%rbx)
mov    0xc(%rsp),%eax
cmp    $0xffffffff,%eax
je     114f <t2p_time2posix_timespec+0x2f>
cmp    $0x2,%eax
je     115b <t2p_time2posix_timespec+0x3b>
cmp    $0x1,%eax
jne    1166 <t2p_time2posix_timespec+0x46>
sarl   (%rbx)
jmp    1166 <t2p_time2posix_timespec+0x46>
shll   (%rbx)
mov    %rbx,%rdi
call   1040 <t2p_normalize_timespec@plt>
jmp    1166 <t2p_time2posix_timespec+0x46>
mov    $0x3b9aca00,%eax
add    (%rbx),%eax
sar    %eax
mov    %eax,(%rbx)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define %struct.timespec* @t2p_time2posix_timespec(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca i32
store %struct.timespec* %0, %struct.timespec** %2
%4 = load %struct.timespec*, %struct.timespec** %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%6 = load i32, i32* %5
%7 = call i32 @t2p_time2posix(i32 %6, i32* %3)
%8 = load %struct.timespec*, %struct.timespec** %2
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 %7, i32* %9
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load %struct.timespec*, %struct.timespec** %2
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = ashr i32 %15, 1
store i32 %16, i32* %14
br label %40
17: ; preds = %1
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = load %struct.timespec*, %struct.timespec** %2
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1000000000
%25 = ashr i32 %24, 1
%26 = load %struct.timespec*, %struct.timespec** %2
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 0
store i32 %25, i32* %27
br label %39
28: ; preds = %17
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load %struct.timespec*, %struct.timespec** %2
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = shl i32 %34, 1
store i32 %35, i32* %33
%36 = load %struct.timespec*, %struct.timespec** %2
%37 = call i32 @t2p_normalize_timespec(%struct.timespec* %36)
br label %38
38: ; preds = %31, %28
br label %39
39: ; preds = %38, %20
br label %40
40: ; preds = %39, %12
%41 = load %struct.timespec*, %struct.timespec** %2
ret %struct.timespec* %41
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define %struct.timespec* @t2p_time2posix_timespec(%struct.timespec* returned %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = call i32 @t2p_time2posix(i32 %5, i32* %2)
store i32 %6, i32* %4
%7 = load i32, i32* %2
switch i32 %7, label %22 [
i32 1, label %8
i32 2, label %12
i32 -1, label %17
]
8: ; preds = %1
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = ashr i32 %10, 1
store i32 %11, i32* %9
br label %22
12: ; preds = %1
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1000000000
%16 = ashr i32 %15, 1
store i32 %16, i32* %13
br label %22
17: ; preds = %1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%19 = load i32, i32* %18
%20 = shl i32 %19, 1
store i32 %20, i32* %18
%21 = call i32 @t2p_normalize_timespec(%struct.timespec* %0)
br label %22
22: ; preds = %1, %12, %17, %8
ret %struct.timespec* %0
}