#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int dup(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_profile(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
void *handle_con(void *s_) {
  int s = (int)(intptr_t)s_;
  FILE *in = fdopen(s, "r");
  FILE *out = fdopen(dup(s), "w");
  if (!in || !out) {
    fputs("fdopen failed", stderr);
    exit(1);
  }
  FILE *profile_file = open_profile(in);
  if (profile_file == NULL) {
    fputs("sorry, but I was unable to find that profile.\n", out);
    goto out;
  }
  char buf[1024];
  size_t len;
  while (len = fread(buf, 1, 1024, profile_file)) {
    fwrite(buf, 1, len, out);
  }
out:
  fclose(in);
  fclose(out);
  return NULL;
}


x86 assembly (clang, -O0):
<handle_con>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe60(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,%edi
lea    0xe48(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d9 <handle_con+0x59>
cmpq   $0x0,-0x20(%rbp)
jne    11f9 <handle_con+0x79>
mov    0x2e00(%rip),%rax
mov    (%rax),%rsi
lea    0xe1a(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <open_profile@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1226 <handle_con+0xa6>
mov    -0x20(%rbp),%rsi
lea    0xdf6(%rip),%rdi
call   1070 <fputs@plt>
jmp    127c <handle_con+0xfc>
jmp    122b <handle_con+0xab>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
je     1277 <handle_con+0xf7>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    122b <handle_con+0xab>
jmp    127c <handle_con+0xfc>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_con>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xe78(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   1060 <dup@plt>
lea    0xe64(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %r14,%r14
je     1259 <handle_con+0xe9>
mov    %rax,%r15
test   %rax,%rax
je     1259 <handle_con+0xe9>
mov    %r14,%rdi
call   1030 <open_profile@plt>
test   %rax,%rax
je     121f <handle_con+0xaf>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1238 <handle_con+0xc8>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    11f0 <handle_con+0x80>
jmp    1238 <handle_con+0xc8>
lea    0xdec(%rip),%rdi
mov    $0x2e,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d80(%rip),%rax
mov    (%rax),%rcx
lea    0xd9a(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [14 x i8] c"fdopen failed\00"
@stderr = global i32* null
@.str.3 = [47 x i8] c"sorry, but I was unable to find that profile.\0A\00"
define i8* @handle_con(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [1024 x i8]
%8 = alloca i64
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = ptrtoint i8* %9 to i64
%11 = trunc i64 %10 to i32
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = call i32* @fdopen(i32 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32, i32* %3
%15 = call i32 @dup(i32 %14)
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %5
%17 = load i32*, i32** %4
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32*, i32** %5
%21 = icmp ne i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %19, %1
%23 = load i32*, i32** @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
%27 = load i32*, i32** %4
%28 = call i32* @open_profile(i32* %27)
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32*, i32** %5
%33 = call i32 @fputs(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32* %32)
br label %46
34: ; preds = %26
br label %35
35: ; preds = %40, %34
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i32*, i32** %6
%38 = call i64 @fread(i8* %36, i32 1, i32 1024, i32* %37)
store i64 %38, i64* %8
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%42 = load i64, i64* %8
%43 = load i32*, i32** %5
%44 = call i32 @fwrite(i8* %41, i32 1, i64 %42, i32* %43)
br label %35
45: ; preds = %35
br label %46
46: ; preds = %45, %31
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
ret i8* null
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [14 x i8] c"fdopen failed\00"
@stderr = global i32* null
@.str.3 = [47 x i8] c"sorry, but I was unable to find that profile.\0A\00"
define i8* @handle_con(i8* %0) {
%2 = alloca [1024 x i8]
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = tail call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @dup(i32 %4)
%7 = tail call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp ne i32* %5, null
%9 = icmp ne i32* %7, null
%10 = and i1 %8, %9
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32*, i32** @stderr
%13 = tail call i64 bitcast (i32 (i8*, i32, i64, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64 13, i64 1, i32* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = tail call i32* @open_profile(i32* %5)
%17 = icmp eq i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%20 = call i64 @fread(i8* %19, i32 1, i32 1024, i32* %16)
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %24
22: ; preds = %15
%23 = tail call i64 bitcast (i32 (i8*, i32, i64, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i64 46, i64 1, i32* %7)
br label %29
24: ; preds = %18, %24
%25 = phi i64 [ %27, %24 ], [ %20, %18 ]
%26 = call i32 @fwrite(i8* %19, i32 1, i64 %25, i32* %7)
%27 = call i64 @fread(i8* %19, i32 1, i32 1024, i32* %16)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %24
29: ; preds = %24, %18, %22
%30 = tail call i32 @fclose(i32* %5)
%31 = tail call i32 @fclose(i32* %7)
ret i8* null
}