#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 getDebug();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int write_port(int *fd, char *s) {
  int n, ret = 1;
  if (getDebug() > 1)
    printf("I: Write port: ");
  n = write(*fd, s, strlen(s));
  if (n < 0) {
    if (getDebug() > 1)
      perror("\nE: write() failed!");
    ret = -1;
  } else if (getDebug() > 1)
    puts("OK");
  return ret;
}


x86 assembly (clang, -O0):
<write_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    1193 <write_port+0x33>
lea    0xe72(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f0 <write_port+0x90>
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    11e4 <write_port+0x84>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x18(%rbp)
jmp    1211 <write_port+0xb1>
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    120c <write_port+0xac>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
jmp    1211 <write_port+0xb1>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_port>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <getDebug@plt>
cmp    $0x2,%eax
jl     1182 <write_port+0x22>
lea    0xe83(%rip),%rdi
call   1060 <printf@plt>
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getDebug@plt>
test   %ebx,%ebx
js     11be <write_port+0x5e>
mov    $0x1,%ebx
cmp    $0x2,%eax
jl     11c8 <write_port+0x68>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
jmp    11c8 <write_port+0x68>
mov    $0xffffffff,%ebx
cmp    $0x2,%eax
jge    11cf <write_port+0x6f>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
jmp    11c8 <write_port+0x68>

LLVM IR (-O0):
@.str = [16 x i8] c"I: Write port: \00"
@.str.1 = [20 x i8] c"\0AE: write() failed!\00"
@.str.2 = [3 x i8] c"OK\00"
define i32 @write_port(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32 1, i32* %6
%7 = call i32 (...) @getDebug()
%8 = icmp sgt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32*, i32** %3
%13 = load i32, i32* %12
%14 = load i8*, i8** %4
%15 = load i8*, i8** %4
%16 = call i32 @strlen(i8* %15)
%17 = call i32 @write(i32 %13, i8* %14, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = call i32 (...) @getDebug()
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
store i32 -1, i32* %6
br label %32
26: ; preds = %11
%27 = call i32 (...) @getDebug()
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
br label %32
32: ; preds = %31, %25
%33 = load i32, i32* %6
ret i32 %33
}

LLVM IR (-O2):
@.str = [16 x i8] c"I: Write port: \00"
@.str.1 = [20 x i8] c"\0AE: write() failed!\00"
@.str.2 = [3 x i8] c"OK\00"
define i32 @write_port(i32* nocapture %0, i8* %1) {
%3 = tail call i32 (...) @getDebug()
%4 = icmp sgt i32 %3, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* %0
%9 = tail call i32 @strlen(i8* %1)
%10 = tail call i32 @write(i32 %8, i8* %1, i32 %9)
%11 = icmp slt i32 %10, 0
%12 = tail call i32 (...) @getDebug()
%13 = icmp sgt i32 %12, 1
br i1 %11, label %14, label %17
14: ; preds = %7
br i1 %13, label %15, label %20
15: ; preds = %14
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %20
17: ; preds = %7
br i1 %13, label %18, label %20
18: ; preds = %17
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %14, %15, %17, %18
%21 = phi i32 [ 1, %18 ], [ 1, %17 ], [ -1, %15 ], [ -1, %14 ]
ret i32 %21
}