#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 /*<<< orphan*/ O_RDWR;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char *argv[]) {
  int globalvarfd = 0;
  int input = 0;
  globalvarfd = open("/dev/globalvar0", O_RDWR);
  if (globalvarfd < 0) {
    perror("open globalvar0 failed\n");
    return -1;
  }
  do {
    scanf("%d", &input);
    write(globalvarfd, &input, sizeof(int));
  } while (input != 0xFF);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119e <main+0x5e>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <main+0x98>
jmp    11a3 <main+0x63>
lea    0xe7e(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmpl   $0xff,-0x18(%rbp)
jne    11a3 <main+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11af <main+0x6f>
mov    %eax,%ebp
lea    0xeb6(%rip),%r14
lea    0xc(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <scanf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmpl   $0xff,0xc(%rsp)
jne    1180 <main+0x40>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a6 <main+0x66>

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/globalvar0\00"
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"open globalvar0 failed\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %23
14: ; preds = %2
br label %15
15: ; preds = %19, %14
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%17 = load i32, i32* %6
%18 = call i32 @write(i32 %17, i32* %7, i32 4)
br label %19
19: ; preds = %15
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 255
br i1 %21, label %15, label %22
22: ; preds = %19
store i32 0, i32* %3
br label %23
23: ; preds = %22, %12
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/globalvar0\00"
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"open globalvar0 failed\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %15
10: ; preds = %2, %10
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%12 = call i32 @write(i32 %6, i32* %3, i32 4)
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 255
br i1 %14, label %15, label %10
15: ; preds = %10, %8
%16 = phi i32 [ -1, %8 ], [ 0, %10 ]
ret i32 %16
}