#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ err(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ rename(char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ unlink(char *);
int main(int argc, char *argv[]) {
  FILE *f;
  struct stat buf;
  unlink("testfoo2.tmp");
  f = fopen("testfoo1.tmp", "w");
  if (f == NULL)
    err("open");
  fclose(f);
  if (rename("testfoo1.tmp", "testfoo2.tmp") != 0)
    err("rename");
  if (stat("testfoo2.tmp", &buf) != 0 || !S_ISREG(buf.st_mode))
    err("stat 1");
  if (stat("testfoo2.tmp", &buf) != 0 || !S_ISREG(buf.st_mode))
    err("stat 2");
  if (unlink("testfoo2.tmp") != 0)
    err("unlink");
  printf("pass\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe64(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <main+0x50>
lea    0xe51(%rip),%rdi
call   1080 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe2d(%rip),%rdi
lea    0xe19(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0x0,%rax
je     1202 <main+0x82>
lea    0xe24(%rip),%rdi
call   1080 <err@plt>
lea    0xdf7(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    122d <main+0xad>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1239 <main+0xb9>
lea    0xdf4(%rip),%rdi
call   1080 <err@plt>
lea    0xdc0(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    1264 <main+0xe4>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1270 <main+0xf0>
lea    0xdc4(%rip),%rdi
call   1080 <err@plt>
lea    0xd89(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1292 <main+0x112>
lea    0xda9(%rip),%rdi
call   1080 <err@plt>
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe74(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe75(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b8 <main+0x38>
lea    0xe69(%rip),%rdi
call   1080 <err@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe46(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10a0 <rename@plt>
test   %rax,%rax
je     11e4 <main+0x64>
lea    0xe42(%rip),%rdi
call   1080 <err@plt>
lea    0xe15(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    1207 <main+0x87>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
jne    1213 <main+0x93>
lea    0xe1a(%rip),%rdi
call   1080 <err@plt>
lea    0xde6(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    1236 <main+0xb6>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
jne    1242 <main+0xc2>
lea    0xdf2(%rip),%rdi
call   1080 <err@plt>
lea    0xdb7(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     125f <main+0xdf>
lea    0xddc(%rip),%rdi
call   1080 <err@plt>
lea    0xdd7(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"testfoo2.tmp\00"
@.str.1 = [13 x i8] c"testfoo1.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [7 x i8] c"rename\00"
@.str.5 = [7 x i8] c"stat 1\00"
@.str.6 = [7 x i8] c"stat 2\00"
@.str.7 = [7 x i8] c"unlink\00"
@.str.8 = [6 x i8] c"pass\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32*, i32** %6
%16 = call i32 @fclose(i32* %15)
%17 = call i64 @rename(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %7)
%23 = icmp ne i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISREG(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %24, %21
%30 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %7)
%33 = icmp ne i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @S_ISREG(i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %34, %31
%40 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = call i64 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
%47 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"testfoo2.tmp\00"
@.str.1 = [13 x i8] c"testfoo1.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [7 x i8] c"rename\00"
@.str.5 = [7 x i8] c"stat 1\00"
@.str.6 = [7 x i8] c"stat 2\00"
@.str.7 = [7 x i8] c"unlink\00"
@str = [5 x i8] c"pass\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i64 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 @fclose(i32* %6)
%12 = tail call i64 @rename(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @S_ISREG(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19, %16
%25 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISREG(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29, %26
%35 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = call i64 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
ret i32 0
}