#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 scalar_t__ time_t;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct tcp {
  int dummy;
};

/* Variables and functions */
int tcp_internal_sleep(struct tcp *, struct timeval *, int, int);
scalar_t__ time(int /*<<< orphan*/);
int tcp_sleep(struct tcp *tcp, time_t stoptime, int reading, int writing) {
  int timeout;
  struct timeval tm, *tptr;
  if (stoptime == 0) {
    tptr = 0;
  } else {
    timeout = stoptime - time(0);
    if (timeout < 1)
      timeout = 1;
    tm.tv_sec = timeout;
    tm.tv_usec = 0;
    tptr = &tm;
  }
  return tcp_internal_sleep(tcp, tptr, reading, writing);
}


x86 assembly (clang, -O0):
<tcp_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114e <tcp_sleep+0x2e>
movq   $0x0,-0x38(%rbp)
jmp    1191 <tcp_sleep+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %edi,%edi
call   1030 <time@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    117b <tcp_sleep+0x5b>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1040 <tcp_internal_sleep@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_sleep>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rdi,%rbx
test   %rsi,%rsi
je     1162 <tcp_sleep+0x42>
mov    %rsi,%rbp
xor    %edi,%edi
call   1030 <time@plt>
sub    %eax,%ebp
test   %ebp,%ebp
mov    $0x1,%eax
cmovg  %ebp,%eax
mov    %eax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
jmp    1164 <tcp_sleep+0x44>
xor    %esi,%esi
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1040 <tcp_internal_sleep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tcp = type { i32 }
%struct.timeval = type { i32, i64 }
define i32 @tcp_sleep(%struct.tcp* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca %struct.tcp*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval*
store %struct.tcp* %0, %struct.tcp** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store %struct.timeval* null, %struct.timeval** %11
br label %27
15: ; preds = %4
%16 = load i64, i64* %6
%17 = call i64 @time(i32 0)
%18 = sub nsw i64 %16, %17
%19 = trunc i64 %18 to i32
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 1, i32* %9
br label %23
23: ; preds = %22, %15
%24 = load i32, i32* %9
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %26
store %struct.timeval* %10, %struct.timeval** %11
br label %27
27: ; preds = %23, %14
%28 = load %struct.tcp*, %struct.tcp** %5
%29 = load %struct.timeval*, %struct.timeval** %11
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = call i32 @tcp_internal_sleep(%struct.tcp* %28, %struct.timeval* %29, i32 %30, i32 %31)
ret i32 %32
}

LLVM IR (-O2):
%struct.tcp = type { i32 }
%struct.timeval = type { i32, i64 }
define i32 @tcp_sleep(%struct.tcp* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %5 to i8*
%7 = icmp eq i64 %1, 0
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = tail call i64 @time(i32 0)
%10 = sub nsw i64 %1, %9
%11 = trunc i64 %10 to i32
%12 = icmp sgt i32 %11, 1
%13 = select i1 %12, i32 %11, i32 1
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %15
br label %16
16: ; preds = %4, %8
%17 = phi %struct.timeval* [ %5, %8 ], [ null, %4 ]
%18 = call i32 @tcp_internal_sleep(%struct.tcp* %0, %struct.timeval* %17, i32 %2, i32 %3)
ret i32 %18
}