#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 timeval {
  int dummy;
};
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ ETIMEDOUT;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, struct timespec *);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ puts(char *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int do_test(void) {
  sem_t s;
  struct timespec ts;
  struct timeval tv;
  if (sem_init(&s, 0, 1) == -1) {
    puts("sem_init failed");
    return 1;
  }
  if (TEMP_FAILURE_RETRY(sem_wait(&s)) == -1) {
    puts("sem_wait failed");
    return 1;
  }
  if (gettimeofday(&tv, NULL) != 0) {
    puts("gettimeofday failed");
    return 1;
  }
  TIMEVAL_TO_TIMESPEC(&tv, &ts);
  ts.tv_nsec += 500000000;
  if (ts.tv_nsec >= 1000000000) {
    ++ts.tv_sec;
    ts.tv_nsec -= 1000000000;
  }
  errno = 0;
  if (TEMP_FAILURE_RETRY(sem_timedwait(&s, &ts)) != -1) {
    puts("sem_timedwait succeeded");
    return 1;
  }
  if (errno != ETIMEDOUT) {
    printf("sem_timedwait return errno = %d instead of ETIMEDOUT\n", errno);
    return 1;
  }
  struct timespec ts2;
  if (clock_gettime(CLOCK_REALTIME, &ts2) != 0) {
    puts("clock_gettime failed");
    return 1;
  }
  if (ts2.tv_sec < ts.tv_sec ||
      (ts2.tv_sec == ts.tv_sec && ts2.tv_nsec < ts.tv_nsec)) {
    puts("timeout too short");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1090 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11c9 <do_test+0x39>
lea    0xe48(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
lea    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    11fa <do_test+0x6a>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <gettimeofday@plt>
cmp    $0x0,%rax
je     1229 <do_test+0x99>
lea    0xe08(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x18(%rbp),%eax
add    $0x1dcd6500,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x3b9aca00,-0x18(%rbp)
jl     1265 <do_test+0xd5>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x18(%rbp)
mov    0x2d74(%rip),%rax
movq   $0x0,(%rax)
lea    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <sem_timedwait@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     12a8 <do_test+0x118>
lea    0xd9d(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
mov    0x2d31(%rip),%rax
mov    (%rax),%rax
mov    0x2d1f(%rip),%rcx
cmp    (%rcx),%rax
je     12e4 <do_test+0x154>
mov    0x2d17(%rip),%rax
mov    (%rax),%rsi
lea    0xd79(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_gettime@plt>
cmp    $0x0,%rax
je     1318 <do_test+0x188>
lea    0xd7b(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jl     1340 <do_test+0x1b0>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1358 <do_test+0x1c8>
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1358 <do_test+0x1c8>
lea    0xd50(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    135f <do_test+0x1cf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x30,%rsp
lea    0x4(%rsp),%rdi
mov    $0x1,%ebx
xor    %esi,%esi
mov    $0x1,%edx
call   1090 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11e0 <do_test+0x50>
lea    0x4(%rsp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     11ee <do_test+0x5e>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1080 <gettimeofday@plt>
test   %rax,%rax
je     11f7 <do_test+0x67>
lea    0xe42(%rip),%rdi
jmp    125a <do_test+0xca>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
jmp    1264 <do_test+0xd4>
lea    0xe1b(%rip),%rdi
jmp    125a <do_test+0xca>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10b0 <TIMEVAL_TO_TIMESPEC@plt>
mov    0x10(%rsp),%eax
lea    0x1dcd6500(%rax),%ecx
mov    %ecx,0x10(%rsp)
cmp    $0x1dcd6500,%eax
jl     122a <do_test+0x9a>
addq   $0x1,0x18(%rsp)
add    $0xe2329b00,%eax
mov    %eax,0x10(%rsp)
mov    0x2daf(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x4(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1060 <sem_timedwait@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     126c <do_test+0xdc>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret
mov    (%rbx),%rsi
mov    0x2d62(%rip),%rax
cmp    (%rax),%rsi
jne    129c <do_test+0x10c>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0x20(%rsp),%rsi
call   1050 <clock_gettime@plt>
test   %rax,%rax
je     12aa <do_test+0x11a>
lea    0xde8(%rip),%rdi
jmp    125a <do_test+0xca>
lea    0xda9(%rip),%rdi
call   1070 <printf@plt>
jmp    125f <do_test+0xcf>
mov    0x28(%rsp),%rax
mov    0x18(%rsp),%rcx
lea    0xddc(%rip),%rdi
cmp    %rcx,%rax
jl     125a <do_test+0xca>
xor    %ebx,%ebx
cmp    %rcx,%rax
jne    1264 <do_test+0xd4>
mov    0x20(%rsp),%eax
cmp    0x10(%rsp),%eax
jl     125a <do_test+0xca>
jmp    1264 <do_test+0xd4>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
%struct.timeval = type { i32 }
@.str = [16 x i8] c"sem_init failed\00"
@.str.1 = [16 x i8] c"sem_wait failed\00"
@.str.2 = [20 x i8] c"gettimeofday failed\00"
@errno = global i64 0
@.str.3 = [24 x i8] c"sem_timedwait succeeded\00"
@ETIMEDOUT = global i64 0
@.str.4 = [54 x i8] c"sem_timedwait return errno = %d instead of ETIMEDOUT\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.5 = [21 x i8] c"clock_gettime failed\00"
@.str.6 = [18 x i8] c"timeout too short\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = alloca %struct.timespec
%6 = call i32 @sem_init(i32* %2, i32 0, i32 1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %76
10: ; preds = %0
%11 = call i32 @sem_wait(i32* %2)
%12 = call i32 @TEMP_FAILURE_RETRY(i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %76
16: ; preds = %10
%17 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %76
21: ; preds = %16
%22 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 500000000
store i32 %25, i32* %23
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = icmp sge i32 %27, 1000000000
br i1 %28, label %29, label %36
29: ; preds = %21
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%31 = load i64, i64* %30
%32 = add nsw i64 %31, 1
store i64 %32, i64* %30
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%34 = load i32, i32* %33
%35 = sub nsw i32 %34, 1000000000
store i32 %35, i32* %33
br label %36
36: ; preds = %29, %21
store i64 0, i64* @errno
%37 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %3)
%38 = call i32 @TEMP_FAILURE_RETRY(i32 %37)
%39 = icmp ne i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %76
42: ; preds = %36
%43 = load i64, i64* @errno
%44 = load i64, i64* @ETIMEDOUT
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i64, i64* @errno
%48 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %47)
store i32 1, i32* %1
br label %76
49: ; preds = %42
%50 = load i32, i32* @CLOCK_REALTIME
%51 = call i64 @clock_gettime(i32 %50, %struct.timespec* %5)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %76
55: ; preds = %49
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%57 = load i64, i64* %56
%58 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%59 = load i64, i64* %58
%60 = icmp slt i64 %57, %59
br i1 %60, label %73, label %61
61: ; preds = %55
%62 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%63 = load i64, i64* %62
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%65 = load i64, i64* %64
%66 = icmp eq i64 %63, %65
br i1 %66, label %67, label %75
67: ; preds = %61
%68 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%69 = load i32, i32* %68
%70 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%71 = load i32, i32* %70
%72 = icmp slt i32 %69, %71
br i1 %72, label %73, label %75
73: ; preds = %67, %55
%74 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %76
75: ; preds = %67, %61
store i32 0, i32* %1
br label %76
76: ; preds = %75, %73, %53, %46, %40, %19, %14, %8
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
%struct.timeval = type { i32 }
@.str = [16 x i8] c"sem_init failed\00"
@.str.1 = [16 x i8] c"sem_wait failed\00"
@.str.2 = [20 x i8] c"gettimeofday failed\00"
@errno = global i64 0
@.str.3 = [24 x i8] c"sem_timedwait succeeded\00"
@ETIMEDOUT = global i64 0
@.str.4 = [54 x i8] c"sem_timedwait return errno = %d instead of ETIMEDOUT\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.5 = [21 x i8] c"clock_gettime failed\00"
@.str.6 = [18 x i8] c"timeout too short\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = alloca %struct.timespec
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast %struct.timeval* %3 to i8*
%8 = call i32 @sem_init(i32* %1, i32 0, i32 1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %69
12: ; preds = %0
%13 = call i32 @sem_wait(i32* %1)
%14 = call i32 @TEMP_FAILURE_RETRY(i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %69
18: ; preds = %12
%19 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %69
23: ; preds = %18
%24 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 500000000
store i32 %27, i32* %25
%28 = icmp sgt i32 %26, 499999999
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%31 = load i64, i64* %30
%32 = add nsw i64 %31, 1
store i64 %32, i64* %30
%33 = add nsw i32 %26, -500000000
store i32 %33, i32* %25
br label %34
34: ; preds = %29, %23
store i64 0, i64* @errno
%35 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%36 = call i32 @TEMP_FAILURE_RETRY(i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %69
40: ; preds = %34
%41 = load i64, i64* @errno
%42 = load i64, i64* @ETIMEDOUT
%43 = icmp eq i64 %41, %42
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %41)
br label %69
46: ; preds = %40
%47 = bitcast %struct.timespec* %4 to i8*
%48 = load i32, i32* @CLOCK_REALTIME
%49 = call i64 @clock_gettime(i32 %48, %struct.timespec* %4)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %64
51: ; preds = %46
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%53 = load i64, i64* %52
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%55 = load i64, i64* %54
%56 = icmp slt i64 %53, %55
br i1 %56, label %64, label %57
57: ; preds = %51
%58 = icmp eq i64 %53, %55
br i1 %58, label %59, label %67
59: ; preds = %57
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%61 = load i32, i32* %60
%62 = load i32, i32* %25
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %51, %59, %46
%65 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), %46 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), %59 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), %51 ]
%66 = call i32 @puts(i8* dereferenceable(1) %65)
br label %67
67: ; preds = %64, %57, %59
%68 = phi i32 [ 0, %59 ], [ 0, %57 ], [ 1, %64 ]
br label %69
69: ; preds = %67, %44, %38, %21, %16, %10
%70 = phi i32 [ 1, %10 ], [ 1, %16 ], [ 1, %21 ], [ 1, %38 ], [ 1, %44 ], [ %68, %67 ]
ret i32 %70
}