#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*/ portstr;
typedef int /*<<< orphan*/ logmsg;
typedef int /*<<< orphan*/ hostfile;

/* Variables and functions */
int /*<<< orphan*/ logging(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ writeFile(char *, int, char *);
int writePort(int port, char *hostip) {
  char portstr[6];
  sprintf(portstr, "%d", port);
  char hostfile[17];
  memset(hostfile, 0, sizeof(hostfile));
  strcat(hostfile, hostip);
  if (writeFile(portstr, sizeof(portstr), hostfile) < 0) {
    perror("writeport");
    return -1;
  }
  char logmsg[128];
  snprintf(logmsg, sizeof(logmsg), "write port %s to file: %s\n", portstr,
           hostfile);
  logging("lsrp-router.log", logmsg);
  return 0;
}


x86 assembly (clang, -O0):
<writePort>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x16(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe70(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x16(%rbp),%rdi
lea    -0x30(%rbp),%rdx
mov    $0x6,%esi
call   1050 <writeFile@plt>
cmp    $0x0,%rax
jge    11e6 <writePort+0x76>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <writePort+0xb0>
lea    -0xb0(%rbp),%rdi
lea    -0x16(%rbp),%rcx
lea    -0x30(%rbp),%r8
mov    $0x80,%esi
lea    0xe0c(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0xb0(%rbp),%rsi
lea    0xe14(%rip),%rdi
call   1060 <logging@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writePort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xe77(%rip),%rsi
lea    0xa(%rsp),%r15
mov    %r15,%rdi
call   1090 <sprintf@plt>
lea    0x10(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
mov    $0x6,%esi
mov    %rbp,%rdx
call   1050 <writeFile@plt>
test   %rax,%rax
js     1210 <writePort+0xa0>
lea    0xe39(%rip),%rdx
lea    0x30(%rsp),%rbx
lea    0xa(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
lea    0xe31(%rip),%rdi
mov    %rbx,%rsi
call   1060 <logging@plt>
mov    %r14d,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11ff <writePort+0x8f>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"writeport\00"
@.str.2 = [27 x i8] c"write port %s to file: %s\0A\00"
@.str.3 = [16 x i8] c"lsrp-router.log\00"
define i32 @writePort(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [6 x i8]
%7 = alloca [17 x i8]
%8 = alloca [128 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 17)
%14 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%15 = load i8*, i8** %5
%16 = call i32 @strcat(i8* %14, i8* %15)
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%19 = call i64 @writeFile(i8* %17, i32 6, i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
23: ; preds = %2
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%27 = call i32 @snprintf(i8* %24, i32 128, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %26)
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%29 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i32 0, i32* %3
br label %30
30: ; preds = %23, %21
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [10 x i8] c"writeport\00"
@.str.2 = [27 x i8] c"write port %s to file: %s\0A\00"
@.str.3 = [16 x i8] c"lsrp-router.log\00"
define i32 @writePort(i32 %0, i8* %1) {
%3 = alloca [6 x i8]
%4 = alloca [17 x i8]
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 17)
%10 = call i32 @strcat(i8* %8, i8* %1)
%11 = call i64 @writeFile(i8* %6, i32 6, i8* %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %19
15: ; preds = %2
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = call i32 @snprintf(i8* %16, i32 128, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %8)
%18 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %19
19: ; preds = %15, %13
%20 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %20
}