#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 buffer {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int buffer_at(struct buffer *, int);
int /*<<< orphan*/ buffer_end(struct buffer *);
struct buffer *buffer_fromfile(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int, int);
int test_buffer_at(void) {
  const char fn[] = "buffertest.c";
  struct buffer *b;
  FILE *fh;
  int ret = -1;
  fh = fopen(fn, "r");
  if (fh == NULL) {
    perror("fopen");
    return -1;
  }
  b = buffer_fromfile(fh, 32);
  fclose(fh);
  if (b == NULL)
    goto error;
  printf("at(0): %X\nat(10): %X\nat(-1): %X\nat(-2): %X\n", buffer_at(b, 0),
         buffer_at(b, 10), buffer_at(b, -1), buffer_at(b, -2));
  ret = 1;
error:
  buffer_end(b);
  return ret;
}


x86 assembly (clang, -O0):
<test_buffer_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xe81(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xe7f(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xe7a(%rip),%al
mov    %al,-0x5(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
lea    -0x11(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d3 <test_buffer_at+0x63>
lea    0xe4d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <test_buffer_at+0xfa>
mov    -0x28(%rbp),%rdi
mov    $0x20,%esi
call   1090 <buffer_fromfile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11fe <test_buffer_at+0x8e>
jmp    125b <test_buffer_at+0xeb>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1080 <buffer_at@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1080 <buffer_at@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xffffffff,%esi
call   1080 <buffer_at@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xfffffffe,%esi
call   1080 <buffer_at@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    %eax,%r8d
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <buffer_end@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_buffer_at>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x632e7473657472,%rax
mov    %rax,0xd(%rsp)
movabs $0x6574726566667562,%rax
72 74 65
mov    %rax,0x8(%rsp)
lea    0xe6e(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1243 <test_buffer_at+0xd3>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x20,%esi
call   1090 <buffer_fromfile@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rbx,%rbx
je     1229 <test_buffer_at+0xb9>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <buffer_at@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    $0xa,%esi
call   1080 <buffer_at@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1080 <buffer_at@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0xfffffffe,%esi
call   1080 <buffer_at@plt>
lea    0xe03(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    122e <test_buffer_at+0xbe>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <buffer_end@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1236 <test_buffer_at+0xc6>

LLVM IR (-O0):
%struct.buffer = type { i32 }
@__const.test_buffer_at.fn = [13 x i8] c"buffertest.c\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [44 x i8] c"at(0): %X\0Aat(10): %X\0Aat(-1): %X\0Aat(-2): %X\0A\00"
define i32 @test_buffer_at() {
%1 = alloca i32
%2 = alloca [13 x i8]
%3 = alloca %struct.buffer*
%4 = alloca i32*
%5 = alloca i32
%6 = bitcast [13 x i8]* %2 to i8*
store i32 -1, i32* %5
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
13: ; preds = %0
%14 = load i32*, i32** %4
%15 = call %struct.buffer* @buffer_fromfile(i32* %14, i32 32)
store %struct.buffer* %15, %struct.buffer** %3
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = load %struct.buffer*, %struct.buffer** %3
%19 = icmp eq %struct.buffer* %18, null
br i1 %19, label %20, label %21
20: ; preds = %13
br label %31
21: ; preds = %13
%22 = load %struct.buffer*, %struct.buffer** %3
%23 = call i32 @buffer_at(%struct.buffer* %22, i32 0)
%24 = load %struct.buffer*, %struct.buffer** %3
%25 = call i32 @buffer_at(%struct.buffer* %24, i32 10)
%26 = load %struct.buffer*, %struct.buffer** %3
%27 = call i32 @buffer_at(%struct.buffer* %26, i32 -1)
%28 = load %struct.buffer*, %struct.buffer** %3
%29 = call i32 @buffer_at(%struct.buffer* %28, i32 -2)
%30 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %25, i32 %27, i32 %29)
store i32 1, i32* %5
br label %31
31: ; preds = %21, %20
%32 = load %struct.buffer*, %struct.buffer** %3
%33 = call i32 @buffer_end(%struct.buffer* %32)
%34 = load i32, i32* %5
store i32 %34, i32* %1
br label %35
35: ; preds = %31, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
@__const.test_buffer_at.fn = [13 x i8] c"buffertest.c\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [44 x i8] c"at(0): %X\0Aat(10): %X\0Aat(-1): %X\0Aat(-2): %X\0A\00"
define i32 @test_buffer_at() {
%1 = alloca [13 x i8]
%2 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%3 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
7: ; preds = %0
%8 = tail call %struct.buffer* @buffer_fromfile(i32* %3, i32 32)
%9 = tail call i32 @fclose(i32* %3)
%10 = icmp eq %struct.buffer* %8, null
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = tail call i32 @buffer_at(%struct.buffer* %8, i32 0)
%13 = tail call i32 @buffer_at(%struct.buffer* %8, i32 10)
%14 = tail call i32 @buffer_at(%struct.buffer* %8, i32 -1)
%15 = tail call i32 @buffer_at(%struct.buffer* %8, i32 -2)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13, i32 %14, i32 %15)
br label %17
17: ; preds = %7, %11
%18 = phi i32 [ -1, %7 ], [ 1, %11 ]
%19 = tail call i32 @buffer_end(%struct.buffer* %8)
br label %20
20: ; preds = %17, %5
%21 = phi i32 [ -1, %5 ], [ %18, %17 ]
ret i32 %21
}