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

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ remove(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *tmpnam(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  char *tmp;
  FILE *file;
  struct stat sb;
  int failed = 0;
  tmp = tmpnam(NULL);
  if (tmp == NULL) {
    perror("tmpnam");
    return 1;
  }
  if ((file = fopen(tmp, "wx")) == NULL) {
    perror("fopen");
    return 1;
  }
  if (fclose(file) != 0) {
    perror("fclose");
    return 1;
  }
  if (remove(tmp) != 0) {
    perror("remove");
    return 1;
  }
  if (stat(tmp, &sb) == 0) {
    fprintf(stderr, "*** failed to remove file `%s'", tmp);
    failed++;
  } else
    printf("test 1 passed\n");
  tmp = tmpnam(NULL);
  if (tmp == NULL) {
    perror("tmpnam");
    return 1;
  }
  if (mkdir(tmp, 0644) != 0) {
    perror("mkdir");
    return 1;
  }
  if (remove(tmp) != 0) {
    perror("remove");
    return 1;
  }
  if (stat(tmp, &sb) == 0) {
    fprintf(stderr, "*** failed to remove directory `%s'", tmp);
    failed++;
  } else
    printf("test 2 passed\n");
  return failed ? 1 : 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <tmpnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <main+0x4d>
lea    0xe34(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1213 <main+0x83>
lea    0xe08(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     123e <main+0xae>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     1269 <main+0xd9>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    12a7 <main+0x117>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd8a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12b3 <main+0x123>
lea    0xd8f(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <tmpnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12e3 <main+0x153>
lea    0xd2e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
mov    $0x1a4,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1313 <main+0x183>
lea    0xd4a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
cmp    $0x0,%rax
je     133e <main+0x1ae>
lea    0xcea(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x20b>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    137c <main+0x1ec>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xce9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1388 <main+0x1f8>
lea    0xcf3(%rip),%rdi
call   1060 <printf@plt>
mov    -0x2c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %edi,%edi
call   10b0 <tmpnam@plt>
test   %rax,%rax
je     1283 <main+0xf3>
mov    %rax,%rbx
lea    0xe5a(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1295 <main+0x105>
mov    %rax,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    129e <main+0x10e>
mov    %rbx,%rdi
call   1030 <remove@plt>
test   %rax,%rax
jne    128c <main+0xfc>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1200 <main+0x70>
lea    0xe70(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
jmp    121d <main+0x8d>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%ebp
xor    %edi,%edi
call   10b0 <tmpnam@plt>
test   %rax,%rax
je     1283 <main+0xf3>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1a4,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jne    12a7 <main+0x117>
mov    %rbx,%rdi
call   1030 <remove@plt>
test   %rax,%rax
jne    128c <main+0xfc>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     1269 <main+0xd9>
lea    0xe13(%rip),%rdi
call   1050 <puts@plt>
jmp    12b8 <main+0x128>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
jmp    12b3 <main+0x123>
lea    0xd76(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xd84(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xd6e(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xd6b(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xd8f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"tmpnam\00"
@.str.1 = [3 x i8] c"wx\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fclose\00"
@.str.4 = [7 x i8] c"remove\00"
@stderr = global i32 0
@.str.5 = [31 x i8] c"*** failed to remove file `%s'\00"
@.str.6 = [15 x i8] c"test 1 passed\0A\00"
@.str.7 = [6 x i8] c"mkdir\00"
@.str.8 = [36 x i8] c"*** failed to remove directory `%s'\00"
@.str.9 = [15 x i8] c"test 2 passed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%10 = call i8* @tmpnam(i32* null)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %80
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %80
21: ; preds = %15
%22 = load i32*, i32** %7
%23 = call i64 @fclose(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %80
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = call i64 @remove(i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %80
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = call i64 @stat(i8* %34, %struct.stat* %8)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %6
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %45
43: ; preds = %33
%44 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = call i8* @tmpnam(i32* null)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %80
51: ; preds = %45
%52 = load i8*, i8** %6
%53 = call i64 @mkdir(i8* %52, i32 420)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %80
57: ; preds = %51
%58 = load i8*, i8** %6
%59 = call i64 @remove(i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %80
63: ; preds = %57
%64 = load i8*, i8** %6
%65 = call i64 @stat(i8* %64, %struct.stat* %8)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %63
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %6
%70 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* %69)
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 1
store i32 %72, i32* %9
br label %75
73: ; preds = %63
%74 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %75
75: ; preds = %73, %67
%76 = load i32, i32* %9
%77 = icmp ne i32 %76, 0
%78 = zext i1 %77 to i64
%79 = select i1 %77, i32 1, i32 0
store i32 %79, i32* %3
br label %80
80: ; preds = %75, %61, %55, %49, %31, %25, %19, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"tmpnam\00"
@.str.1 = [3 x i8] c"wx\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fclose\00"
@.str.4 = [7 x i8] c"remove\00"
@stderr = global i32 0
@.str.5 = [31 x i8] c"*** failed to remove file `%s'\00"
@.str.7 = [6 x i8] c"mkdir\00"
@.str.8 = [36 x i8] c"*** failed to remove directory `%s'\00"
@str = [14 x i8] c"test 1 passed\00"
@str.10 = [14 x i8] c"test 2 passed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i8* @tmpnam(i32* null)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %60
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %60
14: ; preds = %9
%15 = tail call i64 @fclose(i32* %10)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %60
19: ; preds = %14
%20 = tail call i64 @remove(i8* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %60
24: ; preds = %19
%25 = call i64 @stat(i8* %5, %struct.stat* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %5)
br label %32
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = phi i32 [ 1, %27 ], [ 0, %30 ]
%34 = call i8* @tmpnam(i32* null)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %60
38: ; preds = %32
%39 = call i64 @mkdir(i8* %34, i32 420)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %60
43: ; preds = %38
%44 = call i64 @remove(i8* %34)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %60
48: ; preds = %43
%49 = call i64 @stat(i8* %34, %struct.stat* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i8* %34)
br label %56
54: ; preds = %48
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = phi i32 [ 1, %51 ], [ %33, %54 ]
%58 = icmp ne i32 %57, 0
%59 = zext i1 %58 to i32
br label %60
60: ; preds = %56, %46, %41, %36, %22, %17, %12, %7
%61 = phi i32 [ 1, %7 ], [ 1, %12 ], [ 1, %17 ], [ 1, %22 ], [ 1, %36 ], [ 1, %41 ], [ 1, %46 ], [ %59, %56 ]
ret i32 %61
}