#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*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror_msg_and_die(char *, char const *);
int /*<<< orphan*/ set_cloexec_flag(int /*<<< orphan*/);
int /*<<< orphan*/ swap_uid();
FILE *strace_fopen(const char *path) {
  FILE *fp;
  swap_uid();
  fp = fopen(path, "w");
  if (!fp)
    perror_msg_and_die("Can't fopen '%s'", path);
  swap_uid();
  set_cloexec_flag(fileno(fp));
  return fp;
}


x86 assembly (clang, -O0):
<strace_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <swap_uid@plt>
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1192 <strace_fopen+0x42>
mov    -0x8(%rbp),%rsi
lea    0xe75(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    $0x0,%al
call   1070 <swap_uid@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1030 <set_cloexec_flag@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strace_fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <swap_uid@plt>
lea    0xe9b(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1184 <strace_fopen+0x34>
lea    0xe86(%rip),%rdi
mov    %r14,%rsi
call   1040 <perror_msg_and_die@plt>
xor    %eax,%eax
call   1070 <swap_uid@plt>
mov    %rbx,%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1030 <set_cloexec_flag@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [17 x i8] c"Can't fopen '%s'\00"
define i32* @strace_fopen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = call i32 (...) @swap_uid()
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @perror_msg_and_die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
%13 = call i32 (...) @swap_uid()
%14 = load i32*, i32** %3
%15 = call i32 @fileno(i32* %14)
%16 = call i32 @set_cloexec_flag(i32 %15)
%17 = load i32*, i32** %3
ret i32* %17
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [17 x i8] c"Can't fopen '%s'\00"
define noalias i32* @strace_fopen(i8* %0) {
%2 = tail call i32 (...) @swap_uid()
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror_msg_and_die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %7
7: ; preds = %5, %1
%8 = tail call i32 (...) @swap_uid()
%9 = tail call i32 @fileno(i32* %3)
%10 = tail call i32 @set_cloexec_flag(i32 %9)
ret i32* %3
}