#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 /*<<< orphan*/ O_RDONLY;
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*/ *);
char getc(int /*<<< orphan*/ *);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
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_RDONLY);
  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, "r");
  if (!(fp != NULL)) {
    fputs("test failed: "
          "fp != NULL"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
  if (!(getc(fp) == 'r')) {
    fputs("test failed: "
          "getc (fp) == 'r'"
          "\n",
          stderr);
    retval = 1;
    goto the_end;
  };
  if (!(getc(fp) == ' ')) {
    fputs("test failed: "
          "getc (fp) == ' '"
          "\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   10a0 <tmpnam@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <main+0x73>
mov    0x2df0(%rip),%rax
mov    (%rax),%rsi
lea    0xe10(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
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    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1265 <main+0xd5>
mov    0x2d8e(%rip),%rax
mov    (%rax),%rsi
lea    0xdd6(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x28(%rbp),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <lseek@plt>
cmp    $0x5,%eax
je     12a6 <main+0x116>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rsi
lea    0xdac(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x28(%rbp),%edi
lea    0xdbc(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12e6 <main+0x156>
mov    0x2d0d(%rip),%rax
mov    (%rax),%rsi
lea    0xd2d(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x20(%rbp),%rdi
call   10b0 <getc@plt>
movsbl %al,%eax
cmp    $0x72,%eax
je     131d <main+0x18d>
mov    0x2cd6(%rip),%rax
mov    (%rax),%rsi
lea    0xd62(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
mov    -0x20(%rbp),%rdi
call   10b0 <getc@plt>
movsbl %al,%eax
cmp    $0x20,%eax
je     1354 <main+0x1c4>
mov    0x2c9f(%rip),%rax
mov    (%rax),%rsi
lea    0xd4a(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x1,-0x24(%rbp)
jmp    1359 <main+0x1c9>
jmp    1359 <main+0x1c9>
cmpq   $0x0,-0x20(%rbp)
je     136d <main+0x1dd>
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   10a0 <tmpnam@plt>
mov    %rax,%rbx
lea    0xe5b(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1249 <main+0xb9>
mov    %rax,%rbp
lea    0xe5b(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1261 <main+0xd1>
mov    %eax,%ebp
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <lseek@plt>
cmp    $0x5,%eax
jne    1279 <main+0xe9>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
call   1060 <fdopen@plt>
test   %rax,%rax
je     1249 <main+0xb9>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <getc@plt>
cmp    $0x72,%al
jne    12ad <main+0x11d>
mov    %r14,%rdi
call   10b0 <getc@plt>
xor    %ebp,%ebp
cmp    $0x20,%al
je     12d2 <main+0x142>
lea    0xe46(%rip),%rdi
jmp    12b4 <main+0x124>
mov    0x2d88(%rip),%rax
mov    (%rax),%rcx
lea    0xda8(%rip),%rdi
mov    $0x18,%esi
jmp    128f <main+0xff>
mov    0x2d70(%rip),%rax
mov    (%rax),%rcx
lea    0xdb8(%rip),%rdi
mov    $0x16,%esi
jmp    128f <main+0xff>
mov    0x2d58(%rip),%rax
mov    (%rax),%rcx
lea    0xdb7(%rip),%rdi
mov    $0x2a,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdba(%rip),%rdi
mov    0x2d1d(%rip),%rax
mov    (%rax),%rcx
mov    $0x1e,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    129e <main+0x10e>

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_RDONLY = 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"r\00"
@.str.6 = [31 x i8] c"test failed: getc (fp) == 'r'\0A\00"
@.str.7 = [31 x i8] c"test failed: getc (fp) == ' '\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 %64
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_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %18
%29 = load i32*, i32** @stderr
%30 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32* %29)
store i32 1, i32* %8
br label %64
31: ; preds = %18
%32 = load i32, i32* %9
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %32, i32 5, i32 %33)
%35 = icmp eq i32 %34, 5
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = load i32*, i32** @stderr
%38 = call i32 @fputs(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32* %37)
store i32 1, i32* %8
br label %64
39: ; preds = %31
%40 = load i32, i32* %9
%41 = call i32* @fdopen(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %41, i32** %7
%42 = load i32*, i32** %7
%43 = icmp ne i32* %42, null
br i1 %43, label %47, label %44
44: ; preds = %39
%45 = load i32*, i32** @stderr
%46 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32* %45)
store i32 1, i32* %8
br label %64
47: ; preds = %39
%48 = load i32*, i32** %7
%49 = call signext i8 @getc(i32* %48)
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 114
br i1 %51, label %55, label %52
52: ; preds = %47
%53 = load i32*, i32** @stderr
%54 = call i32 @fputs(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32* %53)
store i32 1, i32* %8
br label %64
55: ; preds = %47
%56 = load i32*, i32** %7
%57 = call signext i8 @getc(i32* %56)
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 32
br i1 %59, label %63, label %60
60: ; preds = %55
%61 = load i32*, i32** @stderr
%62 = call i32 @fputs(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32* %61)
store i32 1, i32* %8
br label %64
63: ; preds = %55
br label %64
64: ; preds = %63, %60, %52, %44, %36, %28, %15
%65 = load i32*, i32** %7
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %67, %64
%71 = load i8*, i8** %6
%72 = call i32 @unlink(i8* %71)
%73 = load i32, i32* %8
ret i32 %73
}

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_RDONLY = 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"r\00"
@.str.6 = [31 x i8] c"test failed: getc (fp) == 'r'\0A\00"
@.str.7 = [31 x i8] c"test failed: getc (fp) == ' '\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 %44
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_RDONLY
%13 = tail call i32 @open(i8* %3, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32*, i32** @stderr
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 22, i64 1, i32* %16)
br label %44
18: ; preds = %9
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @lseek(i32 %13, i32 5, i32 %19)
%21 = icmp eq i32 %20, 5
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32*, i32** @stderr
%24 = tail call i64 @fwrite(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i64 42, i64 1, i32* %23)
br label %44
25: ; preds = %18
%26 = tail call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** @stderr
%30 = tail call i64 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 24, i64 1, i32* %29)
br label %44
31: ; preds = %25
%32 = tail call signext i8 @getc(i32* %26)
%33 = icmp eq i8 %32, 114
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call signext i8 @getc(i32* %26)
%36 = icmp eq i8 %35, 32
br i1 %36, label %41, label %37
37: ; preds = %34, %31
%38 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), %31 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), %34 ]
%39 = load i32*, i32** @stderr
%40 = tail call i64 @fwrite(i8* %38, i64 30, i64 1, i32* %39)
br label %41
41: ; preds = %37, %34
%42 = phi i32 [ 0, %34 ], [ 1, %37 ]
%43 = tail call i32 @fclose(i32* %26)
br label %44
44: ; preds = %6, %15, %22, %28, %41
%45 = phi i32 [ %42, %41 ], [ 1, %28 ], [ 1, %22 ], [ 1, %15 ], [ 1, %6 ]
%46 = tail call i32 @unlink(i8* %3)
ret i32 %45
}