#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 */

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ printMemory();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readClose();
int main(const int argc, const char **argv) {
  int loop, loops;
  loops = 1000;
  if (argc == 2) {
    loops = atoi(argv[1]);
    if (loops < 0) {
      loops = 0 - loops;
    }
  }
  printf("Pre-defer tests:\n");
  printMemory();
  for (loop = 0; loop < loops; loop++) {
    readClose();
  }
  printf("\nPost-defer tests:\n");
  printMemory();
  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)
movl   $0x3e8,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    118e <main+0x4e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1189 <main+0x49>
xor    %eax,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118e <main+0x4e>
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <printMemory@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11c9 <main+0x89>
mov    $0x0,%al
call   1030 <readClose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a8 <main+0x68>
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <printMemory@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x3e8,%ebx
cmp    $0x2,%edi
jne    115b <main+0x1b>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
neg    %ebx
cmovs  %eax,%ebx
lea    0xe9e(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1040 <printMemory@plt>
test   %ebx,%ebx
je     118c <main+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <readClose@plt>
add    $0xffffffff,%ebx
jne    1180 <main+0x40>
lea    0xe7e(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1040 <printMemory@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Pre-defer tests:\0A\00"
@.str.1 = [20 x i8] c"\0APost-defer tests:\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1000, i32* %7
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @atoi(i8* %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* %7
%19 = sub nsw i32 0, %18
store i32 %19, i32* %7
br label %20
20: ; preds = %17, %10
br label %21
21: ; preds = %20, %2
%22 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (...) @printMemory()
store i32 0, i32* %6
br label %24
24: ; preds = %30, %21
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = call i32 (...) @readClose()
br label %30
30: ; preds = %28
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
br label %24
33: ; preds = %24
%34 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 (...) @printMemory()
ret i32 0
}

LLVM IR (-O2):
@str = [17 x i8] c"Pre-defer tests:\00"
@str.2 = [19 x i8] c"\0APost-defer tests:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = tail call i32 @llvm.abs.i32(i32 %7, i1 true)
br label %9
9: ; preds = %4, %2
%10 = phi i32 [ 1000, %2 ], [ %8, %4 ]
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 (...) @printMemory()
%13 = icmp eq i32 %10, 0
br i1 %13, label %19, label %14
14: ; preds = %9, %14
%15 = phi i32 [ %17, %14 ], [ 0, %9 ]
%16 = tail call i32 (...) @readClose()
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, %10
br i1 %18, label %19, label %14
19: ; preds = %14, %9
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0))
%21 = tail call i32 (...) @printMemory()
ret i32 0
}