#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int ftell(int /*<<< orphan*/ *);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ *stderr;
char *tmpnam(int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  char *name;
  FILE *fp = NULL;
  int retval = 0;
  int fd;
  name = tmpnam(NULL);
  fp = fopen(name, "w");
  if (!(fp != NULL)) {
    fputs("test failed: "
          "fp != NULL"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  }
  fputs("foobar and baz", fp);
  fclose(fp);
  fp = NULL;
  fd = open(name, O_RDWR | O_CREAT);
  if (!(fd != -1)) {
    fputs("test failed: "
          "fd != -1"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
  if (!(lseek(fd, 5, SEEK_SET) == 5)) {
    fputs("test failed: "
          "lseek (fd, 5, SEEK_SET) == 5"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
  fp = fdopen(fd, "a");
  if (!(fp != NULL)) {
    fputs("test failed: "
          "fp != NULL"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
  if (!(ftell(fp) == 14)) {
    fputs("test failed: "
          "ftell (fp) == 14"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
the_end:
  if (fp != NULL)
    fclose(fp);
  unlink(name);
  return retval;
}


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)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <tmpnam@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <main+0x73>
mov    0x2de8(%rip),%rax
mov    (%rax),%rsi
lea    0xe10(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1328 <main+0x198>
mov    -0x20(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    126e <main+0xde>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rsi
lea    0xdcd(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1328 <main+0x198>
mov    -0x28(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <lseek@plt>
cmp    $0x5,%eax
je     12af <main+0x11f>
mov    0x2d3c(%rip),%rax
mov    (%rax),%rsi
lea    0xda3(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1328 <main+0x198>
mov    -0x28(%rbp),%edi
lea    0xdb3(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12ef <main+0x15f>
mov    0x2cfc(%rip),%rax
mov    (%rax),%rsi
lea    0xd24(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1328 <main+0x198>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
cmp    $0xe,%eax
je     1323 <main+0x193>
mov    0x2cc8(%rip),%rax
mov    (%rax),%rsi
lea    0xd5c(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1328 <main+0x198>
jmp    1328 <main+0x198>
cmpq   $0x0,-0x20(%rbp)
je     133c <main+0x1ac>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %edi,%edi
call   10b0 <tmpnam@plt>
mov    %rax,%rbx
lea    0xe5b(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1271 <main+0xe1>
mov    %rax,%rbp
lea    0xe5b(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2dff(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1289 <main+0xf9>
mov    %eax,%ebp
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <lseek@plt>
cmp    $0x5,%eax
jne    12a1 <main+0x111>
lea    0xe48(%rip),%rsi
mov    %ebp,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     1271 <main+0xe1>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <ftell@plt>
xor    %ebp,%ebp
cmp    $0xe,%eax
je     1267 <main+0xd7>
mov    0x2d87(%rip),%rax
mov    (%rax),%rcx
lea    0xe1b(%rip),%rdi
mov    $0x1e,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    12c6 <main+0x136>
mov    0x2d58(%rip),%rax
mov    (%rax),%rcx
lea    0xd80(%rip),%rdi
mov    $0x18,%esi
jmp    12b7 <main+0x127>
mov    0x2d40(%rip),%rax
mov    (%rax),%rcx
lea    0xd90(%rip),%rdi
mov    $0x16,%esi
jmp    12b7 <main+0x127>
mov    0x2d28(%rip),%rax
mov    (%rax),%rcx
lea    0xd8f(%rip),%rdi
mov    $0x2a,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"test failed: fp != NULL\0A\00"
@stderr = global i32* null
@.str.2 = [15 x i8] c"foobar and baz\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.3 = [23 x i8] c"test failed: fd != -1\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [43 x i8] c"test failed: lseek (fd, 5, SEEK_SET) == 5\0A\00"
@.str.5 = [2 x i8] c"a\00"
@.str.6 = [31 x i8] c"test failed: ftell (fp) == 14\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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %7
store i32 0, i32* %8
%10 = call i8* @tmpnam(i32* null)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32*, i32** @stderr
%17 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32* %16)
store i32 1, i32* %8
br label %57
18: ; preds = %2
%19 = load i32*, i32** %7
%20 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32* %19)
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
store i32* null, i32** %7
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, -1
br i1 %29, label %33, label %30
30: ; preds = %18
%31 = load i32*, i32** @stderr
%32 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32* %31)
store i32 1, i32* %8
br label %57
33: ; preds = %18
%34 = load i32, i32* %9
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 5, i32 %35)
%37 = icmp eq i32 %36, 5
br i1 %37, label %41, label %38
38: ; preds = %33
%39 = load i32*, i32** @stderr
%40 = call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32* %39)
store i32 1, i32* %8
br label %57
41: ; preds = %33
%42 = load i32, i32* %9
%43 = call i32* @fdopen(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %43, i32** %7
%44 = load i32*, i32** %7
%45 = icmp ne i32* %44, null
br i1 %45, label %49, label %46
46: ; preds = %41
%47 = load i32*, i32** @stderr
%48 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32* %47)
store i32 1, i32* %8
br label %57
49: ; preds = %41
%50 = load i32*, i32** %7
%51 = call i32 @ftell(i32* %50)
%52 = icmp eq i32 %51, 14
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = load i32*, i32** @stderr
%55 = call i32 @fputs(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32* %54)
store i32 1, i32* %8
br label %57
56: ; preds = %49
br label %57
57: ; preds = %56, %53, %46, %38, %30, %15
%58 = load i32*, i32** %7
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i32*, i32** %7
%62 = call i32 @fclose(i32* %61)
br label %63
63: ; preds = %60, %57
%64 = load i8*, i8** %6
%65 = call i32 @unlink(i8* %64)
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"test failed: fp != NULL\0A\00"
@stderr = global i32* null
@.str.2 = [15 x i8] c"foobar and baz\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.3 = [23 x i8] c"test failed: fd != -1\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [43 x i8] c"test failed: lseek (fd, 5, SEEK_SET) == 5\0A\00"
@.str.5 = [2 x i8] c"a\00"
@.str.6 = [31 x i8] c"test failed: ftell (fp) == 14\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @tmpnam(i32* null)
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32*, i32** @stderr
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 24, i64 1, i32* %7)
br label %42
9: ; preds = %2
%10 = tail call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 14, i64 1, i32* %4)
%11 = tail call i32 @fclose(i32* %4)
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %3, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32*, i32** @stderr
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 22, i64 1, i32* %18)
br label %42
20: ; preds = %9
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %15, i32 5, i32 %21)
%23 = icmp eq i32 %22, 5
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i32*, i32** @stderr
%26 = tail call i64 @fwrite(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i64 42, i64 1, i32* %25)
br label %42
27: ; preds = %20
%28 = tail call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32*, i32** @stderr
%32 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 24, i64 1, i32* %31)
br label %42
33: ; preds = %27
%34 = tail call i32 @ftell(i32* %28)
%35 = icmp eq i32 %34, 14
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i32*, i32** @stderr
%38 = tail call i64 @fwrite(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i64 30, i64 1, i32* %37)
br label %39
39: ; preds = %36, %33
%40 = phi i32 [ 0, %33 ], [ 1, %36 ]
%41 = tail call i32 @fclose(i32* %28)
br label %42
42: ; preds = %6, %17, %24, %30, %39
%43 = phi i32 [ %40, %39 ], [ 1, %30 ], [ 1, %24 ], [ 1, %17 ], [ 1, %6 ]
%44 = tail call i32 @unlink(i8* %3)
ret i32 %43
}