#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int main(int argc, char **argv) {
  int i, count, rv, fd, buf;
  if (argc != 3) {
    printf("USAGE: uio_read <filename> <count>\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDWR);
  count = atoi(argv[2]);
  printf("[UIO] opening file %s\n", argv[1]);
  printf("[UIO] reading\n");
  for (i = 0; i < count; i++) {
    rv = read(fd, &buf, sizeof(buf));
    printf("[UIO] buf is %d\n", buf);
  }
  close(fd);
  printf("[UIO] Exiting...\n");
}


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)
cmpl   $0x3,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    122b <main+0xcb>
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ec <main+0x8c>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1212 <main+0xa2>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x10(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%r14),%rsi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jle    11f2 <main+0x82>
lea    0x4(%rsp),%r14
lea    0xe47(%rip),%r15
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebp
jne    11d0 <main+0x60>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"USAGE: uio_read <filename> <count>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"[UIO] opening file %s\0A\00"
@.str.2 = [15 x i8] c"[UIO] reading\0A\00"
@.str.3 = [17 x i8] c"[UIO] buf is %d\0A\00"
@.str.4 = [18 x i8] c"[UIO] Exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %31
31: ; preds = %40, %16
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i32, i32* %9
%37 = call i32 @read(i32 %36, i32* %10, i32 4)
store i32 %37, i32* %8
%38 = load i32, i32* %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %35
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %31
43: ; preds = %31
%44 = load i32, i32* %9
%45 = call i32 @close(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"[UIO] opening file %s\0A\00"
@.str.3 = [17 x i8] c"[UIO] buf is %d\0A\00"
@str = [14 x i8] c"[UIO] reading\00"
@str.5 = [17 x i8] c"[UIO] Exiting...\00"
@str.6 = [35 x i8] c"USAGE: uio_read <filename> <count>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = load i8*, i8** %10
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%20 = icmp sgt i32 %16, 0
br i1 %20, label %21, label %28
21: ; preds = %9, %21
%22 = phi i32 [ %26, %21 ], [ 0, %9 ]
%23 = call i32 @read(i32 %13, i32* %3, i32 4)
%24 = load i32, i32* %3
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = add nuw nsw i32 %22, 1
%27 = icmp eq i32 %26, %16
br i1 %27, label %28, label %21
28: ; preds = %21, %9
%29 = tail call i32 @close(i32 %13)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
ret i32 0
}