#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("need to supply one username as argument\n");
    exit(1);
  }
  FILE *pw = fopen("/etc/passwd", "r");
  if (pw == NULL) {
    perror("fopen /etc/passwd");
    exit(2);
  }
  char *pwstring = malloc(4096);
  if (pwstring == NULL) {
    perror("malloc buffer");
    exit(3);
  }
  while (fgets(pwstring, 4096, pw)) {
    char *pwstring_copy = strdup(pwstring);
    if (pwstring_copy == NULL) {
      perror("strdup");
      exit(4);
    }
    char *p = strtok(pwstring_copy, ":");
    if (strcmp(argv[1], p) == 0) {
      printf("%s", pwstring);
    }
    free(pwstring_copy);
    pwstring_copy = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d8 <main+0x38>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe50(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1210 <main+0x70>
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123f <main+0x9f>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
jmp    1244 <main+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12e5 <main+0x145>
mov    -0x20(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    128e <main+0xee>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12cf <main+0x12f>
mov    -0x20(%rbp),%rsi
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    1244 <main+0xa4>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12af <main+0xff>
mov    %rsi,%r14
lea    0xe38(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c5 <main+0x115>
mov    %rax,%r15
mov    $0x1000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12db <main+0x12b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1288 <main+0xd8>
lea    0xe0f(%rip),%r12
lea    0xe0a(%rip),%r13
jmp    124d <main+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1288 <main+0xd8>
mov    %rbx,%rdi
call   10d0 <strdup@plt>
test   %rax,%rax
je     1299 <main+0xe9>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
call   10b0 <strtok@plt>
mov    0x8(%r14),%rdi
mov    %rax,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1230 <main+0x80>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1230 <main+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd80(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x4,%edi
call   10c0 <exit@plt>
lea    0xd76(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd34(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    0xd30(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"need to supply one username as argument\0A\00"
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [18 x i8] c"fopen /etc/passwd\00"
@.str.4 = [14 x i8] c"malloc buffer\00"
@.str.5 = [7 x i8] c"strdup\00"
@.str.6 = [2 x i8] c":\00"
@.str.7 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %15
%23 = call i8* @malloc(i32 4096)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %22
br label %30
30: ; preds = %55, %29
%31 = load i8*, i8** %7
%32 = load i32*, i32** %6
%33 = call i64 @fgets(i8* %31, i32 4096, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %58
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = call i8* @strdup(i8* %36)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 4)
unreachable
43: ; preds = %35
%44 = load i8*, i8** %8
%45 = call i8* @strtok(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* %45, i8** %9
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = load i8*, i8** %9
%50 = call i64 @strcmp(i8* %48, i8* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = load i8*, i8** %7
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %52, %43
%56 = load i8*, i8** %8
%57 = call i32 @free(i8* %56)
store i8* null, i8** %8
br label %30
58: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [18 x i8] c"fopen /etc/passwd\00"
@.str.4 = [14 x i8] c"malloc buffer\00"
@.str.5 = [7 x i8] c"strdup\00"
@.str.6 = [2 x i8] c":\00"
@.str.7 = [3 x i8] c"%s\00"
@str = [40 x i8] c"need to supply one username as argument\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %7
%14 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i64 @fgets(i8* %14, i32 4096, i32* %8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %41, label %19
19: ; preds = %16
%20 = getelementptr inbounds i8*, i8** %1, i64 1
br label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @exit(i32 3)
unreachable
24: ; preds = %19, %37
%25 = tail call i8* @strdup(i8* %14)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 @exit(i32 4)
unreachable
30: ; preds = %24
%31 = tail call i8* @strtok(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%32 = load i8*, i8** %20
%33 = tail call i64 @strcmp(i8* %32, i8* %31)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %14)
br label %37
37: ; preds = %35, %30
%38 = tail call i32 @free(i8* %25)
%39 = tail call i64 @fgets(i8* %14, i32 4096, i32* %8)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %24
41: ; preds = %37, %16
ret i32 0
}