#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stdout;
char *strchr(char *, char);
run(const char *nam, FILE *f) {
  printf("--- %s ---\n", nam);
  fprintf(f, "hello %s %d\n", "there", 5);
  fprintf(f, "at %d\n", ftell(f));
  fseek(f, -5, SEEK_END);
  rewind(f);
  {
    char buf[16], *p;
    if (fgets(buf, 16, f)) {
      p = strchr(buf, '\n');
      if (p)
        *p = 0;
      printf("read \"%s\"\n", buf);
    } else {
      printf("no read\n");
    }
  }
  if (f != stdout)
    fclose(f);
  printf("--- fin ---\n");
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe5f(%rip),%rsi
lea    0xe65(%rip),%rdx
mov    $0x5,%ecx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffb,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x10,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     125c <run+0xdc>
lea    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1245 <run+0xc5>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126a <run+0xea>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
je     1287 <run+0x107>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe5c(%rip),%rsi
lea    0xe62(%rip),%rdx
mov    %rbx,%rdi
mov    $0x5,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfffffffb,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     123c <run+0xac>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1229 <run+0x99>
movb   $0x0,(%rax)
lea    0xdf6(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1248 <run+0xb8>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d89(%rip),%rax
cmp    %rbx,(%rax)
je     125c <run+0xcc>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"--- %s ---\0A\00"
@.str.1 = [13 x i8] c"hello %s %d\0A\00"
@.str.2 = [6 x i8] c"there\00"
@.str.3 = [7 x i8] c"at %d\0A\00"
@SEEK_END = global i32 0
@.str.4 = [11 x i8] c"read \22%s\22\0A\00"
@.str.5 = [9 x i8] c"no read\0A\00"
@stdout = global i32* null
@.str.6 = [13 x i8] c"--- fin ---\0A\00"
define i32 @run(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [16 x i8]
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32*, i32** %5
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%12 = load i32*, i32** %5
%13 = load i32*, i32** %5
%14 = call i32 @ftell(i32* %13)
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %5
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 -5, i32 %17)
%19 = load i32*, i32** %5
%20 = call i32 @rewind(i32* %19)
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i64 @fgets(i8* %21, i32 16, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %2
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%27 = call i8* @strchr(i8* %26, i8 signext 10)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i8*, i8** %7
store i8 0, i8* %31
br label %32
32: ; preds = %30, %25
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %37
35: ; preds = %2
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = load i32*, i32** %5
%39 = load i32*, i32** @stdout
%40 = icmp ne i32* %38, %39
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32*, i32** %5
%43 = call i32 @fclose(i32* %42)
br label %44
44: ; preds = %41, %37
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [12 x i8] c"--- %s ---\0A\00"
@.str.1 = [13 x i8] c"hello %s %d\0A\00"
@.str.2 = [6 x i8] c"there\00"
@.str.3 = [7 x i8] c"at %d\0A\00"
@SEEK_END = global i32 0
@.str.4 = [11 x i8] c"read \22%s\22\0A\00"
@stdout = global i32* null
@str = [8 x i8] c"no read\00"
@str.7 = [12 x i8] c"--- fin ---\00"
define i32 @run(i8* %0, i32* nocapture %1) {
%3 = alloca [16 x i8]
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%6 = tail call i32 @ftell(i32* %1)
%7 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %1, i32 -5, i32 %8)
%10 = tail call i32 @rewind(i32* %1)
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%12 = call i64 @fgets(i8* %11, i32 16, i32* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = call i8* @strchr(i8* %11, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14
store i8 0, i8* %15
br label %18
18: ; preds = %17, %14
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %11)
br label %22
20: ; preds = %2
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = load i32*, i32** @stdout
%24 = icmp eq i32* %23, %1
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @fclose(i32* %1)
br label %27
27: ; preds = %25, %22
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
ret i32 undef
}