#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 errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strerror(int);
unsigned char strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int write(int, unsigned char *, int);
int write_operation(int fd) {
  unsigned char buf[2];
  char msg[10];
  int err;
  fprintf(stdout, "addr --> ");
  fgets(msg, 10, stdin);
  buf[0] = strtol(msg, NULL, 0);
  fprintf(stdout, "data --> ");
  fgets(msg, 10, stdin);
  buf[1] = strtol(msg, NULL, 0);
  if ((write(fd, buf, 2)) != 2) {
    err = errno;
    fprintf(stderr,
            "\nFailed to write to the device (fd=%d, 0x%02X, 0x%02X).\n", fd,
            buf[0], buf[1]);
    fprintf(stderr, " err=%d: %s.\n\n", err, strerror(err));
    return 1;
  }
  fprintf(stdout, "Write success.\n");
  return 0;
}


x86 assembly (clang, -O0):
<write_operation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x14(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1040 <fgets@plt>
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %al,-0xa(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x14(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1040 <fgets@plt>
lea    -0x14(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     1261 <write_operation+0x111>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
movzbl -0xa(%rbp),%ecx
movzbl -0x9(%rbp),%r8d
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127f <write_operation+0x12f>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_operation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e60(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe8d(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e64(%rip),%rbx
mov    (%rbx),%edx
lea    0xe(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
call   1040 <fgets@plt>
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r12d
mov    %al,0xc(%rsp)
mov    0x0(%r13),%edi
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edx
mov    %rbp,%rdi
mov    $0xa,%esi
call   1040 <fgets@plt>
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%ebx
mov    %al,0xd(%rsp)
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
jne    1209 <write_operation+0xb9>
mov    0x0(%r13),%edi
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    125a <write_operation+0x10a>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbf(%rip),%r15
mov    (%r15),%edi
movzbl %r12b,%ecx
movzbl %bl,%r8d
lea    0xde9(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%ebx
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe07(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [10 x i8] c"addr --> \00"
@stdin = global i32 0
@.str.1 = [10 x i8] c"data --> \00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [57 x i8] c"\0AFailed to write to the device (fd=%d, 0x%02X, 0x%02X).\0A\00"
@.str.3 = [15 x i8] c" err=%d: %s.\0A\0A\00"
@.str.4 = [16 x i8] c"Write success.\0A\00"
define i32 @write_operation(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca [10 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @stdout
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %9, i32 10, i32 %10)
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%13 = call zeroext i8 @strtol(i8* %12, i32* null, i32 0)
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 %13, i8* %14
%15 = load i32, i32* @stdout
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %17, i32 10, i32 %18)
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%21 = call zeroext i8 @strtol(i8* %20, i32* null, i32 0)
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 %21, i8* %22
%23 = load i32, i32* %3
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%25 = call i32 @write(i32 %23, i8* %24, i32 2)
%26 = icmp ne i32 %25, 2
br i1 %26, label %27, label %43
27: ; preds = %1
%28 = load i32, i32* @errno
store i32 %28, i32* %6
%29 = load i32, i32* @stderr
%30 = load i32, i32* %3
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %33, i32 %36)
%38 = load i32, i32* @stderr
%39 = load i32, i32* %6
%40 = load i32, i32* %6
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %39, i8* %41)
store i32 1, i32* %2
br label %46
43: ; preds = %1
%44 = load i32, i32* @stdout
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %46
46: ; preds = %43, %27
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [10 x i8] c"addr --> \00"
@stdin = global i32 0
@.str.1 = [10 x i8] c"data --> \00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [57 x i8] c"\0AFailed to write to the device (fd=%d, 0x%02X, 0x%02X).\0A\00"
@.str.3 = [15 x i8] c" err=%d: %s.\0A\0A\00"
@.str.4 = [16 x i8] c"Write success.\0A\00"
define i32 @write_operation(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @stdout
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %5, i32 10, i32 %8)
%10 = call zeroext i8 @strtol(i8* nocapture %5, i32* null, i32 0)
store i8 %10, i8* %4
%11 = load i32, i32* @stdout
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %5, i32 10, i32 %13)
%15 = call zeroext i8 @strtol(i8* nocapture %5, i32* null, i32 0)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 %15, i8* %16
%17 = call i32 @write(i32 %0, i8* %4, i32 2)
%18 = icmp eq i32 %17, 2
br i1 %18, label %28, label %19
19: ; preds = %1
%20 = load i32, i32* @errno
%21 = load i32, i32* @stderr
%22 = zext i8 %10 to i32
%23 = zext i8 %15 to i32
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %22, i32 %23)
%25 = load i32, i32* @stderr
%26 = call i8* @strerror(i32 %20)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %20, i8* %26)
br label %31
28: ; preds = %1
%29 = load i32, i32* @stdout
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %28, %19
%32 = phi i32 [ 1, %19 ], [ 0, %28 ]
ret i32 %32
}