#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
FILE *open_tmp(int idx) {
  FILE *f;
  char fname[20];
  sprintf(fname, "tmpfile.%08x", idx);
  f = fopen(fname, "r+");
  if (f == NULL) {
    perror("open_tmp: open failed.\n");
    printf("filename %s\n", fname);
    exit(1);
  }
  return f;
}


x86 assembly (clang, -O0):
<open_tmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe97(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <open_tmp+0x63>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
lea    -0x30(%rbp),%rsi
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tmp>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%edx
lea    0xea2(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1183 <open_tmp+0x33>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
lea    0xe92(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"tmpfile.%08x\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [24 x i8] c"open_tmp: open failed.\0A\00"
@.str.3 = [13 x i8] c"filename %s\0A\00"
define i32* @open_tmp(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [20 x i8]
store i32 %0, i32* %2
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* %2
%7 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%15 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %3
ret i32* %18
}

LLVM IR (-O2):
@.str = [13 x i8] c"tmpfile.%08x\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [24 x i8] c"open_tmp: open failed.\0A\00"
@.str.3 = [13 x i8] c"filename %s\0A\00"
define noalias i32* @open_tmp(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
ret i32* %5
}