#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 int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int read(int, char *, int);
int write(int, char *, int);
void *handle_alternate_thread(void *fd_as_ptr) {
  int fd = (intptr_t)fd_as_ptr;
  ssize_t s;
  char b;
  do
    s = write(fd, "hello", 5);
  while (s == -1 && errno == EINTR);
  assert(s == 5);
  do
    s = read(fd, &b, 1);
  while (s == -1 && errno == EINTR);
  assert(s == 0);
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<handle_alternate_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0xea3(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x10(%rbp)
mov    %al,-0x12(%rbp)
jne    1193 <handle_alternate_thread+0x53>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    1153 <handle_alternate_thread+0x13>
cmpl   $0x5,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x10(%rbp)
mov    %al,-0x13(%rbp)
jne    11ec <handle_alternate_thread+0xac>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    11af <handle_alternate_thread+0x6f>
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_alternate_thread>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xeae(%rip),%r14
mov    0x2e87(%rip),%r15
mov    0x2e78(%rip),%r12
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    117d <handle_alternate_thread+0x3d>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1160 <handle_alternate_thread+0x20>
xor    %edi,%edi
cmp    $0x5,%eax
sete   %dil
call   1060 <assert@plt>
lea    0x7(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    11ad <handle_alternate_thread+0x6d>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1190 <handle_alternate_thread+0x50>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@errno = global i64 0
@EINTR = global i64 0
define i8* @handle_alternate_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i64
%8 = trunc i64 %7 to i32
store i32 %8, i32* %3
br label %9
9: ; preds = %19, %1
%10 = load i32, i32* %3
%11 = call i32 @write(i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
store i32 %11, i32* %4
br label %12
12: ; preds = %9
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %9, label %21
21: ; preds = %19
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 5
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
br label %26
26: ; preds = %36, %21
%27 = load i32, i32* %3
%28 = call i32 @read(i32 %27, i8* %5, i32 1)
store i32 %28, i32* %4
br label %29
29: ; preds = %26
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br label %36
36: ; preds = %32, %29
%37 = phi i1 [ false, %29 ], [ %35, %32 ]
br i1 %37, label %26, label %38
38: ; preds = %36
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* %3
%44 = call i32 @close(i32 %43)
ret i8* null
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@errno = global i64 0
@EINTR = global i64 0
define noalias i8* @handle_alternate_thread(i8* %0) {
%2 = alloca i8
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
br label %5
5: ; preds = %5, %1
%6 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%7 = icmp eq i32 %6, -1
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = icmp eq i32 %6, 5
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
br label %16
16: ; preds = %16, %12
%17 = call i32 @read(i32 %4, i8* %2, i32 1)
%18 = icmp eq i32 %17, -1
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %16, label %23
23: ; preds = %16
%24 = icmp eq i32 %17, 0
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = tail call i32 @close(i32 %4)
ret i8* null
}