#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*/ O_RDONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  FILE *fp;
  int fd;
  char str[100] = "fdopen test string\n";
  fd = open("fdopen.txt", O_RDONLY);
  fp = fopen("fdopen.txt", "w");
  if (fp == NULL)
    return -1;
  fwrite(str, strlen(str), 1, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe83(%rip),%rsi
mov    $0x64,%edx
call   1050 <memcpy@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xecd(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xebe(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <main+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <main+0xa1>
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
movaps 0xef2(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xed6(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeba(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe9e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe82(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xeae(%rip),%rbx
mov    %rbx,%rdi
call   1050 <open@plt>
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fe <main+0xae>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1203 <main+0xb3>
mov    $0xffffffff,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str = [100 x i8] c"fdopen test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"fdopen.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = bitcast [100 x i8]* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %20
12: ; preds = %0
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = load i32*, i32** %2
%17 = call i32 @fwrite(i8* %13, i32 %15, i32 1, i32* %16)
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %12, %11
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@__const.main.str = [100 x i8] c"fdopen test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"fdopen.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = call i32 @strlen(i8* %2)
%9 = call i32 @fwrite(i8* %2, i32 %8, i32 1, i32* %5)
%10 = call i32 @fclose(i32* %5)
br label %11
11: ; preds = %0, %7
%12 = phi i32 [ 0, %7 ], [ -1, %0 ]
ret i32 %12
}