#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 /*<<< orphan*/ *fdopen(int, char *);
int fgetc(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int read_msg(int file) {
  int k = 0;
  char line[128];
  printf(" file stfin stdout %d %d %d \n", file, fileno(stdin), fileno(stdout));
  FILE *f;
  f = fdopen(file, "r+");
  if (f == NULL) {
    printf("error in file\n");
    perror("dopen");
  }
  int c;
  while ((c = fgetc(f)) && k < 10)
    printf("%c", c);
  return 10;
}


x86 assembly (clang, -O0):
<read_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,-0xa0(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    -0xa4(%rbp),%esi
mov    -0xa0(%rbp),%edx
mov    %eax,%ecx
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
lea    0xe68(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11ea <read_msg+0x9a>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
jmp    11ef <read_msg+0x9f>
mov    -0x98(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x9c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xa5(%rbp)
je     1221 <read_msg+0xd1>
cmpl   $0xa,-0x8(%rbp)
setl   %al
mov    %al,-0xa5(%rbp)
mov    -0xa5(%rbp),%al
test   $0x1,%al
jne    1234 <read_msg+0xe4>
jmp    124d <read_msg+0xfd>
mov    -0x9c(%rbp),%esi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ef <read_msg+0x9f>
mov    $0xa,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msg>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %eax,%ebp
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
lea    0xe66(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe70(%rip),%rsi
mov    %ebx,%edi
call   1080 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e7 <read_msg+0x77>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
lea    0xe51(%rip),%rdi
call   1090 <perror@plt>
jmp    11e7 <read_msg+0x77>
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
test   %eax,%eax
jne    11e0 <read_msg+0x70>
mov    $0xa,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c" file stfin stdout %d %d %d \0A\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [15 x i8] c"error in file\0A\00"
@.str.3 = [6 x i8] c"dopen\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @read_msg(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%7 = load i32, i32* %2
%8 = load i32, i32* @stdin
%9 = call i32 @fileno(i32 %8)
%10 = load i32, i32* @stdout
%11 = call i32 @fileno(i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %9, i32 %11)
%13 = load i32, i32* %2
%14 = call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %17, %1
br label %21
21: ; preds = %30, %20
%22 = load i32*, i32** %5
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %6
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, 10
br label %28
28: ; preds = %25, %21
%29 = phi i1 [ false, %21 ], [ %27, %25 ]
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = load i32, i32* %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %21
33: ; preds = %28
ret i32 10
}

LLVM IR (-O2):
@.str = [30 x i8] c" file stfin stdout %d %d %d \0A\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.3 = [6 x i8] c"dopen\00"
@str = [14 x i8] c"error in file\00"
define i32 @read_msg(i32 %0) {
%2 = load i32, i32* @stdin
%3 = tail call i32 @fileno(i32 %2)
%4 = load i32, i32* @stdout
%5 = tail call i32 @fileno(i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %3, i32 %5)
%7 = tail call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %9, %1
%13 = tail call i32 @fgetc(i32* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12, %15
%16 = phi i32 [ %18, %15 ], [ %13, %12 ]
%17 = tail call i32 @putchar(i32 %16)
%18 = tail call i32 @fgetc(i32* %7)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %15
20: ; preds = %15, %12
ret i32 10
}