#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*/ cctools_fatal(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int const);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void opts_write_port_file(const char *port_file, const int port) {
  if (port_file) {
    FILE *file = fopen(port_file, "w");
    if (!file)
      cctools_fatal("couldn't write to %s: %s\n", port_file, strerror(errno));
    fprintf(file, "%d\n", port);
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<opts_write_port_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11cd <opts_write_port_file+0x7d>
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b1 <opts_write_port_file+0x61>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe56(%rip),%rdi
call   1040 <cctools_fatal@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opts_write_port_file>:
test   %rdi,%rdi
je     11b0 <opts_write_port_file+0x60>
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1192 <opts_write_port_file+0x42>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe7a(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <cctools_fatal@plt>
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [26 x i8] c"couldn't write to %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define void @opts_write_port_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %24
8: ; preds = %2
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @cctools_fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %16)
br label %18
18: ; preds = %13, %8
%19 = load i32*, i32** %5
%20 = load i32, i32* %4
%21 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %18, %2
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [26 x i8] c"couldn't write to %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
define void @opts_write_port_file(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @cctools_fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %9)
br label %11
11: ; preds = %7, %4
%12 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%13 = tail call i32 @fclose(i32* %5)
br label %14
14: ; preds = %11, %2
ret void
}