#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*/ uid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ debugLog(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *ipv4_bindstring(char const *, unsigned int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long *);
scalar_t__ strcmp(char *, char const *);
char *strtok(char *, char *);
uid_t ipv4_tcp_port_uid(const char *ipv4, unsigned int port) {
  char buffer[1024];
  const char *bindstring = ipv4_bindstring(ipv4, port);
  FILE *portlist = fopen("/proc/net/tcp", "r");
  while (fgets(buffer, 1024, portlist)) {
    char *uid;
    char *field = strtok(buffer, " ");
    char *local = field = strtok(NULL, " ");
    field = strtok(NULL, " ");
    field = strtok(NULL, " ");
    field = strtok(NULL, " ");
    field = strtok(NULL, " ");
    field = strtok(NULL, " ");
    uid = strtok(NULL, " ");
    if (strcmp(local, bindstring) == 0) {
      unsigned long id;
      sscanf(uid, "%lu", &id);
      debugLog(LOG_DEBUG, "Found UID=%lu\n", id);
      fclose(portlist);
      return (uid_t)id;
    }
  }
  fclose(portlist);
  debugLog(LOG_NOTICE, "UID fpr UDP Port %i Not found\n", port);
  return ((uid_t)-1);
}


x86 assembly (clang, -O0):
<ipv4_tcp_port_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <ipv4_bindstring@plt>
mov    %rax,-0x428(%rbp)
lea    0xe54(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1318 <ipv4_tcp_port_uid+0x198>
lea    -0x420(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe0a(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
mov    %rax,-0x448(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdec(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdd5(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdbe(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xda7(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd90(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x440(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd79(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x438(%rbp)
mov    -0x448(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1313 <ipv4_tcp_port_uid+0x193>
mov    -0x438(%rbp),%rdi
lea    0xd44(%rip),%rsi
lea    -0x450(%rbp),%rdx
call   1080 <sscanf@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
mov    -0x450(%rbp),%rax
mov    %eax,%edx
lea    0xd23(%rip),%rsi
call   1030 <debugLog@plt>
mov    -0x430(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x450(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1343 <ipv4_tcp_port_uid+0x1c3>
jmp    11bf <ipv4_tcp_port_uid+0x3f>
mov    -0x430(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xcee(%rip),%rsi
call   1030 <debugLog@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_tcp_port_uid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,0x4(%rsp)
call   1070 <ipv4_bindstring@plt>
mov    %rax,%r12
lea    0xe5c(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
lea    0x10(%rsp),%r13
lea    0xe51(%rip),%rbp
nop
mov    %r13,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     127b <ipv4_tcp_port_uid+0xfb>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11c0 <ipv4_tcp_port_uid+0x40>
lea    0xdcb(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
call   1080 <sscanf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%edx
lea    0xdae(%rip),%rsi
call   1030 <debugLog@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%eax
jmp    12a1 <ipv4_tcp_port_uid+0x121>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
mov    0x4(%rsp),%edx
call   1030 <debugLog@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/net/tcp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [4 x i8] c"%lu\00"
@LOG_DEBUG = global i32 0
@.str.4 = [15 x i8] c"Found UID=%lu\0A\00"
@LOG_NOTICE = global i32 0
@.str.5 = [31 x i8] c"UID fpr UDP Port %i Not found\0A\00"
define i32 @ipv4_tcp_port_uid(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = call i8* @ipv4_bindstring(i8* %13, i32 %14)
store i8* %15, i8** %7
%16 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
br label %17
17: ; preds = %47, %2
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = load i32*, i32** %8
%20 = call i64 @fgets(i8* %18, i32 1024, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %48
22: ; preds = %17
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%24 = call i8* @strtok(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %10
%25 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %25, i8** %10
store i8* %25, i8** %11
%26 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %26, i8** %10
%27 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %27, i8** %10
%28 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %10
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %10
%30 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %10
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %31, i8** %9
%32 = load i8*, i8** %11
%33 = load i8*, i8** %7
%34 = call i64 @strcmp(i8* %32, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %22
%37 = load i8*, i8** %9
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %12)
%39 = load i32, i32* @LOG_DEBUG
%40 = load i64, i64* %12
%41 = trunc i64 %40 to i32
%42 = call i32 @debugLog(i32 %39, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
%45 = load i64, i64* %12
%46 = trunc i64 %45 to i32
store i32 %46, i32* %3
br label %54
47: ; preds = %22
br label %17
48: ; preds = %17
%49 = load i32*, i32** %8
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* @LOG_NOTICE
%52 = load i32, i32* %5
%53 = call i32 @debugLog(i32 %51, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %52)
store i32 -1, i32* %3
br label %54
54: ; preds = %48, %36
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/net/tcp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [4 x i8] c"%lu\00"
@LOG_DEBUG = global i32 0
@.str.4 = [15 x i8] c"Found UID=%lu\0A\00"
@LOG_NOTICE = global i32 0
@.str.5 = [31 x i8] c"UID fpr UDP Port %i Not found\0A\00"
define i32 @ipv4_tcp_port_uid(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @ipv4_bindstring(i8* %0, i32 %1)
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %11, %2
%9 = call i64 @fgets(i8* %5, i32 1024, i32* %7)
%10 = icmp eq i64 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = call i64 @strcmp(i8* %13, i8* %6)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %8
22: ; preds = %11
%23 = bitcast i64* %4 to i8*
%24 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %4)
%25 = load i32, i32* @LOG_DEBUG
%26 = load i64, i64* %4
%27 = trunc i64 %26 to i32
%28 = call i32 @debugLog(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = call i32 @fclose(i32* %7)
%30 = load i64, i64* %4
%31 = trunc i64 %30 to i32
br label %36
32: ; preds = %8
%33 = call i32 @fclose(i32* %7)
%34 = load i32, i32* @LOG_NOTICE
%35 = call i32 @debugLog(i32 %34, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %1)
br label %36
36: ; preds = %22, %32
%37 = phi i32 [ -1, %32 ], [ %31, %22 ]
ret i32 %37
}