#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*/ abort();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main() {
  FILE *fp;
  int i;
  char pathname[1024];
  for (i = 0; i < 30; i++) {
    fp = fopen("output", "w");
    if (fp == NULL) {
      perror("fopen");
      getcwd(pathname, 1024);
      fprintf(stderr, "pathname = %s\n", pathname);
      abort();
    }
    sleep(5);
    fclose(fp);
  }
  fprintf(stdout, "completed successfully\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x1e,-0x14(%rbp)
jge    1218 <main+0xa8>
lea    0xe66(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <main+0x87>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1189 <main+0x19>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    $0x1e,%ebp
lea    0xe77(%rip),%r14
lea    0xe77(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e1 <main+0x71>
mov    %rax,%rbx
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0xffffffff,%ebp
jne    1190 <main+0x20>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"output\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"pathname = %s\0A\00"
@stdout = global i32 0
@.str.4 = [24 x i8] c"completed successfully\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
br label %5
5: ; preds = %24, %0
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 30
br i1 %7, label %8, label %27
8: ; preds = %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i32 @getcwd(i8* %14, i32 1024)
%16 = load i32, i32* @stderr
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %8
%21 = call i32 @sleep(i32 5)
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %20
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %5
27: ; preds = %5
%28 = load i32, i32* @stdout
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"output\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.3 = [15 x i8] c"pathname = %s\0A\00"
@stdout = global i32 0
@.str.4 = [24 x i8] c"completed successfully\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %13
%4 = phi i32 [ 0, %0 ], [ %16, %13 ]
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @getcwd(i8* %2, i32 1024)
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %3
%14 = tail call i32 @sleep(i32 5)
%15 = tail call i32 @fclose(i32* %5)
%16 = add nuw nsw i32 %4, 1
%17 = icmp eq i32 %16, 30
br i1 %17, label %18, label %3
18: ; preds = %13
%19 = load i32, i32* @stdout
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}