#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 uint32_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int O_WRONLY;
scalar_t__ ab_read(int, char *, int);
int /*<<< orphan*/ ab_write(int, char *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
void *thread_copy(void *arg) {
  char buff[1024];
  int fdin, fdout;
  uint32_t copy_count = 0;
  fdin = open("/dev/random", O_RDONLY | O_NONBLOCK);
  if (fdin < 0) {
    perror("/dev/random.");
    goto out_open_fdin;
  }
  fdout = open("/dev/zero", O_WRONLY | O_NONBLOCK);
  if (fdout < 0) {
    perror("/dev/zero");
    goto out_open_fdout;
  }
  ssize_t retsize;
  while ((retsize = ab_read(fdin, buff, 1024)) > 0) {
    ab_write(fdout, buff, (size_t)retsize);
    copy_count += retsize;
    fprintf(stderr, "Copied %u bytes\n", copy_count);
  }
  close(fdout);
out_open_fdout:
  close(fdin);
out_open_fdin:
  if (retsize) {
    fprintf(stderr, "thread_copy exited (Error:%s)\n", strerror(-(int)retsize));
  }
  return arg;
}


x86 assembly (clang, -O0):
<thread_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x41c(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    11cb <thread_copy+0x5b>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
jmp    12a2 <thread_copy+0x132>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
lea    0xe35(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    120d <thread_copy+0x9d>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
jmp    1297 <thread_copy+0x127>
jmp    1212 <thread_copy+0xa2>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <ab_read@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jle    128c <thread_copy+0x11c>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1030 <ab_write@plt>
mov    -0x428(%rbp),%rcx
movslq -0x41c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x41c(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x41c(%rbp),%edx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1212 <thread_copy+0xa2>
mov    -0x418(%rbp),%edi
call   1040 <close@plt>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x428(%rbp)
je     12e6 <thread_copy+0x176>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x428(%rbp),%rax
xor    %edi,%edi
sub    %eax,%edi
call   1090 <strerror@plt>
mov    -0x42c(%rbp),%edi
mov    %rax,%rdx
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
mov    0x2e55(%rip),%rax
mov    0x2e56(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1298 <thread_copy+0x128>
mov    %eax,%r15d
mov    0x2e0b(%rip),%rax
mov    0x0(%rbp),%esi
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     12a6 <thread_copy+0x136>
mov    %eax,%r12d
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <ab_read@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    1242 <thread_copy+0xd2>
lea    0x10(%rsp),%rbx
lea    0xe27(%rip),%r13
xor    %r14d,%r14d
nop
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <ab_write@plt>
movslq %r14d,%r14
add    %rbp,%r14
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <ab_read@plt>
mov    %rax,%rbp
test   %rax,%rax
jg     1200 <thread_copy+0x90>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rbp,%rbp
mov    0x8(%rsp),%rbx
je     1283 <thread_copy+0x113>
mov    0x2d6d(%rip),%rax
mov    (%rax),%r14d
neg    %ebp
mov    %ebp,%edi
call   1090 <strerror@plt>
lea    0xdbe(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
jmp    125c <thread_copy+0xec>
lea    0xd6c(%rip),%rdi
call   1080 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
jmp    1283 <thread_copy+0x113>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [13 x i8] c"/dev/random.\00"
@.str.2 = [10 x i8] c"/dev/zero\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.3 = [17 x i8] c"Copied %u bytes\0A\00"
@.str.4 = [31 x i8] c"thread_copy exited (Error:%s)\0A\00"
define i8* @thread_copy(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %6
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %50
16: ; preds = %1
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %47
25: ; preds = %16
br label %26
26: ; preds = %31, %25
%27 = load i32, i32* %4
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%29 = call i64 @ab_read(i32 %27, i8* %28, i32 1024)
store i64 %29, i64* %7
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %26
%32 = load i32, i32* %5
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%34 = load i64, i64* %7
%35 = call i32 @ab_write(i32 %32, i8* %33, i64 %34)
%36 = load i64, i64* %7
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = add nsw i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
%41 = load i32, i32* @stderr
%42 = load i32, i32* %6
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %26
44: ; preds = %26
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %23
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %14
%51 = load i64, i64* %7
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = load i64, i64* %7
%56 = trunc i64 %55 to i32
%57 = sub nsw i32 0, %56
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %53, %50
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [13 x i8] c"/dev/random.\00"
@.str.2 = [10 x i8] c"/dev/zero\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.3 = [17 x i8] c"Copied %u bytes\0A\00"
@.str.4 = [31 x i8] c"thread_copy exited (Error:%s)\0A\00"
define i8* @thread_copy(i8* readnone returned %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %40
11: ; preds = %1
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = call i64 @ab_read(i32 %7, i8* %3, i32 1024)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %23, label %35
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @close(i32 %7)
br label %47
23: ; preds = %17, %23
%24 = phi i64 [ %33, %23 ], [ %18, %17 ]
%25 = phi i64 [ %29, %23 ], [ 0, %17 ]
%26 = call i32 @ab_write(i32 %15, i8* %3, i64 %24)
%27 = shl i64 %25, 32
%28 = ashr exact i64 %27, 32
%29 = add nsw i64 %28, %24
%30 = trunc i64 %29 to i32
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%33 = call i64 @ab_read(i32 %7, i8* %3, i32 1024)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %23, label %35
35: ; preds = %23, %17
%36 = phi i64 [ %18, %17 ], [ %33, %23 ]
%37 = call i32 @close(i32 %15)
%38 = call i32 @close(i32 %7)
%39 = icmp eq i64 %36, 0
br i1 %39, label %47, label %40
40: ; preds = %9, %35
%41 = phi i64 [ undef, %9 ], [ %36, %35 ]
%42 = load i32, i32* @stderr
%43 = trunc i64 %41 to i32
%44 = sub nsw i32 0, %43
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %20, %40, %35
ret i8* %0
}