#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int ftell(int /*<<< orphan*/ *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ unlink(char *);
int main() {
  FILE *f = popen("/bin/echo testing", "r");
  int i = getc(f);
  assert(ftell(f) == -1);
  assert(getc(f) == 'e');
  pclose(f);
  f = fopen("/tmp/test", "w");
  assert(ftell(f) == 0);
  fputs("test", f);
  assert(ftell(f) == 4);
  fclose(f);
  f = fopen("/tmp/test", "r");
  assert(ftell(f) == 0);
  i = getc(f);
  assert(ftell(f) == 1);
  ungetc(i, f);
  assert(ftell(f) == 0);
  i = getc(f);
  i = getc(f);
  assert(ftell(f) == 2);
  fclose(f);
  unlink("/tmp/test");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe51(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <getc@plt>
movsbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   10c0 <getc@plt>
movsbl %al,%eax
cmp    $0x65,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <pclose@plt>
lea    0xe01(%rip),%rdi
lea    0xe04(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x4,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd9f(%rip),%rdi
lea    0xd96(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   10c0 <getc@plt>
movsbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1090 <ungetc@plt>
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   10c0 <getc@plt>
movsbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <getc@plt>
movsbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x2,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xce2(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe52(%rip),%rdi
lea    0xe5d(%rip),%r15
mov    %r15,%rsi
call   1090 <popen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <getc@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   10c0 <getc@plt>
xor    %edi,%edi
cmp    $0x65,%al
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
lea    0xe12(%rip),%r14
lea    0xe15(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
lea    0xde9(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   10c0 <getc@plt>
movsbl %al,%ebp
mov    %rbx,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <ungetc@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
cmp    $0x2,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/bin/echo testing\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"/tmp/test\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [5 x i8] c"test\00"
define i32 @main() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i32* @popen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = call signext i8 @getc(i32* %4)
%6 = sext i8 %5 to i32
store i32 %6, i32* %2
%7 = load i32*, i32** %1
%8 = call i32 @ftell(i32* %7)
%9 = icmp eq i32 %8, -1
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32*, i32** %1
%13 = call signext i8 @getc(i32* %12)
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 101
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32*, i32** %1
%19 = call i32 @pclose(i32* %18)
%20 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %20, i32** %1
%21 = load i32*, i32** %1
%22 = call i32 @ftell(i32* %21)
%23 = icmp eq i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32*, i32** %1
%27 = call i32 @fputs(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32* %26)
%28 = load i32*, i32** %1
%29 = call i32 @ftell(i32* %28)
%30 = icmp eq i32 %29, 4
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32*, i32** %1
%34 = call i32 @fclose(i32* %33)
%35 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %1
%36 = load i32*, i32** %1
%37 = call i32 @ftell(i32* %36)
%38 = icmp eq i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32*, i32** %1
%42 = call signext i8 @getc(i32* %41)
%43 = sext i8 %42 to i32
store i32 %43, i32* %2
%44 = load i32*, i32** %1
%45 = call i32 @ftell(i32* %44)
%46 = icmp eq i32 %45, 1
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i32, i32* %2
%50 = load i32*, i32** %1
%51 = call i32 @ungetc(i32 %49, i32* %50)
%52 = load i32*, i32** %1
%53 = call i32 @ftell(i32* %52)
%54 = icmp eq i32 %53, 0
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i32*, i32** %1
%58 = call signext i8 @getc(i32* %57)
%59 = sext i8 %58 to i32
store i32 %59, i32* %2
%60 = load i32*, i32** %1
%61 = call signext i8 @getc(i32* %60)
%62 = sext i8 %61 to i32
store i32 %62, i32* %2
%63 = load i32*, i32** %1
%64 = call i32 @ftell(i32* %63)
%65 = icmp eq i32 %64, 2
%66 = zext i1 %65 to i32
%67 = call i32 @assert(i32 %66)
%68 = load i32*, i32** %1
%69 = call i32 @fclose(i32* %68)
%70 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"/bin/echo testing\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"/tmp/test\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [5 x i8] c"test\00"
define i32 @main() {
%1 = tail call i32* @popen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call signext i8 @getc(i32* %1)
%3 = tail call i32 @ftell(i32* %1)
%4 = icmp eq i32 %3, -1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call signext i8 @getc(i32* %1)
%8 = icmp eq i8 %7, 101
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = tail call i32 @pclose(i32* %1)
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @ftell(i32* %12)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 4, i64 1, i32* %12)
%18 = tail call i32 @ftell(i32* %12)
%19 = icmp eq i32 %18, 4
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = tail call i32 @fclose(i32* %12)
%23 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @ftell(i32* %23)
%25 = icmp eq i32 %24, 0
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = tail call signext i8 @getc(i32* %23)
%29 = sext i8 %28 to i32
%30 = tail call i32 @ftell(i32* %23)
%31 = icmp eq i32 %30, 1
%32 = zext i1 %31 to i32
%33 = tail call i32 @assert(i32 %32)
%34 = tail call i32 @ungetc(i32 %29, i32* %23)
%35 = tail call i32 @ftell(i32* %23)
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
%38 = tail call i32 @assert(i32 %37)
%39 = tail call signext i8 @getc(i32* %23)
%40 = tail call signext i8 @getc(i32* %23)
%41 = tail call i32 @ftell(i32* %23)
%42 = icmp eq i32 %41, 2
%43 = zext i1 %42 to i32
%44 = tail call i32 @assert(i32 %43)
%45 = tail call i32 @fclose(i32* %23)
%46 = tail call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}