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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int main(int argc, char **argv) {
  int fileDescriptor, counter = 1;
  char buffer[100];
  fileDescriptor = open("/dev/TestDevice", O_RDONLY);
  printf("%d\n", fileDescriptor);
  while (counter > 0) {
    counter = read(fileDescriptor, buffer, 10);
    if (counter < 0) {
      break;
    }
    buffer[counter] = 0;
    printf("Read %2d chars: %s\n", counter, buffer);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jle    11c9 <main+0x99>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a6 <main+0x76>
jmp    11c9 <main+0x99>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
mov    -0x18(%rbp),%esi
lea    -0x80(%rbp),%rdx
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1179 <main+0x49>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r15d
lea    0xeb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rbx
lea    0xea8(%rip),%r14
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <read@plt>
test   %eax,%eax
js     119f <main+0x6f>
mov    %eax,%ebp
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jne    1170 <main+0x40>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/TestDevice\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [20 x i8] c"Read %2d chars: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %23, %2
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i32, i32* %6
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 10)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %30
23: ; preds = %16
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %7
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
br label %13
30: ; preds = %22, %13
%31 = load i32, i32* @EXIT_SUCCESS
ret i32 %31
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/TestDevice\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [20 x i8] c"Read %2d chars: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %2, %11
%9 = call i32 @read(i32 %6, i8* %4, i32 10)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %4)
%15 = icmp eq i32 %9, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %11
%17 = load i32, i32* @EXIT_SUCCESS
ret i32 %17
}