#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 dummy;
};
typedef int /*<<< orphan*/ st;
typedef int /*<<< orphan*/ fst;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int memcmp(struct stat *, struct stat *, int);
int /*<<< orphan*/ memset(struct stat *, int, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ show_stat(struct stat *);
int stat(char *, struct stat *);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  int ret;
  int fd;
  struct stat fst, st;
  memset(&fst, 0xAA, sizeof(fst));
  memset(&st, 0x55, sizeof(st));
  unlink(".testfile");
  fd = open(".testfile", O_WRONLY | O_CREAT | O_EXCL, 0);
  if (fd < 0) {
    perror("open(.testfile) failed");
    return 1;
  }
  ret = fstat(fd, &fst);
  if (ret != 0) {
    perror("fstat(.testfile) failed");
    return 1;
  }
  close(fd);
  ret = stat(".testfile", &st);
  if (ret != 0) {
    perror("stat(.testfile) failed");
    return 1;
  }
  ret = memcmp(&fst, &st, sizeof(fst));
  if (ret != 0) {
    printf("FAILED: memcmp() = %i\n", ret);
    show_stat(&fst);
    show_stat(&st);
  }
  unlink(".testfile");
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0xaa,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x18(%rbp),%rdi
mov    $0x55,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0xe24(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
lea    0xdfd(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    122f <main+0x8f>
lea    0xdec(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ef <main+0x14f>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1260 <main+0xc0>
lea    0xdd2(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ef <main+0x14f>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xd91(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     129d <main+0xfd>
lea    0xd96(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ef <main+0x14f>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcmp@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     12dd <main+0x13d>
mov    -0x8(%rbp),%esi
lea    0xd73(%rip),%rdi
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
call   1090 <show_stat@plt>
lea    -0x18(%rbp),%rdi
call   1090 <show_stat@plt>
lea    0xd1c(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
mov    $0xaa,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %rsp,%rdi
mov    $0x55,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0xe2e(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2de7(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     127b <main+0xdb>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %eax,%eax
jne    1284 <main+0xe4>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xddd(%rip),%rdi
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %eax,%eax
jne    128d <main+0xed>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
mov    $0x4,%edx
call   1070 <memcmp@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1267 <main+0xc7>
lea    0xdeb(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0x8(%rsp),%rdi
call   1090 <show_stat@plt>
mov    %rsp,%rdi
call   1090 <show_stat@plt>
lea    0xd92(%rip),%rdi
call   1030 <unlink@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd88(%rip),%rdi
jmp    1294 <main+0xf4>
lea    0xd96(%rip),%rdi
jmp    1294 <main+0xf4>
lea    0xd8e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    1273 <main+0xd3>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c".testfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [23 x i8] c"open(.testfile) failed\00"
@.str.2 = [24 x i8] c"fstat(.testfile) failed\00"
@.str.3 = [23 x i8] c"stat(.testfile) failed\00"
@.str.4 = [23 x i8] c"FAILED: memcmp() = %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca %struct.stat
store i32 0, i32* %1
%6 = call i32 @memset(%struct.stat* %4, i32 170, i32 4)
%7 = call i32 @memset(%struct.stat* %5, i32 85, i32 4)
%8 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13, i32 0)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %46
19: ; preds = %0
%20 = load i32, i32* %3
%21 = call i32 @fstat(i32 %20, %struct.stat* %4)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %46
26: ; preds = %19
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %5)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
34: ; preds = %26
%35 = call i32 @memcmp(%struct.stat* %4, %struct.stat* %5, i32 4)
store i32 %35, i32* %2
%36 = load i32, i32* %2
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i32, i32* %2
%40 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = call i32 @show_stat(%struct.stat* %4)
%42 = call i32 @show_stat(%struct.stat* %5)
br label %43
43: ; preds = %38, %34
%44 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%45 = load i32, i32* %2
store i32 %45, i32* %1
br label %46
46: ; preds = %43, %32, %24, %17
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c".testfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [23 x i8] c"open(.testfile) failed\00"
@.str.2 = [24 x i8] c"fstat(.testfile) failed\00"
@.str.3 = [23 x i8] c"stat(.testfile) failed\00"
@.str.4 = [23 x i8] c"FAILED: memcmp() = %i\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %1 to i8*
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i32 @memset(%struct.stat* %1, i32 170, i32 4)
%6 = call i32 @memset(%struct.stat* %2, i32 85, i32 4)
%7 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %37
17: ; preds = %0
%18 = call i32 @fstat(i32 %13, %struct.stat* %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %37
22: ; preds = %17
%23 = call i32 @close(i32 %13)
%24 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %37
28: ; preds = %22
%29 = call i32 @memcmp(%struct.stat* %1, %struct.stat* %2, i32 4)
%30 = icmp eq i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%33 = call i32 @show_stat(%struct.stat* %1)
%34 = call i32 @show_stat(%struct.stat* %2)
br label %35
35: ; preds = %31, %28
%36 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %26, %20, %15
%38 = phi i32 [ 1, %15 ], [ 1, %20 ], [ 1, %26 ], [ %29, %35 ]
ret i32 %38
}