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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strtok(char *, char *);
char *getUserName(uid_t codeUser) {
  const char FileUsersData[] = "/etc/passwd";
  const int PosUID = 2;
  char *strUser, *userTmp, *tokenTmp;
  FILE *inputFile;
  char line[2047];
  char strUID[20];
  int i;
  strUser = NULL;
  if ((inputFile = fopen(FileUsersData, "r")) != NULL) {
    sprintf(strUID, "%d", codeUser);
    while (!feof(inputFile)) {
      fgets(line, 2047, inputFile);
      userTmp = strtok(line, ":");
      for (i = 1; i <= PosUID; i++)
        tokenTmp = strtok(NULL, ":");
      if (strcmp(tokenTmp, strUID) == 0) {
        strUser = malloc(strlen(userTmp) + 1);
        strcpy(strUser, userTmp);
        break;
      }
    }
    fclose(inputFile);
  }
  return strUser;
}


x86 assembly (clang, -O0):
<getUserName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
mov    %edi,-0x4(%rbp)
mov    0xe4b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe49(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0x2,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12e1 <getUserName+0x141>
lea    -0x860(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe0e(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1223 <getUserName+0x83>
jmp    12d8 <getUserName+0x138>
lea    -0x840(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x7ff,%esi
call   1060 <fgets@plt>
lea    -0x840(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x864(%rbp)
cmpl   $0x2,-0x864(%rbp)
jg     128e <getUserName+0xee>
xor    %eax,%eax
mov    %eax,%edi
lea    0xda0(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x864(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x864(%rbp)
jmp    1259 <getUserName+0xb9>
mov    -0x30(%rbp),%rdi
lea    -0x860(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12d3 <getUserName+0x133>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    12d8 <getUserName+0x138>
jmp    1205 <getUserName+0x65>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getUserName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %edi,%ebx
movabs $0x7361702f6374652f,%rax
70 61 73
mov    %rax,(%rsp)
movl   $0x647773,0x8(%rsp)
lea    0xe3c(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1287 <getUserName+0xe7>
mov    %rax,%r13
lea    0xe23(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %ebx,%edx
call   10c0 <sprintf@plt>
xor    %r14d,%r14d
lea    0x30(%rsp),%r12
lea    0xe08(%rip),%rbx
nopl   0x0(%rax)
mov    %r13,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    127d <getUserName+0xdd>
mov    %r12,%rdi
mov    $0x7ff,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10b0 <strtok@plt>
mov    %rax,%rbp
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10b0 <strtok@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1210 <getUserName+0x70>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
jmp    128a <getUserName+0xea>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.getUserName.FileUsersData = [12 x i8] c"/etc/passwd\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c":\00"
define i8* @getUserName(i32 %0) {
%2 = alloca i32
%3 = alloca [12 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [2047 x i8]
%10 = alloca [20 x i8]
%11 = alloca i32
store i32 %0, i32* %2
%12 = bitcast [12 x i8]* %3 to i8*
store i32 2, i32* %4
store i8* null, i8** %5
%13 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %56
16: ; preds = %1
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* %2
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %52, %16
%21 = load i32*, i32** %8
%22 = call i32 @feof(i32* %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
br i1 %24, label %25, label %53
25: ; preds = %20
%26 = getelementptr inbounds [2047 x i8], [2047 x i8]* %9, i64 0, i64 0
%27 = load i32*, i32** %8
%28 = call i32 @fgets(i8* %26, i32 2047, i32* %27)
%29 = getelementptr inbounds [2047 x i8], [2047 x i8]* %9, i64 0, i64 0
%30 = call i8* @strtok(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %6
store i32 1, i32* %11
br label %31
31: ; preds = %36, %25
%32 = load i32, i32* %11
%33 = icmp sle i32 %32, 2
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %7
br label %36
36: ; preds = %34
%37 = load i32, i32* %11
%38 = add nsw i32 %37, 1
store i32 %38, i32* %11
br label %31
39: ; preds = %31
%40 = load i8*, i8** %7
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%42 = call i64 @strcmp(i8* %40, i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = call i64 @strlen(i8* %45)
%47 = add nsw i64 %46, 1
%48 = call i8* @malloc(i64 %47)
store i8* %48, i8** %5
%49 = load i8*, i8** %5
%50 = load i8*, i8** %6
%51 = call i32 @strcpy(i8* %49, i8* %50)
br label %53
52: ; preds = %39
br label %20
53: ; preds = %44, %20
%54 = load i32*, i32** %8
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %1
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
@__const.getUserName.FileUsersData = [12 x i8] c"/etc/passwd\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c":\00"
define i8* @getUserName(i32 %0) {
%2 = alloca [12 x i8]
%3 = alloca [2047 x i8]
%4 = alloca [20 x i8]
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [2047 x i8], [2047 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%8 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %30, label %10
10: ; preds = %1
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %12
12: ; preds = %15, %10
%13 = call i32 @feof(i32* %8)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = call i32 @fgets(i8* %6, i32 2047, i32* %8)
%17 = call i8* @strtok(i8* %6, 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* %19, i8* %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %12
22: ; preds = %15
%23 = call i64 @strlen(i8* dereferenceable(1) %17)
%24 = add nsw i64 %23, 1
%25 = call i8* @malloc(i64 %24)
%26 = call i32 @strcpy(i8* %25, i8* %17)
br label %27
27: ; preds = %12, %22
%28 = phi i8* [ %25, %22 ], [ null, %12 ]
%29 = call i32 @fclose(i32* %8)
br label %30
30: ; preds = %27, %1
%31 = phi i8* [ %28, %27 ], [ null, %1 ]
ret i8* %31
}