#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 *);
int /*<<< orphan*/ strcpy(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));
  strcpy(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    0xe60(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x16(%rbp),%rdi
lea    -0x30(%rbp),%rdx
mov    $0x6,%esi
call   1060 <writeFile@plt>
cmp    $0x0,%rax
jge    1206 <writePort+0x86>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <writePort+0xc0>
lea    -0xb0(%rbp),%rdi
lea    -0x16(%rbp),%rcx
lea    -0x30(%rbp),%r8
mov    $0x80,%esi
lea    0xdf1(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xb0(%rbp),%rsi
lea    0xdf9(%rip),%rdi
call   1070 <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    0xe67(%rip),%rsi
lea    0xa(%rsp),%r15
mov    %r15,%rdi
call   10a0 <sprintf@plt>
lea    0x10(%rsp),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1050 <memset@plt>
lea    0xe3f(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    $0x6,%esi
mov    %rbp,%rdx
call   1060 <writeFile@plt>
test   %rax,%rax
js     122f <writePort+0xaf>
lea    0xe1f(%rip),%rdx
lea    0x30(%rsp),%rbx
lea    0xa(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
lea    0xe17(%rip),%rdi
mov    %rbx,%rsi
call   1070 <logging@plt>
mov    %r14d,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121e <writePort+0x9e>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"../.\00"
@.str.2 = [10 x i8] c"writeport\00"
@.str.3 = [27 x i8] c"write port %s to file: %s\0A\00"
@.str.4 = [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 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %5
%18 = call i32 @strcat(i8* %16, i8* %17)
%19 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%20 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%21 = call i64 @writeFile(i8* %19, i32 6, i8* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %32
25: ; preds = %2
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%29 = call i32 @snprintf(i8* %26, i32 128, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %27, i8* %28)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%31 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %30)
store i32 0, i32* %3
br label %32
32: ; preds = %25, %23
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [5 x i8] c"../.\00"
@.str.2 = [10 x i8] c"writeport\00"
@.str.3 = [27 x i8] c"write port %s to file: %s\0A\00"
@.str.4 = [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 @strcpy(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @strcat(i8* %8, i8* %1)
%12 = call i64 @writeFile(i8* %6, i32 6, i8* %8)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %20
16: ; preds = %2
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%18 = call i32 @snprintf(i8* %17, i32 128, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %8)
%19 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %17)
br label %20
20: ; preds = %16, %14
%21 = phi i32 [ -1, %14 ], [ 0, %16 ]
ret i32 %21
}