#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__ EAGAIN;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int my_thread_number();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, char *, int);
int write(int, char *, int);
int do_work(int fd) {
  int s, done = 0;
  while (1) {
    ssize_t count;
    char buf[512];
    count = read(fd, buf, sizeof buf);
    if (count == -1) {
      if (errno != EAGAIN) {
        perror("read");
        done = 1;
      }
      break;
    } else if (count == 0) {
      done = 1;
      break;
    }
    s = write(1, buf, count);
    if (s == -1)
      do {
        perror("write");
        exit(EXIT_FAILURE);
      } while (0);
  }
  if (done) {
    printf("thread %d: Closed connection on descriptor %d\n",
           my_thread_number(), fd);
    close(fd);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d8 <do_work+0x68>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%rax
je     11d3 <do_work+0x63>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x10(%rbp)
jmp    1238 <do_work+0xc8>
cmpl   $0x0,-0x14(%rbp)
jne    11ee <do_work+0x7e>
movl   $0x1,-0x10(%rbp)
jmp    1238 <do_work+0xc8>
jmp    11f3 <do_work+0x83>
lea    -0x220(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1233 <do_work+0xc3>
jmp    1219 <do_work+0xa9>
lea    0xde5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1185 <do_work+0x15>
cmpl   $0x0,-0x10(%rbp)
je     126e <do_work+0xfe>
mov    $0x0,%al
call   1050 <my_thread_number@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1275 <do_work+0x105>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_work>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
je     11ec <do_work+0x7c>
cmp    $0xffffffff,%eax
je     11c6 <do_work+0x56>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1180 <do_work+0x10>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <do_work+0x70>
xor    %eax,%eax
jmp    120f <do_work+0x9f>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1050 <my_thread_number@plt>
lea    0xe11(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [47 x i8] c"thread %d: Closed connection on descriptor %d\0A\00"
define i32 @do_work(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [512 x i8]
store i32 %0, i32* %3
store i32 0, i32* %5
br label %8
8: ; preds = %1, %38
%9 = load i32, i32* %3
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 512)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %20
20: ; preds = %18, %14
br label %39
21: ; preds = %8
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 1, i32* %5
br label %39
25: ; preds = %21
br label %26
26: ; preds = %25
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @write(i32 1, i8* %27, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; No predecessors!
br label %38
38: ; preds = %37, %26
br label %8
39: ; preds = %24, %20
%40 = load i32, i32* %5
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = call i32 (...) @my_thread_number()
%44 = load i32, i32* %3
%45 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
store i32 1, i32* %2
br label %49
48: ; preds = %39
store i32 0, i32* %2
br label %49
49: ; preds = %48, %42
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [47 x i8] c"thread %d: Closed connection on descriptor %d\0A\00"
define i32 @do_work(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %19, %1
%5 = call i32 @read(i32 %0, i8* %3, i32 512)
switch i32 %5, label %12 [
i32 -1, label %6
i32 0, label %20
]
6: ; preds = %4
%7 = load i64, i64* @errno
%8 = load i64, i64* @EAGAIN
%9 = icmp eq i64 %7, %8
br i1 %9, label %24, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
12: ; preds = %4
%13 = call i32 @write(i32 1, i8* %3, i32 %5)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %12
br label %4
20: ; preds = %4, %10
%21 = tail call i32 (...) @my_thread_number()
%22 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %0)
%23 = tail call i32 @close(i32 %0)
br label %25
24: ; preds = %6
br label %25
25: ; preds = %24, %20
%26 = phi i32 [ 1, %20 ], [ 0, %24 ]
ret i32 %26
}