#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*/ name;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *, char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char *, ...);
FILE *open_exclusive_file(int picture_no, char *extension) {
  int try;
  char name[32];
  FILE *fh;
  for (try = 0;; try++) {
    if (try == 0)
      snprintf(name, sizeof(name), "polaroid.%02d.%s", picture_no, extension);
    else
      snprintf(name, sizeof(name), "polaroid.%02d.%s.%d", picture_no, extension,
               try);
    fh = fopen(name, "wx");
    if (fh == NULL) {
      if (errno != EEXIST)
        error(1, errno, "%s.%d: fopen()",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/"
              "main.c",
              164);
    } else
      break;
  }
  return fh;
}


x86 assembly (clang, -O0):
<open_exclusive_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1173 <open_exclusive_file+0x43>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x20,%esi
lea    0xe99(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1195 <open_exclusive_file+0x65>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
mov    $0x20,%esi
lea    0xe83(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11fb <open_exclusive_file+0xcb>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     11f6 <open_exclusive_file+0xc6>
mov    0x2e03(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xe44(%rip),%rdx
lea    0xe4c(%rip),%rcx
mov    $0xa4,%r8d
call   1040 <error@plt>
jmp    1200 <open_exclusive_file+0xd0>
jmp    1213 <open_exclusive_file+0xe3>
jmp    1205 <open_exclusive_file+0xd5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <open_exclusive_file+0x16>
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_exclusive_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rsp,%rbx
lea    0xed8(%rip),%r12
mov    0x2e8c(%rip),%r13
xor    %r14d,%r14d
jmp    1164 <open_exclusive_file+0x34>
nopl   0x0(%rax)
add    $0x1,%r14d
mov    %rbx,%rdi
mov    $0x20,%esi
test   %r14d,%r14d
je     1190 <open_exclusive_file+0x60>
lea    0xe99(%rip),%rdx
mov    %ebp,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11a3 <open_exclusive_file+0x73>
nopl   0x0(%rax)
lea    0xe69(%rip),%rdx
mov    %ebp,%ecx
mov    %r15,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11e6 <open_exclusive_file+0xb6>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rsi
cmp    0x0(%r13),%rsi
je     1160 <open_exclusive_file+0x30>
mov    $0x1,%edi
lea    0xe59(%rip),%rdx
lea    0xe61(%rip),%rcx
mov    $0xa4,%r8d
call   1040 <error@plt>
jmp    1160 <open_exclusive_file+0x30>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"polaroid.%02d.%s\00"
@.str.1 = [20 x i8] c"polaroid.%02d.%s.%d\00"
@.str.2 = [3 x i8] c"wx\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [15 x i8] c"%s.%d: fopen()\00"
@.str.4 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/main.c\00"
define i32* @open_exclusive_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %8
8: ; preds = %37, %2
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i32, i8*, i32, i8*, ...) @snprintf(i8* %12, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14)
br label %22
16: ; preds = %8
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* %3
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 (i8*, i32, i8*, i32, i8*, ...) @snprintf(i8* %17, i32 32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %19, i32 %20)
br label %22
22: ; preds = %16, %11
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EEXIST
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = call i32 @error(i32 1, i64 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 164)
br label %34
34: ; preds = %31, %27
br label %36
35: ; preds = %22
br label %40
36: ; preds = %34
br label %37
37: ; preds = %36
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %8
40: ; preds = %35
%41 = load i32*, i32** %7
ret i32* %41
}

LLVM IR (-O2):
@.str = [17 x i8] c"polaroid.%02d.%s\00"
@.str.1 = [20 x i8] c"polaroid.%02d.%s.%d\00"
@.str.2 = [3 x i8] c"wx\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [15 x i8] c"%s.%d: fopen()\00"
@.str.4 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/main.c\00"
define noalias i32* @open_exclusive_file(i32 %0, i8* %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %21, %2
%6 = phi i32 [ 0, %2 ], [ %22, %21 ]
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 (i8*, i32, i8*, i32, i8*, ...) @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
br label %12
10: ; preds = %5
%11 = call i32 (i8*, i32, i8*, i32, i8*, ...) @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %1, i32 %6)
br label %12
12: ; preds = %10, %8
%13 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @error(i32 1, i64 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 164)
br label %21
21: ; preds = %19, %15
%22 = add nuw nsw i32 %6, 1
br label %5
23: ; preds = %12
ret i32* %13
}