#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;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ sem_t;

/* Variables and functions */
scalar_t__ ETIMEDOUT;
int TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ TIMEVAL_TO_TIMESPEC(struct timeval *, 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;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11b9 <do_test+0x39>
lea    0xe58(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <do_test+0x158>
lea    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    11ea <do_test+0x6a>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <do_test+0x158>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
cmp    $0x0,%rax
je     1219 <do_test+0x99>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <do_test+0x158>
lea    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    -0x10(%rbp),%eax
add    $0x1dcd6500,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x3b9aca00,-0x10(%rbp)
jl     1252 <do_test+0xd2>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x10(%rbp)
mov    0x2d87(%rip),%rax
movq   $0x0,(%rax)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <sem_timedwait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1295 <do_test+0x115>
lea    0xdb0(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <do_test+0x158>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
je     12d1 <do_test+0x151>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rsi
lea    0xd8c(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <do_test+0x158>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x20,%rsp
lea    0x14(%rsp),%rdi
mov    $0x1,%ebx
xor    %esi,%esi
mov    $0x1,%edx
call   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11d8 <do_test+0x58>
lea    0x14(%rsp),%rdi
call   1030 <sem_wait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     11e9 <do_test+0x69>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
test   %rax,%rax
je     11fa <do_test+0x7a>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
jmp    1280 <do_test+0x100>
lea    0xe21(%rip),%rdi
call   1040 <puts@plt>
jmp    1285 <do_test+0x105>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
jmp    1280 <do_test+0x100>
lea    0x18(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10a0 <TIMEVAL_TO_TIMESPEC@plt>
mov    0x8(%rsp),%eax
lea    0x1dcd6500(%rax),%ecx
mov    %ecx,0x8(%rsp)
cmp    $0x1dcd6500,%eax
jl     122c <do_test+0xac>
addl   $0x1,0xc(%rsp)
add    $0xe2329b00,%eax
mov    %eax,0x8(%rsp)
mov    0x2dad(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x14(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <sem_timedwait@plt>
mov    %eax,%edi
call   1090 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1263 <do_test+0xe3>
lea    0xdd8(%rip),%rdi
call   1040 <puts@plt>
jmp    1280 <do_test+0x100>
mov    (%rbx),%rsi
mov    0x2d6b(%rip),%rax
xor    %ebx,%ebx
cmp    (%rax),%rsi
je     1285 <do_test+0x105>
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%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"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = call i32 @sem_init(i32* %2, i32 0, i32 1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %49
9: ; preds = %0
%10 = call i32 @sem_wait(i32* %2)
%11 = call i32 @TEMP_FAILURE_RETRY(i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = 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 %49
15: ; preds = %9
%16 = call i64 @gettimeofday(%struct.timeval* %4, i32* null)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = 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 %49
20: ; preds = %15
%21 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %4, %struct.timespec* %3)
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 500000000
store i32 %24, i32* %22
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp sge i32 %26, 1000000000
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
store i32 %31, i32* %29
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sub nsw i32 %33, 1000000000
store i32 %34, i32* %32
br label %35
35: ; preds = %28, %20
store i64 0, i64* @errno
%36 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %3)
%37 = call i32 @TEMP_FAILURE_RETRY(i32 %36)
%38 = icmp ne i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = 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 %49
41: ; preds = %35
%42 = load i64, i64* @errno
%43 = load i64, i64* @ETIMEDOUT
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i64, i64* @errno
%47 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %46)
store i32 1, i32* %1
br label %49
48: ; preds = %41
store i32 0, i32* %1
br label %49
49: ; preds = %48, %45, %39, %18, %13, %7
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%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"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = call i32 @sem_init(i32* %1, i32 0, i32 1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %45
11: ; preds = %0
%12 = call i32 @sem_wait(i32* %1)
%13 = call i32 @TEMP_FAILURE_RETRY(i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %45
17: ; preds = %11
%18 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %45
22: ; preds = %17
%23 = call i32 @TIMEVAL_TO_TIMESPEC(%struct.timeval* %3, %struct.timespec* %2)
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 500000000
store i32 %26, i32* %24
%27 = icmp sgt i32 %25, 499999999
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
store i32 %31, i32* %29
%32 = add nsw i32 %25, -500000000
store i32 %32, i32* %24
br label %33
33: ; preds = %28, %22
store i64 0, i64* @errno
%34 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%35 = call i32 @TEMP_FAILURE_RETRY(i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %45
39: ; preds = %33
%40 = load i64, i64* @errno
%41 = load i64, i64* @ETIMEDOUT
%42 = icmp eq i64 %40, %41
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i64 %40)
br label %45
45: ; preds = %39, %43, %37, %20, %15, %9
%46 = phi i32 [ 1, %9 ], [ 1, %15 ], [ 1, %20 ], [ 1, %37 ], [ 1, %43 ], [ 0, %39 ]
ret i32 %46
}