#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_WRONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  char *fname = "test2.txt";
  int fd;
  FILE *fp;
  if ((fp = open(fname, O_WRONLY)) > 0) {
    printf("Success!!\nFile descriptor: %d\n", fd);
  } else {
    printf("failed\n");
  }
  if ((fp = fdopen(fd, "w")) != NULL) {
    printf("fdopen() Success!!\n");
  } else {
    printf("fdopen() failed\n");
  }
  fprintf(fp, "I'm your father.\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    11a0 <main+0x50>
mov    -0x14(%rbp),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ae <main+0x5e>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xe71(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11de <main+0x8e>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ec <main+0x9c>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1070 <open@plt>
test   %rax,%rax
je     118b <main+0x2b>
lea    0xe88(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1197 <main+0x37>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8b(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
lea    0xe8d(%rip),%rax
lea    0xe96(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xe64(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"test2.txt\00"
@O_WRONLY = global i32 0
@.str.1 = [31 x i8] c"Success!!\0AFile descriptor: %d\0A\00"
@.str.2 = [8 x i8] c"failed\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [20 x i8] c"fdopen() Success!!\0A\00"
@.str.5 = [17 x i8] c"fdopen() failed\0A\00"
@.str.6 = [18 x i8] c"I'm your father.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_WRONLY
%7 = call i32* @open(i8* %5, i32 %6)
store i32* %7, i32** %4
%8 = icmp ugt i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* %3
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %22
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = load i32*, i32** %4
%24 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"test2.txt\00"
@O_WRONLY = global i32 0
@.str.1 = [31 x i8] c"Success!!\0AFile descriptor: %d\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.6 = [18 x i8] c"I'm your father.\0A\00"
@str = [7 x i8] c"failed\00"
@str.7 = [16 x i8] c"fdopen() failed\00"
@str.8 = [19 x i8] c"fdopen() Success!!\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = tail call i32* @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32* %2, null
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 undef)
br label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %8
8: ; preds = %6, %4
%9 = tail call i32* @fdopen(i32 undef, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp eq i32* %9, null
%11 = select i1 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0)
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
%13 = tail call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i64 17, i64 1, i32* %9)
%14 = tail call i32 @fclose(i32* %9)
ret i32 0
}